Python Image Sequence (PIMS)

0.7 · active · verified Thu Apr 16

PIMS (Python Image Sequence) provides a lazy-loading, NumPy-like interface for accessing sequential image and video data from various file formats. It handles diverse inputs, including image directories, TIFF stacks, and common video formats, offering a consistent API for slicing and iteration. The library is actively maintained, with a recent major release (v0.7) in June 2024.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a sequence of image files using `pims.open`, access individual frames, iterate through the sequence, and use NumPy-like slicing. It includes a basic setup to create dummy image files if Pillow is available.

import pims
import numpy as np
import os

# Create dummy image files for demonstration
if not os.path.exists('test_images'):
    os.makedirs('test_images')
for i in range(5):
    # Using a minimal image writing approach or mocking if actual image lib is heavy
    # For real use, ensure Pillow or scikit-image is installed.
    try:
        from PIL import Image
        img = Image.new('L', (10, 10), color = i * 50)
        img.save(f'test_images/frame_{i:02d}.png')
    except ImportError:
        print("Pillow not found, cannot create dummy images. Skipping image creation.")
        break

# Open a sequence of images
try:
    frames = pims.open('test_images/frame_*.png')
    print(f"Opened sequence with {len(frames)} frames.")
    
    # Access a single frame (lazy loading)
    first_frame = frames[0]
    print(f"Shape of the first frame: {first_frame.shape}")
    print(f"Pixel value at (0,0) in first frame: {first_frame[0, 0]}")
    
    # Iterate through frames
    sum_pixels = 0
    for frame in frames:
        sum_pixels += np.sum(frame)
    print(f"Total sum of all pixel values: {sum_pixels}")

    # Slicing returns another lazy-loading object
    sub_sequence = frames[1:4]
    print(f"Sub-sequence has {len(sub_sequence)} frames.")

    # Example with a specific reader (if 'test_tiff.tif' existed)
    # from pims import TiffStack
    # tiff_stack = TiffStack('test_tiff.tif')
    # print(f"Tiff stack has {len(tiff_stack)} frames.")

except Exception as e:
    print(f"Could not run PIMS quickstart example: {e}")
    print("Ensure test_images/ directory and dummy images exist or necessary reader dependencies are installed.")

view raw JSON →