ImageJ and Python integration

I have a code in python 3.7 and i need to import it as a plugin in Imagej. What should i do?

Morning :slight_smile:have a look at this thread for a start:

There’s quite a bit of discussion here on the forum, did you search on ‘python’?


You can have a look, A bridge connect imglib and numpy.

And you can try or contribute to, A framework like ImageJ in Python. There is not many plugins as rich as ImageJ/Fiji now. But you can use numpy/skimage/opencv directly.

And in the future, I would do some work that try to access scijava in ImagePy,

1 Like

Thank you Danielle_Z. I did look there.
Executing Imagej package functions always ends up raising exceptions like

"raise ImportError(‘Unable to import scyjava dependency jnius.’)
ImportError: Unable to import scyjava dependency jnius."

Is there a problem with jnius module?

1 Like

I’m afraid l can’t help. Perhaps a search on that module and python could point you to some folk who you could address specifically.

Myself I’m keen to start combining ImageJ and python too, but I’m rather green in that area right now.


Are you using conda?

Can you be more specific about the steps you took to get set up?
(I presume you’re trying pyimagej but its not clear…)


No i am using Python idle, though i think i will get the same error while using conda too.
I downloaded the imagej package in python (yes it is pyimagej).
It had certain prerequisites like scyjava and imglyb and jnius. So i also downloaded both the packages too.

this is the exact error i get when i run a function in Imagej package. Is there anything i could do?

1 Like


That’s helpful. Looking at this part of the error message:

PYJNIUS_JAR environment variable not defined

it looks like you’re missing an environment variable which tells imglyb where to find the pyjnius jar.

Make sure you followed all the steps for installing imglyb
Especially this part which shows you how to set the environment variables imglyb needs.

:: Set the appropriate environment variables:
SET "PYJNIUS_JAR=path\to\pyjnius\pyjnius.jar"
SET "JAVA_HOME=path\to\jdk"



I feel the quickest and simplest (80-20) way to get something going is to just use subprocess in Python and execute the appropriate command line.

First make your macro and save as macro.ijm. You can build a template but just doing Plugins > Macros > Record in Fiji and copying out what you need from your image operations. Put that in a file or construct a function which generates the macro just as you would writing to a text file (useful for iterating through different datasets, for example). Then just run the following:

import subprocess
fiji_path = "/path/to/"
macro_file = "macro.ijm"
fiji_cmd = " ".join([fiji_path, "--headless", "-macro", macro_file]), shell=True)

The only thing to be aware of is the use of double or single quotes. As an example, say you want to subtract background for a list of files in a folder which have a certain string:

def generate_macro(input_dir,output_dir,unique_identifier):  
    params_list = [
            "list = getFileList('%s');" % input_dir,
            "for (i = 0; i < list.length; i++) {",
            "    idx = indexOf( list[i], '%s' );" % unique_identifier,
            "    if ( idx >= 0 ){",
            "        print(list[i]);",
            "        open('%s' + list[i]);"% input_dir,
            "        run('Subtract Background...', 'rolling=50');",
            "        saveAs('Tiff','%s' + list[i]);" % output_dir,
            "        close();",
            "        }",
    macro_params = "\n".join(params_list)
    return macro_params

Then you just write this to file:

macro_params = generate_macro(*args)
f = open("macro.ijm")

Now you can call it from subprocess as above. Good luck.

1 Like

I had the same problem

my error information as following:

Unable to import scyjava: pyjnius JAR not found.
Traceback (most recent call last):
  File "", line 432, in slotOpenImageJ
  File "site-packages\imagej\", line 126, in init
  File "c:\users\fengzhiheng\anaconda3\envs\pyimagej\lib\site-packages\PyInstaller\loader\", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\imglyb\", line 38, in <module>
  File "site-packages\imglyb\", line 33, in _init_jvm_options
  File "c:\users\fengzhiheng\anaconda3\envs\pyimagej\lib\site-packages\PyInstaller\loader\", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\scyjava\", line 142, in <module>
ImportError: Unable to import scyjava dependency jnius.