OME-Zarr Python Library

0.16.0 · active · verified Thu Apr 16

ome-zarr is a Python library providing an implementation of images in Zarr files according to the OME-Zarr specification. It enables reading and writing bioimaging data stored in the OME-Zarr format, supporting multiscale images, metadata, and various Zarr features like chunking and compression. The current version is 0.16.0, with a release cadence that sees new features and bug fixes approximately every few months, often tied to updates in the OME-Zarr specification itself.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a dummy 5D NumPy array, write it to a local OME-Zarr store using `ome_zarr.writer.write_image`, and then read the data and its associated OME metadata back using `ome_zarr.reader.Reader`. It highlights the use of `parse_url` for store creation and the importance of specifying `axes` during writing.

import numpy as np
import zarr
import os
import shutil
from ome_zarr.io import parse_url
from ome_zarr.writer import write_image
from ome_zarr.reader import Reader

# Define a path for the OME-Zarr store
store_path = 'my_ome_zarr_image.zarr'

# Clean up previous store if it exists for a fresh run
if os.path.exists(store_path):
    shutil.rmtree(store_path)

# 1. Create a dummy numpy array (e.g., a 5D image: T, C, Z, Y, X)
# OME-Zarr typically uses this dimension order or a subset.
# Here: 1 Time point, 2 Channels, 3 Z-slices, 64 Y, 64 X
data = np.zeros((1, 2, 3, 64, 64), dtype=np.uint16)
data[0, 0, 1, 10:20, 10:20] = 100 # Channel 0, Z-slice 1
data[0, 1, 2, 30:40, 30:40] = 200 # Channel 1, Z-slice 2

# 2. Write the numpy array to an OME-Zarr store
# `parse_url` creates an FSSpec store object.
# `zarr.group` creates the root Zarr group, overwriting if it exists.
storage = parse_url(store_path, mode='w').store
root_group = zarr.group(storage, overwrite=True)

# `write_image` handles the OME-Zarr metadata and array layout.
write_image(
    image=data,
    group=root_group,
    axes=['t', 'c', 'z', 'y', 'x'], # Specify axes order, crucial for OME-Zarr
    chunks=(1, 1, 1, 32, 32) # Optional: define Zarr chunking
)

print(f"OME-Zarr image written to: {store_path}")

# 3. Read the OME-Zarr image
reader = Reader(parse_url(store_path))

# Get the list of nodes (images) in the Zarr store. Typically one at the root.
nodes = list(reader())[0] 

# Access image data for the highest resolution (level 0)
image_data_level_0 = nodes.data[0]

# Access OME metadata (a dictionary parsed from the OME-XML JSON)
ome_metadata = nodes.metadata

print(f"Successfully read OME-Zarr image from: {store_path}")
print(f"Shape of data (level 0): {image_data_level_0.shape}")
print(f"OME metadata keys: {ome_metadata.keys()}")

# Clean up the created Zarr store (optional but good practice for examples)
shutil.rmtree(store_path)
print(f"Cleaned up: {store_path}")

view raw JSON →