Analysis with ImageJ and Visualization in the Jupyter Notebook

Tags: #<Tag:0x00007fa304c42dc8> #<Tag:0x00007fa304c428c8> #<Tag:0x00007fa304c42788> #<Tag:0x00007fa304c42648> #<Tag:0x00007fa304c42508> #<Tag:0x00007fa304c423c8>


We can now easily run ImageJ in the Jupyter Notebook and interactively visualize the result!

Here is the notebook that contains installation instructions. It demonstrates interactive visualization with itkwidgets.

This follows a long effort by @hanslovsky and @ctrueden. The usage of ImageJ with the Python ecosystem became much more streamlined when Curtis recently released v0.2.0 of the imagej Python package.

Thanks to the Berkeley Institute of Data Science and the Chan Zuckerberg Initiative, who supported progress on this during a hackathon last week.


Thank you @thewtex for all your efforts on this! Really exciting to see these tools coming together!

To clarify for those here familiar with ImageJ but not familiar with ITK and VTK: the linked notebook illustrates integration of the Insight Toolkit (ITK) and VTK.js with ImageJ via numpy arrays. These three ecosystems together are an extremely powerful combination.

@thewtex I did not see a similar announcement on the ITK forum. Did you mean to make one there as well?


Yes, I will announce on the ITK forum as well, soon, after a few more improvements.


@thewtex, @hanslovsky, @ctrueden

I just tried to install and run the notebooks, but am having trouble with the imglyb environment variables. When I try to import imglyb I get

Path to pyjnius.jar not defined! Use environment variable PYJNIUS_JAR to define it.

I am installing imglyb using

conda install -c hanslovsky imglib2-imglyb

So my environment variables should be automatically set up.

In the documentation it says “Re-activate the environment after installation to correctly set the environment variables.” What does it mean to re-activate the environment?? When I list my environments after installing imglyb I don’t have any new environments or anything.

conda env list
conda environments:

base                  *  /home/bnorthan/anaconda3

Any help you can give would be appreciated. Thanks


@bnorthan The environment setup is now handled by the imagej Python package. See:

i.e. also install the imagej package,

python -m pip install imagej

And initialize by pointing to the path of a Fiji installation,

import imagej
ij = imagej.init(fiji_path)

where fiji_path is the path to


Thanks @thewtex

It seems you have to initialize imagej before importing imglyb. That’s the part I didn’t clue into. I had done all my imports first.


Ah, tricky.

We could have imglyb check that imagej has been initialized before it is used.


First of all thanks @thewtex for your efforts, this looks great!

@bnorthan imglyb expects certain environment variables in your environment to be set, one of them PYJNIUS_JAR. I assume that the variables do not get set on install but only when you activate the environment. Re-activating:

conda install -c hanslovsky imglib2-imglyb
source deactivate
source activate /* conda environment */

I am no conda expert, though.


We could have imglyb check that imagej has been initialized before it is used.

That is not possible, unfortunately. ImageJ looks like a higher level wrapper around imglyb that sets up the environment appropriately before starting imglyb. imglyb is a lower compatibility layer between numpy and imglib2 and as such should not be aware of ImageJ.

There are efforts to make setting up the imglyb environment easier with run-time class loading and dependency resolution but I do not know about the current progress. Maybe @ctrueden can comment.


Hi @thewtex, @ctrueden, @hanslovsky

This notebook is my l attempt to run the YacuDecu GPU Deconvolution wrapper Op I’ve been working on in a notebook.

It works, but I ran into a few minor issues a long the way, which I’ve commented on in the notebook.

The biggest issue (see Cell 10) is that I wasn’t able to get ij.op().run(...) to work. Some googling indicates other people have trouble calling functions with variable number of inputs with pyjnius. Although perhaps I messed up the call in some other way.

Any ideas??


I had similar issues with calling overloaded Java methods through PyJNIus. Other than writing helper methods that reduce this “ambiguity” (quotations mark because it is no real ambiguity), I have not found any solution yet.


Yes, agreed. We do not want imglyb to have a package dependency on imagej, for example. But, it would help newcomers understand what is happening if there was a check that ensures PYJNIUS_JAR is set before it is used. And, if it is not set, then an informative error is thrown that 1) explains the issue and 2) suggests the most common approach to resolve it, whether that is imagej.init, scyjava, …

That is awesome, @bnorthan! We are approaching a beautiful place, which @ctrueden described, where we can mix open source image analysis technologies, whether they are Java-based, Python-based, C++ based, or JavaScript-based. The true power of open source is unleashed when efforts are combined. Now, I have access to an super-speedy deconvolution implementation :sunny: :fireworks:.

I ran into these issues, also.


It already throws an error that explains the issue:

Path to pyjnius.jar not defined! Use environment variable PYJNIUS_JAR to define it.

I could add a statement like

If you are using a framework that sets the environment for you (e.g. imagej) make sure to import and set up that framework before importing imglyb.

