Python wrapper for ImageJ2 that provides seamless integration between ImageJ and Python scientific computing ecosystems
—
Display and visualization functions with matplotlib backend support for showing images, managing active images, and synchronizing data between ImageJ and Python environments.
Display images using matplotlib backend with customizable visualization options.
def show(image, cmap=None):
"""
Display a 2D image using matplotlib.
Args:
image: Image data (Java or Python format) that can be converted to NumPy array
cmap: Matplotlib colormap name (e.g., "gray", "hot", "viridis")
Raises:
TypeError: If image is None
ImportError: If matplotlib is not available in headless environments
"""Usage Examples:
import numpy as np
# Display NumPy array
array = np.random.rand(256, 256)
ij.py.show(array, cmap="gray")
# Display Java image with color mapping
dataset = ij.io().open("path/to/image.tif")
ij.py.show(dataset, cmap="viridis")
# Display processed result
gaussian_filtered = ij.op().filter().gauss(dataset, 2.0)
ij.py.show(gaussian_filtered, cmap="hot")
# Display without colormap (uses matplotlib default)
ij.py.show(array)Access and manage the currently active image in ImageJ's interface.
def active_dataset() -> "Dataset":
"""
Get the active image as an ImageJ2 Dataset.
Returns:
Current active Dataset from ImageJ2's Dataset service
Returns:
None if no image is active
"""
def active_imageplus(sync: bool = True) -> "ImagePlus":
"""
Get the active image as an ImageJ ImagePlus.
Args:
sync: Synchronize current ImagePlus slice if True
Returns:
Current active ImagePlus from original ImageJ
None if no image is active or legacy support unavailable
"""
def active_xarray(sync=True) -> xr.DataArray:
"""
Get the active image as an xarray DataArray.
Args:
sync: Synchronize current ImagePlus slice if True
Returns:
Active image converted to xarray.DataArray with dimension labels
"""Usage Examples:
# Open multiple images in ImageJ
ij.py.run_macro('open("/path/to/image1.tif");')
ij.py.run_macro('open("/path/to/image2.tif");')
# Get currently active image in different formats
active_ds = ij.py.active_dataset()
active_imp = ij.py.active_imageplus()
active_xr = ij.py.active_xarray()
print(f"Dataset dimensions: {active_ds.numDimensions()}")
print(f"ImagePlus title: {active_imp.getTitle()}")
print(f"xarray shape: {active_xr.shape}")
print(f"xarray dims: {active_xr.dims}")
# Process active image
if active_ds is not None:
processed = ij.op().filter().gauss(active_ds, 1.5)
processed_array = ij.py.from_java(processed)
ij.py.show(processed_array, cmap="gray")Synchronize image data between ImageJ and ImageJ2 when using legacy support.
def sync_image(imp=None):
"""
Synchronize data between ImageJ ImagePlus and ImageJ2 Dataset/ImageDisplay.
Args:
imp: Specific ImagePlus to synchronize, or None for active image
Note:
Ensures consistency when ImagePlus data is modified via ImageProcessor
without automatic Dataset/ImageDisplay synchronization
"""Usage Examples:
# Get active ImagePlus and modify it
imp = ij.py.active_imageplus()
if imp is not None:
# Direct pixel manipulation (bypasses automatic sync)
processor = imp.getProcessor()
processor.invert()
# Manually synchronize to ensure Dataset sees changes
ij.py.sync_image(imp)
# Now Dataset will reflect the inversion
synced_dataset = ij.py.active_dataset()
synced_array = ij.py.from_java(synced_dataset)
ij.py.show(synced_array)
# Synchronize all active images
ij.py.sync_image() # Syncs current active imageAccess ImageJ's window management system for controlling image displays.
# Through ImageJ2 gateway properties
@property
def WindowManager():
"""Access to original ImageJ WindowManager class for display management."""Usage Examples:
# Access WindowManager for display control
wm = ij.WindowManager
# Get list of open image windows
image_titles = wm.getImageTitles()
print(f"Open images: {list(image_titles)}")
# Get specific image by title
imp = wm.getImage("image.tif")
if imp is not None:
print(f"Image size: {imp.getWidth()}x{imp.getHeight()}")
# Close all images
wm.closeAllWindows()
# Check if any images are open
if wm.getImageCount() > 0:
print(f"{wm.getImageCount()} images currently open")
else:
print("No images open")Integration with ImageJ2's display system for complex visualization scenarios.
UI Service Integration:
# Access ImageJ2 UI services through gateway
ui_service = ij.ui()
# Show ImageJ2 user interface (in interactive mode)
ui_service.showUI()
# Check UI state
is_headless = ui_service.isHeadless()
is_visible = ui_service.isVisible()
print(f"Headless mode: {is_headless}")
print(f"UI visible: {is_visible}")Display Service Usage:
# Create and show ImageJ2 displays
dataset = ij.py.to_dataset(np.random.rand(256, 256))
# Create display through ImageJ2
display = ij.display().createDisplay("My Dataset", dataset)
# Access display properties
display_name = display.getName()
is_displaying = display.isDisplaying(dataset)
print(f"Display name: {display_name}")
print(f"Is displaying dataset: {is_displaying}")PyImageJ's display system integrates with matplotlib for Python-native visualization.
Custom Visualization:
import matplotlib.pyplot as plt
# Get image data
dataset = ij.io().open("path/to/image.tif")
image_array = ij.py.from_java(dataset)
# Custom matplotlib visualization
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Original image
axes[0].imshow(image_array, cmap='gray')
axes[0].set_title('Original')
axes[0].axis('off')
# Processed version
filtered = ij.op().filter().gauss(dataset, 2.0)
filtered_array = ij.py.from_java(filtered)
axes[1].imshow(filtered_array, cmap='gray')
axes[1].set_title('Gaussian Filtered')
axes[1].axis('off')
plt.tight_layout()
plt.show()Legacy methods maintained for backward compatibility.
def window_manager():
"""
Deprecated: Use ij.WindowManager instead.
Access to ImageJ's WindowManager class.
"""ij.py.show() requires matplotlib, not ImageJ GUIimage[::4, ::4]volume[:, :, slice_idx]Common display issues:
Install with Tessl CLI
npx tessl i tessl/pypi-pyimagej