Read OME-ZARR images

This sample code reads an image stored on remote s3 server, but the same code can be used to read data on a local file system. In either case, the data is exposed as an instance of the ome_zarr.classes.image.OMEZarrMultiscale class, which provides access to the multiscale levels and metadata of the OME-ZARR image.

from ome_zarr import OMEZarrMultiscale

url = "https://livingobjects.ebi.ac.uk/idr/zarr/v0.5/idr0062A/6001240_labels.zarr"

ngff_image = OMEZarrMultiscale.from_ome_zarr(url)

You can access the multiscale levels by inspecting the images attributes of the ome_zarr.classes.image.OMEZarrMultiscale object, which is a list of ome_zarr.classes.image.OMEZarrImage objects.

ngff_image.images
[OMEZarrImage(data=dask.array<from-zarr, shape=(2, 236, 275, 271), dtype=uint16, chunksize=(1, 1, 256, 256), chunktype=numpy.ndarray>, axes=['c', 'z', 'y', 'x'], scale={'c': 1.0, 'z': 0.5002025531914894, 'y': 0.3603981534640209, 'x': 0.3603981534640209}, axes_units={'z': 'micrometer', 'y': 'micrometer', 'x': 'micrometer'}, name='image'),
 OMEZarrImage(data=dask.array<from-zarr, shape=(2, 236, 137, 135), dtype=uint16, chunksize=(1, 1, 137, 135), chunktype=numpy.ndarray>, axes=['c', 'z', 'y', 'x'], scale={'c': 1.0, 'z': 0.5002025531914894, 'y': 0.7207963069280418, 'x': 0.7207963069280418}, axes_units={'z': 'micrometer', 'y': 'micrometer', 'x': 'micrometer'}, name='image'),
 OMEZarrImage(data=dask.array<from-zarr, shape=(2, 236, 68, 67), dtype=uint16, chunksize=(1, 1, 68, 67), chunktype=numpy.ndarray>, axes=['c', 'z', 'y', 'x'], scale={'c': 1.0, 'z': 0.5002025531914894, 'y': 1.4415926138560835, 'x': 1.4415926138560835}, axes_units={'z': 'micrometer', 'y': 'micrometer', 'x': 'micrometer'}, name='image')]

And of course, retrieve the data as a dask array using the data attribute of each image:

ngff_image.images[0].data
Array Chunk
Bytes 67.09 MiB 128.00 kiB
Shape (2, 236, 275, 271) (1, 1, 256, 256)
Dask graph 1888 chunks in 2 graph layers
Data type uint16 numpy.ndarray
2 1 271 275 236

You can check whether label images were attached to this image by inspecting the labels attribute of the ome_zarr.classes.image.OMEZarrMultiscale object, which is a dictionary mapping label image names to ome_zarr.classes.image.OMEZarrLabels objects.

ngff_image.labels
{'0': <ome_zarr.classes.image.OMEZarrLabels at 0x74ad285dab10>}

Direct read

The code below here demonstrates an alternative, equally functional API for reading OME-ZARR images.

This sample code reads an image stored on remote s3 server, but the same code can be used to read data on a local file system. In either case, the data is exposed as dask arrays;

You can obtain a list of “nodes” which include all arrays stored in the group:

from ome_zarr.io import parse_url
from ome_zarr.reader import Reader

# read the image data
reader = Reader(parse_url(url))
# nodes may include images, labels etc
nodes = list(reader())
nodes
[https://livingobjects.ebi.ac.uk/idr/zarr/v0.5/idr0062A/6001240_labels.zarr/ [zgroup],
 https://livingobjects.ebi.ac.uk/idr/zarr/v0.5/idr0062A/6001240_labels.zarr/labels/ [zgroup] (hidden),
 https://livingobjects.ebi.ac.uk/idr/zarr/v0.5/idr0062A/6001240_labels.zarr/labels/0/ [zgroup] (hidden)]

The first node will be the image pixel data; Since this group is again an ome-zarr multiscales object, it consists of several arrays that represent the different resolution levels:

image_node = nodes[0]

multiscales = image_node.data
multiscales
[dask.array<from-zarr, shape=(2, 236, 275, 271), dtype=uint16, chunksize=(1, 1, 256, 256), chunktype=numpy.ndarray>,
 dask.array<from-zarr, shape=(2, 236, 137, 135), dtype=uint16, chunksize=(1, 1, 137, 135), chunktype=numpy.ndarray>,
 dask.array<from-zarr, shape=(2, 236, 68, 67), dtype=uint16, chunksize=(1, 1, 68, 67), chunktype=numpy.ndarray>]

The first entry in this list represents the 0-th resolution level, and is the highest resolution data.

multiscales[0]
Array Chunk
Bytes 67.09 MiB 128.00 kiB
Shape (2, 236, 275, 271) (1, 1, 256, 256)
Dask graph 1888 chunks in 2 graph layers
Data type uint16 numpy.ndarray
2 1 271 275 236