How to convert an image to CIELab stack and extract 2nd slice using Python Macro

I have the following image:

What I wanted to do is to convert it into CIELab stack and then extract 2nd slice (a*).

I have this code:


from ij import IJ
infile = "/Users/gundalav/Desktop/my_image.png"
imp = IJ.openImage(infile)

# Duplicate
imp2 = imp.duplicate()
imp2.show()
# IJ.run(imp2, "HSB Stack", "") # This is working
IJ.run(imp2, "Lab Stack", "") # But not this


imp2.setSlice(1)
IJ.run(imp2, "Delete Slice", "")
imp2.setSlice(2)
IJ.run(imp2, "Delete Slice", "")

The code failed to do what I intended. In particular this line IJ.run(imp2, "Lab Stack", "").
That line was obtained from Plugins> Macro > Records .

What’s the right way to do it?
Typically I’d execute using Terminal command line:

$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx mycode.py

Note that “HSB stack” works with the above Python macro.

Hi @Peverall_Dubois,
there seems to be some inconsistency here. HSB Stack creates a stack with the > as if it was a z-stack, although properties says it has 3 channels. Lab Stack has the C and properties also says channels. One way to work around this is by duplicating the channel you are interested in and closing the stack, instead of deleting the two other slices.

imp2 = IJ.getImage()
imp3 = Duplicator().run(imp2, 2, 2, 1, 1, 1, 1);
imp2.close();

Best,
Volker

Hi @volker

Thank you for your reply. I tried your code.

I tried this. It is working under FIJI script console after
I clicked “Run” button:

from ij import IJ
from ij.plugin import Duplicator
infile = "/Users/gundalav/Desktop/my_image.png"
imp = IJ.openImage(infile)

# Duplicate
imp2 = imp.duplicate()
imp2.show()

IJ.run(imp2, "Lab Stack", "") 
imp2 = IJ.getImage()
imp3 = Duplicator().run(imp2, 2, 2, 1, 1, 1, 1)
imp3.show()

But it isn’t working, when I tried to execute it with:

$ /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx mycode.py

Is there a way I can resolve the issue?

Hi @Peverall_Dubois,
this is not cpython but jython, which runs on the jvm. That is why you can import IJ and Duplicator.
You can run it from the command line via ImageJ, see:


Best,
Volker