Python wrapper for ImageJ2 that provides seamless integration between ImageJ and Python scientific computing ecosystems
—
Core functionality for initializing and configuring the ImageJ2 environment. The gateway serves as the primary interface to ImageJ2 services and provides access to all ImageJ functionality from Python.
Initialize ImageJ2 environment with configurable options for deployment scenarios.
def init(
ij_dir_or_version_or_endpoint=None,
mode: Union[Mode, str] = Mode.HEADLESS,
add_legacy: bool = True,
headless=None
) -> "ImageJ2Gateway":
"""
Initialize an ImageJ2 environment.
Args:
ij_dir_or_version_or_endpoint: Path to local ImageJ2 installation,
version string (e.g. "2.3.0"), endpoint (e.g. "sc.fiji:fiji"),
or list of Maven artifacts. Default uses latest ImageJ2.
mode: Environment behavior mode - HEADLESS, GUI, or INTERACTIVE
add_legacy: Whether to include original ImageJ (ij.*) support
headless: Deprecated - use mode parameter instead
Returns:
ImageJ2 gateway instance providing access to all ImageJ functionality
"""Usage Examples:
# Use latest ImageJ2 in headless mode (default)
ij = imagej.init()
# Use specific version
ij = imagej.init("2.3.0")
# Use Fiji distribution
ij = imagej.init("sc.fiji:fiji")
# Use local installation
ij = imagej.init("/Applications/Fiji.app")
# Initialize with GUI mode
ij = imagej.init(mode=imagej.Mode.GUI)
# Use custom Maven artifacts
ij = imagej.init([
"net.imagej:imagej:2.3.0",
"net.imagej:imagej-legacy",
"net.preibisch:BigStitcher"
])Control how ImageJ2 operates in different deployment scenarios.
class Mode(Enum):
"""ImageJ2 environment execution modes."""
GUI = "gui" # Start with GUI, display automatically, blocks execution
HEADLESS = "headless" # No GUI, suitable for server/library usage
INTERACTIVE = "interactive" # GUI support available but not displayed automatically
def __eq__(self, other): ...Mode Characteristics:
ij.ui().showUI(), works in Jupyter/IPython environmentsRegister functions to execute immediately after ImageJ2 initialization.
def when_imagej_starts(f) -> None:
"""
Register a function to be called immediately after ImageJ2 starts.
Args:
f: Single-argument function that receives the ImageJ2 Gateway
"""Usage Example:
def configure_imagej(ij):
"""Custom configuration applied after ImageJ2 starts."""
ij.ui().showUI()
print(f"ImageJ2 initialized with {ij.getVersion()}")
# Register callback before initialization
imagej.when_imagej_starts(configure_imagej)
# Initialize - callback will be executed
ij = imagej.init(mode=imagej.Mode.INTERACTIVE)Entry point for launching ImageJ from command line.
def imagej_main():
"""Entry point for the 'imagej' console command."""This function is automatically called when using the imagej command installed with the package:
# Launch ImageJ2 GUI
imagej
# Launch in headless mode
imagej --headlessOnce initialized, the ImageJ2 gateway provides access to ImageJ services:
ij.py - Python convenience methods (ImageJPython class)ij.legacy - Original ImageJ compatibility layerij.op() - ImageJ2 Ops framework for image processingij.io() - Image I/O servicesij.ui() - User interface servicesij.convert() - Data conversion servicesij.dataset() - Dataset creation and managementij.script() - Script execution servicesCommon initialization issues:
scyjava.config.add_option("-Xmx8g")add_legacy=False if experiencing ImageJ 1.x compatibility issuesInstall with Tessl CLI
npx tessl i tessl/pypi-pyimagej