Analysis with ImageJ and Visualization in the Jupyter Notebook



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

Can you successfully import imglyb (without importing any imagej) in that conda environment?


No, I can’t. It raises the same error.

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

I tried reinstalling from the terminal but still doesn’t work.

activate imglyb
(imglyb) C:\Users\User>conda install -c hanslovsky imglib2-imglyb   


The issue is with the conda build of PyJNIus. I tried on a Windows laptop and I got the same error. I tried to build the conda package for PyJNIus again on Windows but it failed with the same error. Unfortunately, my windows knowledge and debugging skill is very limited. It already took me hours to get conda to (try and) build, so I cannot help here, unfortunately.


Until we find maintainers for the conda packages for Windows, I will not be able to provide PyJNIus on conda, unfortunately. The only other option would be to try and build PyJNIus yourself. My hope is that you could run imglyb then.


As stated in the other thread (Imglyb and PyJNIus conda package maintainers needed for Windows and OSX) it seems like I was able to fix the build issues on windows with the help of @jakirkham and @jjahanip
PyJNIus (and eventually imglyb) will be available on conda-forge (instead of my personal channel):
@malj390 I will also update my anaconda channel tonight (when I have access to a Windows machine) with an updated Windows conda package, which should hopefully fix your particular issue for now.


@malj390 I just updated the imglib2-imglyb and imglyb-examples packages on my conda channel hanslovsky. I confirmed on a Windows 10 machine that it works. Please let me know if there are any issues.