How to convert Macro.ijm to Python code

I am Dan . My language is not English. if my English is difficult to understand,please tell me that. i rewrite about this.

【Assumptions / What I want to achieve】

I want to convert thic code(written in Macro.ijm by macro record) to Jpython.

### Written in Macro.ijm
open("C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064c.jpg");

run("RGB to CIELAB");

run("Stack to Images");

open("C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064cROI.zip");

roiManager("Open", "C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064cROI.zip");



selectWindow("a");
roiManager("Show All");
roiManager("Show All with labels");
roiManager("Measure");

selectWindow("L");
roiManager("Show All");
roiManager("Show All with labels");
roiManager("Measure");

selectWindow("b");
roiManager("Show All");
roiManager("Show All with labels");
roiManager("Measure");

saveAs("Results", "C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/Results.csv");
run("Close");

close();
close();
selectWindow("img064c.jpg");
close();
selectWindow("a");
close();

I can convert partially it to Python. (the following code)

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.plugin.filter import ParticleAnalyzer, BackgroundSubtracter, EDM
from ij.measure import ResultsTable
from ij.io import DirectoryChooser
import os


imp = IJ.openImage("C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064c.jpg")
IJ.run(imp, "RGB to CIELAB", "")
IJ.run("Stack to Images", "")
imp2 = IJ.openImage("C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064cROI.zip")
RM = RoiManager()      
rm = RM.getRoiManager()  
rm.runCommand(imp2,"Show All")


selectWindow("L")
rm.runCommand(imp2,"Measure")
rm.runCommand(imp2,"Show All")

selectWindow("b")
rm.runCommand(imp2,"Measure")
rm.runCommand(imp2,"Show All")

selectWindow("a")
rm.runCommand(imp2,"Measure")
rm.runCommand(imp2,"Show All")

What is problem in solveing this

The code which i write in Python is not work perfectly. This cord dose work only
“Open the file” ,“RGB to CIELAB”, “Stack to Images” and “Open Roi Manager”.

The code dose not work “Select window” , “Measure”,“Show All”.

Please tell me how to work <selectWindow(“foo”);> , <roiManager(“Show All”);> ,
<roiManager(“Show All with labels”);> , <roiManager(“Measure”);> in python.

Hello Dan -

No worries. I can understand your English fine.

As you’ve seen, when using jython you can run some commands
using the the IJ.run() method. You can also do things by using
Fiji / Image’s java api directly (and not necessarily trying to imitate
how the code would look in IJM).

The problem is that IJM has some things “built in” in a way that
doesn’t translate simply or directly to the java api.

In general, to use jython (and the other java-aware scripting
languages such as javascript) effectively, you will have to explore
the java api.

First start with the ij.IJ class that contains a lot of static
convenience methods. But that only does some things. You will
generally have to do some of your jython scripting with the java
classes that actually do the work.

Here is a complete, runnable jython script that illustrates how to
do some of the things you were asking about. Note, I’ve purposely
done the same thing in more than one way sometimes to show
how to use the lower-level java classes as well as the higher-level
convenience methods.

# use IJ static convenience functions
from ij import IJ

imp1 = IJ.createImage ('image1', '8-bit ramp', 256, 256, 1)
imp1.show()
imp2 = IJ.createImage ('image2', '8-bit ramp', 256, 256, 1)
imp2.show()
imp3 = IJ.createImage ('image3', '8-bit ramp', 256, 256, 1)
imp3.show()

IJ.selectWindow ('image1')
IJ.makeRectangle (20, 20, 30, 30)

# use WindowManager to manipulate windows
from ij import WindowManager

WindowManager.setCurrentWindow (WindowManager.getWindow ('image2'))
IJ.makeOval (60, 60, 40, 40)
WindowManager.toFront (WindowManager.getWindow ('image2'))

# use (Polygon)Roi class to create ROI
from ij.gui import Roi
from ij.gui import PolygonRoi

roi = PolygonRoi ([135, 160, 110], [110, 160, 160], Roi.POLYGON)
imp3.setRoi (roi)

