Hello, I need to use Python to do some of my analysis work since ImageJ is too limited with respect to some coding aspects, but I need ImageJ to do some image processing work since python is limited there! So I have some issues with “compatiiblity” of languages. For example:
how do I run commands like
using Python as the set language
It would be easiest if I can get all my code in one language tho that may not be possible, but any help would be appreciated to address these compatibility issues.
If you don’t need to use external python modules and you’re ok with working in python 2.7 try scripting in Jython.
Jython is an implementation of python language that runs in the java virtual machine.
Here is the link of imageJ scripting with Jython wiki:
Thank you Igor! I did read through that last night actually, and was a bit confused how to use ‘imp.blank’, ‘ic.blank’, and other commands that are not called in the format “IJ.blank”. Hopefully I will be able to figure it out
Edit: I figured out some aspects, but now when I run the following commands,
from ij.process import ImageProcessor as ip
I am getting this error:
TypeError: drawLine(): self arg can’t be coerced to ij.process.ImageProcessor
First, in general, you can run ImageJ menu commands by calling Ij.run (<command-name>, <option-string>). This can
be accessed in jython (python) running as a script in ImageJ as
shown in this sample:
from ij import IJ
IJ.run ('Image...', 'name=new_image type=RGB fill=Noise width=256 height=256 slices=1')
IJ.run ('Rename...', 'title=rgb_noise')
This mimics running the menu commands (and filling in the
File > New > Image... Image > Rename...
(You can open the script editor, File > New > Script...,
set the language to python, paste this script into it, and run it.)
Second, to run “commands” like “drawLine()” that aren’t directly
accessible from the ImageJ menu, you have to get your hands
on an instance of the appropriate class, and call the method in
question (or call a static method without a specific instance).
This jython script illustrates how to use the drawLine() method
of the ImageProcessor class to draw a line on an image:
from ij import IJ
imp = IJ.createImage ('new_image_b', 'rgb noise', 256, 256, 1)
ip = imp.getProcessor()
ip.drawLine (0, 0, 127, 127)
This script first calls the static createImage() method of the IJ class (so we have an image to draw on). IJ.createImage() returns an instance of the ImagePlus
class that we assign to the jython variable imp. Just for fun,
we then use the ImagePlus.setTitle() method to change
the image’s name (rather than calling IJ.run ('Rename...')).
ip is the ImagePlus's instance of an ImageProcessor.
We then call ImageProcessor.drawLine() to draw the line.
(We could have called imp.getProcessor().drawLine())
directly, without using the variable ip.)
Third, ImageJ is written in java, so (almost) everything it does is
done with java classes and methods. Jython is a python interpreter
written in java that has the following very useful feature: You can
access java classes and methods directly from your jython code
without having to write any special “bridge” code. So you can
access (essentially) all of ImageJ from jython.
My approach to scripting (and non-script programming, and also
using java as well as jython) is to figure out which ImageJ classes
do the work I want to do, and then use jython to instantiate those
classes and call the appropriate methods. For getting started
with ImageJ image processing, IJ (a utility class with a bunch
of static methods), ImagePlus (the class that holds the image,
stack, etc., that you want to work on with metadata and many
methods), and ImageProcessor (the class – plus methods –
that actually holds the image data) are the “big three” you want
to start with.
(Last, as Igor noted, if you use jython, you lose access to some
non-python python packages. In particular, you can’t use numpy
from jython. You are also stuck with python 2, so you can’t use
python 3 syntax or python 3 packages. For scripting, I like jython;
for more involved ImageJ programming, I prefer java.)