Can you run ImageJ plugins using ImageJ.py?

python

#1

Is there a way to call ImageJ plugins using the ImageJ.py API, or the ImageJ.py API just for Ops/ImageJ2?

If not, is there a way to call ImageJ plugins using python?

For background: I’m trying to use the grid/collection stitching plugin, but I couldn’t find a way to call it. IJ.find and IJ.run don’t work, and neither does ij.plugin.stitching or ij.plugin.stitching_grid.

I think the problem is that the class of IJ from imagej.init() is net.imagej.imageJ, and those commands are run from just net.imagej.


#2

Hello Michael -

I can’t answer your specific question about ImageJ.py as I’ve
never used it, but it is possible to run python (jython) scripts
inside of ImageJ. (See below.)

The following is a jython script that you can run from the ImageJ / Fiji
script editor (File > New > Script...):

from ij import IJ
IJ.run ('About ImageJ...')
from ij.plugin import AboutBox
ab = AboutBox()
ab.run ('')

Running this script will open two copies of ImageJ / Fiji’s About box.

First the script calls IJ.run() with the plugin’s menu entry as a string.
Then it instantiates the plugin directly and calls its run() method.

And here’s a jython script that uses IJ.run() to run the grid/collection
stitching plugin:

from ij import IJ
IJ.run ('Grid/Collection stitching')

(According to the documentation, jython has one big limitation in that
it can’t use JNI – so numpy, for example, is out – but is otherwise
regular python. Plus jython gives you have access to java classes,
including the classes that make up ImageJ, whether they’re plugins
or not.)

Thanks, mm


#3

Hi mountain_man,

Thank you for the thorough explanation - I’ll keep this in mind for future ImageJ coding. However, my code is growing into a full python module and would be unwieldy to implement in ImageJ, and it makes heavy use of numpy aside.

I use the grid/stitching algorithm in only one or two places in the pipeline, so it is my hope to be able to call it there. If not, I’ll have to come up with a workflow that involves switching to ImageJ and opening up the script.

Best,
Michael


#4

Depending on what you are trying to do, you could try using imglyb, examples, notebook. Running ImageJ1 plugins on numpy arrays will most likely not work (without copying the data first).

You can add imagej jars like this:

fiji_app_dir = os.path.expanduser( '~/local/Fiji.app' )

jars        = glob.glob( '{}/jars/*.jar'.format( fiji_app_dir ) )
plugins     = glob.glob( '{}/plugins/*.jar'.format( fiji_app_dir ) )
bio_formats = glob.glob( '{}/jars/bio-formats/*.jar'.format( fiji_app_dir ) )

# for jar in jars + plugins + bio_formats:
#     jnius_config.add_classpath( jar )
    
# alternatively set the 'CLASSPATH' environment variable :
os.environ[ 'CLASSPATH' ] = jnius_config.split_char.join( jars + plugins + bio_formats )