Can someone please help me understand PyImageJ? I'm trying to run a plugin in a Python Script. If this is not feasible, is there another way?

Can someone please help me understand or point me to some resources on how to learn how to work with PyImageJ? I followed along with the ImageJ + Python kernel, but this only taught me so much.
I’m trying to run a plugin from ImageJ in a Python Script. I want the script to load two images, run the plugin (Rigid Registration), and return the original and transformed images. I think PyImageJ is the easiest way to do this, but is it even possible? Below is my code so far. I recorded the macro from ImageJ plugin to do this. If this is not feasible, is there another way? Can you please provide me some hints about how I would go about using Jython to accomplish this or Bioformats plugin please to do this. Thank you very very much.

Img1 = io.imread('https://acdbio.com/sites/default/files/styles/sample_image/public/M-IL-8-8-hr_0.jpg')

img2 = io.imread('https://acdbio.com/sites/default/files/styles/sample_image/public/M-IL-8-8-hr_0.jpg')

#take the average over the 3 color channels and display as greyscale

img1 = np.mean(img1,axis=2)

img2 = np.mean(img2,axis=2)

plugin = 'Rigid Registration'

args = { 

    'initialtransform': [],

    'n': 1,

    'tolerance': 1.000,

    'level': 4,

    'stoplevel': 2,

    'materialcenterandbbox': [],

    'level': [],

    'showtransformed': 1,

    'template': img1,

    'measure': 'Correlation'

    'img2'

    'transformed_1'

}

If you want to try Jython, here are my favorite resources to help you get started:

  • Kota Miura’s cookbook

  • Albert Cardona’s tutorial

  • The Jython Scripting page on imagej.net

  • Juan Nunez-Iglesias’ tutorial

  • The ImageJ API is essential to figure out the details of the ImageJ class structure to get the import statements and function calls right…

  • Script Parameters are our friend! Note that there is an issue with the @String parameter. You want something like this:
    @String(label="Image Directory", style="") img_dir

  • Set the macro recorder to Javascript or Beanshell to get a better representation. Note there are some calls that don’t reproduce well so the examples in the resources above really help…

Hope this helps…

Best regards,
John Minter

Thank you. What if I want to use tools like Numpy opencv skimage etc in the script? This isn’t possible in Jython right?

You are correct. You might look at some of the jupyter notebooks here.

John

I’ve made an example Jupyter notebook showing how to do this.

The only caveat is that it is (currently, as far as I’m aware) impossible to open a numpy array as a window in ImageJ. You need to save the numpy modified images to the file system first and open them in ImageJ. This is true for any plugin that runs off of Image1J windows.

You can transform the result into a numpy array as well though. The example shows you how to do this.

4 Likes

@mpinkert, please specify the (conda) enviroment you are using. I tried this on my mac using Curtis Reuden’s scijava conda environment and I get the annoying prompt to install the (useless for this) legacy java kernel. In theory, Curtis’ enviroment should be fine. Any idea what I am missing?

Thanks,
John

@mpinkert Do you mean something like:

ij.ui().showUI()
ij.ui().show(ij.py.to_java(my_numpy_image))

?

I believe that should work on Linux and Windows platforms. It currently does not work on macOS due to a threading issue. A workaround is known, but has not yet been integrated into pyimagej. See:

Or did you mean something else, @mpinkert?

@John_Minter This is a known issue with pyjnius, the library upon which pyimagej is built. See:

Sorry we haven’t found a robust way around it yet, but there is a workaround described there that you can use to avoid installing the old Apple Java 6.

2 Likes

Thanks for the explanation, Curtis. My Fiji java has this set by default (thanks to those of you who bundle it!). Even though the notebook sets the path to Fiji in the first chunk, conda does not seem to use that JVM. I tried looling for a Java info.plist in the conda environment and couldn’t fine it. I have a Win10x64 Parallels VM on the Mac, so I gave up and used that with success. Not elegant, but it worked :slight_smile:

@ctrueden that was exactly what I meant. Thanks for showing that. I’ve modified the notebook so that it uses this new method. It is now considerably simpler.

2 Likes

I have tried accessing the example notebook that you prepared but I could not find it. It would be awesome if you could provide th new location of it.

Hi Carlos,

Thanks for the catch. We moved the repository and forgot to update old links. Here is the new link.

2 Likes

Thanks @mpinkert for the new link. I edited your “solution” post above to reflect the new link. However, I first transformed it into a stable permalink by opening the link on GitHub and pressing the Y key to replace master with the current commit hash. This way, the link will never go dead. The downside is that any future updates to the notebook will not be reflected in the link. But that’s a small price to pay IMHO.

1 Like