Running an ImageJ macro with python


I am trying to run a macro with Python to automate it.
I have tried this one but it is not working.


And this error appear


Have you solution to resolve it ?

Thank you

Hello Tayil -

I don’t know how to do this with, but if you
are willing to use a jython (python) script running in Fiji / ImageJ’s
script interpreter, you can run your macro as follows:

from ij import IJ

macrotest = """
run ("Blobs");
run ("Gaussian Blur...", "sigma=2");

IJ.runMacro (macrotest)

IJ.saveAs ('PNG', 'blurred_blobs.png')

For completeness, here is the resulting image:


Thanks, mm

From your code example, it appears you use #pyimagej, so I added this tag to your forum post.

1 Like

Thank you for your response, but I need to use Pyimagej with Python. Indeed, I will add OpenCV to the same code.

As a side note, you can also use opencv within Fiji, using IJ-OpenCV
It’s a bit more involved that the python version but it works well.

1 Like

Hi @tayil,

have you installed pyimagej following the instructions, i.e. using a specific conda environment created with:

conda create -n pyimagej -c conda-forge pyimagej openjdk=8

If not, you should try to do that.

I had never seen your particular error, but there are two common problems that I have recurrently encountered when installing pyimagej, so you can try to see if the two following points help:

  1. The most recent version of pyjnius that gets installed is 1.2.1. However it causes some problems and it helps to use an earlier version (see Pyimagej failing with "'net.imagej.ops.OpService' object has no attribute 'run'"). Activate your conda environment and use:

    pip install pyjnius==1.2.0

    You might get an error related to Cython. If you do, just install it with pip install Cython

  2. The second thing, is using specific versions of Fiji. I don’t know why but not all of them seem to be compatible with pyimagej. On a fresh installation, this worked for me:

    ij = imagej.init('sc.fiji:fiji:2.0.0-pre-10')

I hope that helps!