class squidpy.im.ImageContainer(img=None, layer='image', lazy=True, scale=1.0, **kwargs)[source]

Container for in memory arrays or on-disk images.

Wraps xarray.Dataset to store several image layers with the same x, y and z dimensions in one object. Dimensions of stored images are (y, x, z, channels). The channel dimension may vary between image layers.

This class also allows for lazy loading and processing using dask, and is given to all image processing functions, along with anndata.AnnData instance, if necessary.

  • img (Union[str, Path, ndarray[Any, dtype[Any]], DataArray, ImageContainer, None]) – In-memory 2, 3 or 4-dimensional array, a URL to a Zarr store (ending in .zarr), or a path to an on-disk image.

  • layer (str) – Image layer in img that should be processed. If None and only 1 layer is present, it will be selected.

  • dims

    Where to save channel dimension when reading from a file or loading an array. Valid options are:

    • ’channels_last’ - load the last non-spatial dimension as channels.

    • ’z_last’ - load the last non-spatial dimension as Z-dimension.

    • ’default’ - same as ‘channels_last’, but for 4-dimensional arrays, tries to also load the first dimension as channels if the last non-spatial dimension is 1.

    • a sequence of dimension names matching the shape of img, e.g. ('y', 'x', 'z', 'channels'). ‘y’, ‘x’ and ‘z’ must always be present.

  • library_id – Name for each Z-dimension of the image. This should correspond to the library_id in anndata.AnnData.uns.

  • lazy (bool) – Whether to use dask to lazily load image.

  • chunks – Chunk size for dask. Only used when lazy = True.

  • copy – Whether to copy the underlying data if img is an in-memory array.

  • scale (float) – Scaling factor of the image with respect to the spatial coordinates saved in the accompanying anndata.AnnData.

  • ValueError – If loading from a file/store with an unknown format or if a supplied channel dimension cannot be aligned.

  • NotImplementedError – If loading a specific data type has not been implemented.


add_img(img[, layer, dims, library_id, ...])

Add a new image to the container.

apply(func[, layer, new_layer, channel, ...])

Apply a function to a layer within this container.


Trigger lazy computation in-place.

concat(imgs[, library_ids, combine_attrs])

Concatenate imgs in Z-dimension.


Return a copy of self.

crop_center(y, x, radius, **kwargs)

Extract a circular crop.

crop_corner(y, x[, size, library_id, scale, ...])

Extract a crop from the upper-left corner.

features_custom(func, layer[, channels, ...])

Calculate features using a custom function.

features_histogram(layer[, library_id, ...])

Compute histogram counts of color channel values.

features_segmentation(label_layer[, ...])

Calculate segmentation features using skimage.measure.regionprops().

features_summary(layer[, library_id, ...])

Calculate summary statistics of image channels.

features_texture(layer[, library_id, ...])

Calculate texture features.

from_adata(adata[, img_key, library_id, ...])

Load an image from anndata object.

generate_equal_crops([size, as_array, squeeze])

Decompose image into equally sized crops.

generate_spot_crops(adata[, spatial_key, ...])

Iterate over anndata.AnnData.obs_names and extract crops.

interactive(adata[, spatial_key, ...])

Launch napari viewer.

load(path[, lazy, chunks])

Load data from a Zarr store.

rename(old, new)

Rename a layer.

save(path, **kwargs)

Save the container into a Zarr store.

show([layer, library_id, channel, ...])

Show an image within this container.

subset(adata[, spatial_key, copy])

Subset anndata.AnnData using this container.

uncrop(crops[, shape])

Re-assemble image from crops and their positions.



Underlying xarray.Dataset.


Library ids.


Image shape (y, x).