Python wrapper for ImageJ
pyimagej provides a set of
wrapper functions for integration between ImageJ and Python.
It also provides a high-level entry point
imagej.IJ for invoking
ImageJ Server APIs;
see "ImageJ Server" below for details.
The recommended way to install
pyimagej is with Conda:
conda config --add channels conda-forge conda install pyimagej openjdk=8
The above installs it with OpenJDK 8; if you leave off the
openjdk=8 it will install OpenJDK 11 by default, which should also work, but is less well tested and may have more rough edges.
Creating the ImageJ gateway
Newest available version
If you want to launch the newest available release version of ImageJ:
import imagej ij = imagej.init()
This invocation will automatically download and cache the newest release of net.imagej:imagej.
Explicitly specified version
You can specify a particular version, to facilitate reproducibility:
import imagej ij = imagej.init('2.0.0-rc-68') ij.getVersion()
With graphical capabilities
If you want to have support for the graphical user interface:
import imagej ij = imagej.init(headless=False) ij.ui().showUI()
Note there are issues with Java AWT via Python on macOS; see this article for a workaround.
Including ImageJ 1.x support
import imagej ij = imagej.init('net.imagej:imagej+net.imagej:imagej-legacy')
Including Fiji plugins
By default, the ImageJ gateway will include base ImageJ2 functionality only, without additional plugins such as those that ship with the Fiji distribution of ImageJ.
You can create an ImageJ gateway including Fiji plugins as follows:
import imagej ij = imagej.init('sc.fiji:fiji')
From a local installation
import imagej ij = imagej.init('/Applications/Fiji.app')
/Applications/Fiji.app with the actual location of your installation.
Using the ImageJ gateway
Once you have your ImageJ gateway, you can start using it. Here is an example:
# Import an image with scikit-image. import skimage from skimage import io # NB: Blood vessel image from: https://www.fi.edu/heart/blood-vessels img = io.imread('https://www.fi.edu/sites/fi.live.franklinds.webair.com/files/styles/featured_large/public/General_EduRes_Heart_BloodVessels_0.jpg') import numpy as np img = np.mean(img, axis=2) # Invoke ImageJ's Frangi vesselness op. vessels = np.zeros(img.shape, dtype=img.dtype) ij.op().filter().frangiVesselness(ij.py.to_java(vessels), ij.py.to_java(img), [1, 1], 20)
test/test_imagej.py for other examples of usage.
The imagej.server module has its own requirements:
requestsis required to communicate with the ImageJ server.
pillowis required for the
imagej.server.IJ.show()function. In addition,
xvmust be available to view the image.
There is a short usage example here.