This notebook covers how to use ImageJ as a library from Python. A major advantage of this approach is the ability to combine ImageJ with other tools available from the Python software ecosystem, including NumPy, SciPy, scikit-image, CellProfiler, OpenCV, ITK and more.
This notebook assumes familiarity with the ImageJ API. Detailed tutorials in that regard can be found in the other notebooks.
PyImageJ is built to provide easy access to key ImageJ resources. We call these collective methods "conveience methods". These methods are attached toij.py
after initializing ImageJ. Here's a quick list of some of the more useful methods and additional information.
ij.py. |
function | more information |
---|---|---|
show |
Show an image | 2-Opening-and-Displaying-Images |
to_java |
Convert data from Python to Java | 3-Sending-Data-to-Java |
from_java |
Convert data from Java to Python | 4-Retrieving-Data-from-Java |
run_macro |
Run an original ImageJ macro | 7-Running-Macros-Scripts-and-Plugins |
run_script |
Run an ImageJ script (supported languages) | 7-Running-Macros-Scripts-and-Plugins |
run_plugin |
Run a plugin | 7-Running-Macros-Scripts-and-Plugins |
initialize_numpy_image |
Create a new numpy image in the same shape as input image | 6-Working-with-Images |
sync_image |
Syncronize data between ImageJ and ImageJ2 data structures | -- |
active_image_plus |
Get the ImagePlus from the current window |
-- |
active_xarray |
Convert current window into an xarray.DataArray |
-- |
active_dataset |
Get the active Dataset from the Dataset service |
-- |
There are other convenience methods that are attached to ij.py
. After initializing ImageJ you can explore ij.py
's methods with dir
.
When the original ImageJ is available (i.e. the legacy layer is active) IJ
, WindowManager
, ResultsTable
and RoiManager
are accessible directly from the initialized ij
object.
import imagej
# initialize imagej
ij = imagej.init()
print(f"ImageJ version: {ij.getVersion()}")
# first check if the legacy layer is active
print(f"Legacy layer active: {ij.legacy.isActive()}")
log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. WARNING:root:Operating in headless mode - the original ImageJ will have limited functionality.
ImageJ version: 2.3.0/1.53f Legacy layer active: True
# demonstrate access to classes
print(ij.IJ)
print(ij.ResultsTable)
print(ij.RoiManager)
print(ij.WindowManager)
WARNING:root:Operating in headless mode - the IJ class will not be fully functional. WARNING:root:Operating in headless mode - the ResultsTable class will not be fully functional. WARNING:root:Operating in headless mode - the RoiManager class will not be fully functional. WARNING:root:Operating in headless mode - the WindowManager class will not be fully functional.
<java class 'ij.IJ'> <java class 'ij.measure.ResultsTable'> <java class 'ij.plugin.frame.RoiManager'> <java class 'ij.WindowManager'>
Note the warnings! We're currently in headless mode. The many legacy ImageJ functions operate limitedly or not at all in headless mode. For example the RoiManager
is not functional in a true headless enviornment.