Frameworks that use imglyb (and initialize the environment) should explicitly state that the framework needs to be set up before any call to import imglyb and demonstrate that in their usage examples, and make sure those usage examples work (imagej usage example does not import imglyb on PyPI -> Use bold letters for that so everybody can see it.

Or, even better, add the imglyb package as a member to your own namespace, and use it consistently in your examples, so people will never have to run import imglyb.
For imagej, this might look like this:

import imagej
ij = imagej.init('/Applications/')
imglyb = ij.imglyb


Good idea – that improvement should help many folks get started.

Cool! ij currently does not have the .imglyb attribute. @ctrueden do you think we should add this?



First of all thank to @thewtex for this useful integration. I have been looking for something like this long time ago.
Sorry if the question is too obvious or it shouldn’t be place here, but it is related. I’m having problems with this first part.

I’m working on Windows with an environment in Anaconda2

!conda install --yes --prefix {sys.prefix} -c hanslovsky imglib2-imglyb
!conda install --yes --prefix {sys.prefix} requests
!{sys.executable} -m pip install imagej

Getting this error, however it says that All requested packages already installed.

Solving environment: ...working... failed

UnsatisfiableError: The following specifications were found to be in conflict:
  - imglib2-imglyb
  - sphinx==1.6.3=py35heeac824_0
Use "conda info <package>" to see the dependencies for each package.

Solving environment: ...working... done

# All requested packages already installed.

Requirement already satisfied: imagej in c:\users\User\anaconda2\envs\py35\lib\site-packages (0.2.0)

After that I load the modules and initialize Imagej (which I store in Dropbox) and I get this error:

import shutil
import os
import requests
import itk
import numpy as np
from itkwidgets import view

fiji_path = 'D:/Dropbox/fiji-win64/'

import imagej
ImportError: No module named 'jnius_config'
import imglyb
ImportError: No module named 'imglyb'

I tried to install it into the environment from the terminal but I get the same error for imglyb and Imagej seems to be installed.

(py35) C:\Users\User>pip install imagej                                                                    Requirement already satisfied: imagej in c:\users\User\anaconda2\envs\py35\lib\site-packages (0.2.0) 



Can you try installing imglyb with python 3.6?
Example command:

conda create -n imglyb -c hanslovsky python=3.6 imglib2-imglyb <any-other-dependencies>

I did not upload any (Windows) packages for python 3.5:

Update: Packages for Python 3.5 and Python 2 for other architectures (osx, Linux) are somewhat obsolete and I do not have plans to update packages for Python <= 3.5 for any architecture.


Thanks @hanslovsky, that worked!

But now I’m having another issue.

fiji_path = 'D:/Dropbox/fiji-win64/'
import imagej
ij = imagej.init(fiji_path)
import imglyb
please set the java enviroment manully by call set_java_env() command
Java can not be found, it might not be correctly installed.
Added 390 JARs to the Java classpath.

So I set it,

imagej.set_java_env("C:\\Program Files\\Java\\jre1.8.0_162\\lib\\rt.jar")

But still raises the same problem.
I don’t know much about Java so maybe I’m choosing the wrong java environment.

Thank you in advance!


I am not too familiar with Python path separator conventions but the forward slashes in fiji_path might be a problem. You can try to use this instead:

fiji_path = os.path.normpath('D:/Dropbox/fiji-win64/')

Also pinging @ctrueden for this question.


Normally to take paths in Python from Ubuntu is with forward slashes “/” and from windows can be either forward or 2 backwards “\\”. I tried with:

fiji_path = 'D:/Dropbox/fiji-win64/'
fiji_path = 'D:\\Dropbox\\fiji-win64\\\\'
fiji_path = os.path.normpath('D:/Dropbox/fiji-win64/')
fiji_path = os.path.normpath('D:\\Dropbox\\fiji-win64\\\\')

But any of them works.


Using os.path.join is a good practice that can help make the commands cross platform, work around string escape characters, etc.


Thanks @thewtex for os.path.join command.

However, I’m still having an issue with Java recognition.

fiji_path = os.path.join('D:/Dropbox/fiji-win64/')

import imagej
fiji_java_env = os.path.join("D:/Dropbox/fiji-win64/") # Java from ImageJ
fiji_java_env2 = os.path.join("C:/Program Files/Java/jre1.8.0_162") # Java from OS

imagej.set_java_env(fiji_java_env) # or fiji_java_env2

ij = imagej.init(fiji_path)
please set the java enviroment manully by call set_java_env() command
Java can not be found, it might not be correctly installed.
Added 390 JARs to the Java classpath.
ImportError                               Traceback (most recent call last)
<ipython-input-33-73c7f0b08f53> in <module>()
      9 imagej.set_java_env(fiji_java_env2)
---> 11 ij = imagej.init(fiji_path)
     12 import imglyb

~\Anaconda2\envs\imglyb\lib\site-packages\imagej\ in init(ij_dir)
    202         return
    203     print("Added " + str(num_jars + 1) + " JARs to the Java classpath.")
--> 204     import imglyb
    205     from jnius import autoclass
    206     ImageJ = autoclass('net.imagej.ImageJ')

~\Anaconda2\envs\imglyb\lib\site-packages\imglyb\ in <module>()
---> 50 from .util import \
     51      to_imglib, \
     52      to_imglib_argb, \

~\Anaconda2\envs\imglyb\lib\site-packages\imglyb\ in <module>()
      5 from collections import defaultdict
----> 7 from jnius import autoclass, PythonJavaClass, java_method
      9 import numpy as np

~\Anaconda2\envs\imglyb\lib\site-packages\pyjnius-1.1.2.dev0-py3.6-win-amd64.egg\jnius\ in <module>()
     10 __version__ = '1.1.2-dev'
---> 12 from .jnius import *  # noqa
     13 from .reflect import *  # noqa

ImportError: DLL load failed: The specified module could not be found.

Thanks in advance!

Imglyb and PyJNIus conda package maintainers needed for Windows and OSX