Failing to connect to ImageJ v1.53e (or A) from Python

Hi,

I am trying to access some ImageJ functionality from within a python script. my goal, in the end, is being able to call an imagej script that utilizes a plugin to some end goal.
But for now I am failing to create the bridge to a pre-installed ImageJ.

I have installed PyImageJ 1.0.0 via PIP (and also in PyCharm) and I am running on windows-10 with Python 3.8. My ImageJ version is 1.53e.
I tried directing %JAVA_HOME% to both jre1.8.0_261 and jdk-15.0.1

I try running this code in Python:

import imagej
import scyjava

scyjava.config.add_option('-Xmx6g')
ij = imagej.init('C:\\Programs\\ImageJ')
ij.getVersion()

I get the following error:

    ij = imagej.init('C:\\Programs\\ImageJ')
  File "C:\Projects\Code_GIT\Neptune\resolutiontargetanalyser\ResolutionTargetAnalyzer\ImagingTool_RTA_V7\source\venv\lib\site-packages\imagej\__init__.py", line 167, in init
    ImageJ = sj.jimport('net.imagej.ImageJ')
  File "C:\Projects\Code_GIT\Neptune\resolutiontargetanalyser\ResolutionTargetAnalyzer\ImagingTool_RTA_V7\source\venv\lib\site-packages\scyjava\__init__.py", line 151, in jimport
    return jpype.JClass(class_name)
  File "C:\Projects\Code_GIT\Neptune\resolutiontargetanalyser\ResolutionTargetAnalyzer\ImagingTool_RTA_V7\source\venv\lib\site-packages\jpype\_jclass.py", line 99, in __new__
    return _jpype._getClass(jc)
TypeError: Class net.imagej.ImageJ is not found

Can anyone assume what is the problem? I am at a loss.

It seems like there might be an issue with the path to your ImageJ installation. You might want to check:

  • Is C:\Programs\ImageJ\ a directory?
  • Does it contain a subfolder /jars/?
  • Does this subfolder contain a file imagej-2.1.0.jar or similar?

Note that you’ll need an ImageJ2 instance (so e.g. a #fiji installation) in order to work with #pyimagej. A plain ImageJ1 won’t work.

EDIT:
The error handling also needs to be improved to provide better information in case the required jar files are not found. There is a related issue on GitHub tracking this:

Thanks.
As I am running ImageJ1, i guess this is the problem. I will test with ImageJ2 then.

in answer to your questions:

  1. C:\Programs\ImageJ\ is indeed a directory
  2. It does not contain \jars (it contains plugins\jars though)
  3. The folder contains ij.jar

Will keep you updated once I check ImageJ2.

Thanks

Checked with Imagej2/FIJI and it works!

Thanks!

1 Like