Python wrapper for ImageJ2 that provides seamless integration between ImageJ and Python scientific computing ecosystems
—
Access to ImageJ2's extensive image processing capabilities through Python-friendly interfaces, including mathematical operations, filtering, array-like access, and transformation functions on ImgLib2 RandomAccessibleInterval objects.
Perform element-wise mathematical operations on RandomAccessibleInterval objects using Python operators.
# Mathematical operators (applied to RandomAccessibleInterval objects)
def __add__(self, other):
"""Element-wise addition: image + value or image + image"""
def __sub__(self, other):
"""Element-wise subtraction: image - value or image - image"""
def __mul__(self, other):
"""Element-wise multiplication: image * value or image * image"""
def __truediv__(self, other):
"""Element-wise division: image / value or image / image"""Usage Examples:
# Load or create images
img1 = ij.py.to_img(np.random.rand(100, 100))
img2 = ij.py.to_img(np.random.rand(100, 100))
# Mathematical operations
result_add = img1 + img2 # Image addition
result_scale = img1 * 2.5 # Scalar multiplication
result_subtract = img1 - 128 # Offset subtraction
result_normalize = img1 / img1.max() # Normalization
# Convert back to Python for display
result_array = ij.py.from_java(result_add)Access image data using familiar Python array indexing and slicing syntax.
def __getitem__(self, key):
"""
Array-like access with support for integer indexing and slice objects.
Args:
key: int, slice, or tuple of int/slice objects
Returns:
Single pixel value or sliced RandomAccessibleInterval
"""Usage Examples:
# Create test image
img = ij.py.to_img(np.random.rand(100, 100, 50))
# Single pixel access
pixel_value = img[50, 50, 25]
# Slicing operations
roi = img[10:90, 20:80, :] # Region of interest
plane = img[:, :, 25] # Single Z plane
subset = img[::2, ::2, ::5] # Downsampled version
# Multi-dimensional slicing
volume = img[25:75, 25:75, 10:40] # 3D subvolumeTransform image geometry and dimensions using ImgLib2 operations.
def transpose() -> "RandomAccessibleInterval":
"""Transpose all dimensions of the image."""
@property
def T() -> "RandomAccessibleInterval":
"""Shorthand property for transpose operation."""
def squeeze(axis=None):
"""
Remove axes of length one from the image.
Args:
axis: int, tuple of ints, or None for all single-dimension axes
Returns:
RandomAccessibleInterval with singleton dimensions removed
"""Usage Examples:
# Create test image with singleton dimensions
img = ij.py.to_img(np.random.rand(1, 100, 100, 1, 50))
print(f"Original shape: {img.shape}") # (1, 100, 100, 1, 50)
# Remove all singleton dimensions
squeezed = img.squeeze()
print(f"Squeezed shape: {squeezed.shape}") # (100, 100, 50)
# Remove specific axis
partial_squeeze = img.squeeze(axis=0)
print(f"Partial squeeze: {partial_squeeze.shape}") # (100, 100, 1, 50)
# Transpose operations
transposed = img.transpose()
# or equivalently:
transposed = img.TQuery image properties and characteristics.
@property
def dtype():
"""Get the dtype of the RandomAccessibleInterval as ImgLib2 Type subclass."""
@property
def shape():
"""Get shape tuple of the interval dimensions."""
@property
def ndim():
"""Get number of dimensions in the image."""Usage Examples:
img = ij.py.to_img(np.random.rand(256, 256, 100).astype(np.float32))
# Query image properties
print(f"Shape: {img.shape}") # (256, 256, 100)
print(f"Dimensions: {img.ndim}") # 3
print(f"Data type: {img.dtype}") # FloatType class
print(f"Min coords: {[img.min(d) for d in range(img.ndim)]}")
print(f"Max coords: {[img.max(d) for d in range(img.ndim)]}")Access ImageJ2's Ops framework for advanced image processing operations.
# Through the ImageJ2 gateway, access ops for processing
# Examples of common operations available via ij.op()Usage Examples:
# Create test image
img = ij.py.to_img(np.random.rand(256, 256))
# ImageJ2 Ops operations
gaussian_filtered = ij.op().filter().gauss(img, 2.0)
median_filtered = ij.op().filter().median(img, ij.op().create().kernelGauss([3, 3]))
thresholded = ij.op().threshold().otsu(img)
# Mathematical operations via Ops
img_doubled = ij.op().math().multiply(img, 2.0)
img_log = ij.op().math().log(img)
# Morphological operations
opened = ij.op().morphology().open(img, ij.op().create().kernelDisk(5))
closed = ij.op().morphology().close(img, ij.op().create().kernelDisk(5))
# Convert results back to Python
result = ij.py.from_java(gaussian_filtered)Specialized functions for working with multi-dimensional image stacks.
def rai_slice(rai, imin: tuple, imax: tuple, istep: tuple):
"""
Slice ImgLib2 RandomAccessibleInterval using Python slice notation.
Args:
rai: Input RandomAccessibleInterval
imin: Tuple of minimum values for each dimension
imax: Tuple of maximum values for each dimension
istep: Tuple of step sizes for each dimension
Returns:
Sliced RandomAccessibleInterval including both imin and imax bounds
"""Usage Examples:
from imagej.stack import rai_slice
# Create 3D volume
volume = ij.py.to_img(np.random.rand(100, 100, 50))
# Extract middle region with custom stepping
roi = rai_slice(
volume,
imin=(25, 25, 10), # Start coordinates
imax=(75, 75, 40), # End coordinates
istep=(1, 1, 2) # Step sizes (every other Z slice)
)
# Convert back to examine
roi_array = ij.py.from_java(roi)
print(f"ROI shape: {roi_array.shape}")RandomAccessibleInterval objects integrate seamlessly with ImageJ2 services:
ij.op().run("operation.name", img, params)ij.convert().convert(img, target_type)ij.dataset().create(img)Common issues when working with image processing:
Install with Tessl CLI
npx tessl i tessl/pypi-pyimagej