# pause script so images can be looked at
from ij.gui import WaitForUserDialog
WaitForUserDialog ('Inspect images', 'show last two ROIs on image1').show()

# use RoiManager to display second and third ROIs on image1
from ij.plugin.frame import RoiManager

rm = RoiManager()
rm.addRoi (roi)  # add ROI (not necesarrily from a image)
rm.runCommand (imp2, 'Add')  # add ROI from an image
rm.runCommand (imp1, 'Show All')

Feel free to ask further questions as they arise.

Thanks, mm

3 Likes

big thanks to mountain_man. I got some real examples in Jython.

I realize that how to use the command “selectwindow” and i need to separate variable each of Photos.

But, i have some problem in implement program. My code separates The picture to three kind of pictures L, a, b (The follow picture,code) .

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.plugin.filter import ParticleAnalyzer, BackgroundSubtracter, EDM
from ij.measure import ResultsTable
from ij.io import DirectoryChooser
import os



os.chdir("C:\Users\For  Programming\Documents\Python Scripts\ImageJtest")

#IJ.openImageの時は <"C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/> の部分は指定してあげないと無理ぽい
#定期的にcloseしないと、メモリが爆発しそう。


imp = IJ.openImage("C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064c.jpg")
IJ.run(imp, "RGB to CIELAB", "")
StackImage = IJ.run("Stack to Images", "")
imp2 = IJ.openImage("C:/Users/For  Programming/Documents/Python Scripts/ImageJtest/img064cROI.zip")
RM = RoiManager()      
rm = RM.getRoiManager()  

type(StackImage)

IJ.selectWindow("L")
rm.runCommand(StackImage,"Measure")
rm.runCommand(StackImage,"Show All")

IJ.selectWindow("a")
rm.runCommand(StackImage,"Measure")
rm.runCommand(StackImage,"Show All")


IJ.selectWindow("b")
rm.runCommand(StackImage,"Measure")
rm.runCommand(StackImage,"Show All")

but dose not work “selectwindow” and "rm.runCommand (foo,“Measure”) , (foo,“Show All”)

i predict this problem because i don’t define the variables on each photos.
but i don’t know how to define the variables on each photos(L,a,b)

Hello Dan -

Your problem is that StackImage is None (null in java).

IJ.run() is a (static) method of type void, that is, it doesn’t
return anything. So StackImage doesn’t refer to the images
produced by “Stack to Images”. (It doesn’t refer to anything
at all, and when you try to use it in
rm.runCommand (StackImage, 'Measure') you will get
a NullPointerException.)

There are two versions of RoiManager.runCommand(): one
takes only one argument, the command String, and operates
on the “active” (“selected”) image; the other acts on whatever
image is passed in as its ImagePlus argument.

This is illustrated by the following jython script:

from ij import IJ
from ij.gui import PolygonRoi
from ij.gui import Roi
from ij.plugin.frame import RoiManager

# "Mean" measurement for 'black' will be 0.000
impBlack = IJ.createImage ('black', '8-bit black', 256, 256, 1)
impBlack.show()
# "Mean" measurement for 'white' will be 255.000
impWhite = IJ.createImage ('white', '8-bit white', 256, 256, 1)
impWhite.show()

# convert 'Clown' sample image to L, a, b images
IJ.run ('Clown (14K)')
impClown = IJ.getImage()
IJ.run (impClown, 'RGB to CIELAB', '')
StackImage = IJ.run ('Stack to Images', '')  # StackImage is None
print  'StackImage =', StackImage   # prints "None"

rm = RoiManager()
rm.addRoi (PolygonRoi ([135, 160, 110], [110, 160, 160], Roi.POLYGON))

rm.runCommand ('Measure')                 # measures image 'b'
rm.runCommand (impBlack, 'Measure')       # measures image 'black'
IJ.selectWindow ('white')
rm.runCommand ('Measure')                 # measures image 'white'

# rm.runCommand (StackImage, 'Measure')   # throws NullPointerException

Note that

RM = RoiManager()
rm = RM.getRoiManager()

is unnecessary;

rm = RoiManager()

suffices.

Thanks, mm