CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyimagej

Python wrapper for ImageJ2 that provides seamless integration between ImageJ and Python scientific computing ecosystems

Pending
Overview
Eval results
Files

image-processing.mddocs/

Image Processing

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.

Capabilities

Mathematical Operations

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)

Array-Like Access and Slicing

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 subvolume

Image Transformations

Transform 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.T

Type and Shape Information

Query 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)]}")

ImageJ2 Ops Integration

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)

Stack and Volume Operations

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}")

Integration with ImageJ2 Services

RandomAccessibleInterval objects integrate seamlessly with ImageJ2 services:

  • Ops Service: ij.op().run("operation.name", img, params)
  • Convert Service: ij.convert().convert(img, target_type)
  • Dataset Service: ij.dataset().create(img)

Performance Considerations

  • View Operations: Slicing and transformations create views, not copies
  • Memory Sharing: ImgLib2 images can share memory with NumPy arrays via imglyb
  • Lazy Evaluation: Many operations are computed on-demand
  • Threading: ImgLib2 operations are thread-safe and can utilize multiple cores

Error Handling

Common issues when working with image processing:

  • Dimension Mismatch: Ensure compatible dimensions for mathematical operations
  • Type Compatibility: Some operations require specific numeric types
  • Memory Limits: Large images may require increased JVM heap size
  • Index Bounds: Slicing operations validate bounds automatically

Install with Tessl CLI

npx tessl i tessl/pypi-pyimagej

docs

data-conversion.md

display-visualization.md

environment-diagnostics.md

gateway-initialization.md

image-processing.md

index.md

script-execution.md

tile.json