Getting started¶
This page takes you from installation to reading image pixels and metadata. Each step links to a guide with more detail.
1. Install¶
Imaris, ilastik, BigDataViewer and other HDF5-based formats require the hdf5 extra:
2. Open a file and inspect its contents¶
import micro_reader
f = micro_reader.open("MouseBrain_41Slices_2x2Tiles_3Channels_2Illuminations_1Angle.czi")
print(f)
<micro_reader.File 'MouseBrain_41Slices_2x2Tiles_3Channels_2Illuminations_1Angle.czi' backend='czi' format='czi' images=8>
series 0: tiles 0-3, x illumination 2, tczyx (1, 3, 41, 1000, 1500) uint16
The output describes the file's structure. It contains one series with four mosaic tiles (tiles 0-3), each imaged from two illumination sides (illumination 2). This gives 4 × 2 = 8 images. Each image is a t c z y x array of uint16.
Use a context manager to ensure that file handles are closed when you are done:
3. Select an image¶
Each image has an index that identifies it within the file:
f.images[0].index
# {'series': 0, 'tile': 0, 'illumination': 0}
f.index_range
# {'series': 1, 'tile': 4, 'illumination': 2}
left_side = f.select(illumination=0) # the four tiles imaged from side 0
image = f.select(tile=2, illumination=1)[0]
You can also pass a list, tuple, range or set to select multiple values. For example:
For plate formats, you can select images by name, too:
Name the plate as well, by position (plate=0) or by name (plate_name=). A level you do not name matches every value, so well_name="B2" alone selects well B2 in every plate the file holds.
See Plates for the available plate-selection keywords.
4. Read pixels¶
An image behaves like a read-only NumPy array. Its pixels remain in the file until you request them:
image.axes # 'tczyx'
image.shape # (1, 3, 41, 1000, 1500)
image.dtype # dtype('uint16')
# NumPy style indexing
plane = image[0, 1, 20] # t=0, channel=1, z=20 -> (1000, 1500)
# Or use .read() method for axis-aware indexing
block = image.read(c=1, z=slice(10, 20), y=slice(0, 512))
block.shape # (1, 10, 512, 1500)
As in NumPy, integer indexing removes the corresponding axis. Only the parts of the file overlapping the requested region are read and decoded.
5. Read metadata¶
Metadata is available through image.metadata:
meta = image.metadata
meta.pixel_sizes
# {
# 'x': Quantity(value=2.5663047987549903, unit='micrometer'),
# 'y': Quantity(value=2.5663047987549903, unit='micrometer'),
# 'z': Quantity(value=7.0, unit='micrometer')
# }
[ch.name for ch in meta.channels]
# ['Cam1-T1', 'Cam2-T1', 'Cam2-T2']
meta.channels[0].emission_wavelength.unit
# 'nanometer'
meta.position # image position in the stage coordinate system
# {'x': Quantity(value=-3946.82..., unit='micrometer'),
# 'y': Quantity(value=15846.91..., unit='micrometer')}
meta.coordinate_system
# 'stage'
→ Metadata
6. Choose the array layout¶
Different formats store image axes in different orders. By default, open() gives every image the same layout, t c z y x, adding missing axes with size 1. To keep the order the file stores instead, pass axes=None:
f = micro_reader.open("cells.ics") # every image is t c z y x
f = micro_reader.open("cells.ics", axes=None) # the file's own order, for example c t z y x
You can also change the layout of an individual image:
image.as_axes("zyx") # drop size-1 t and c axes, and put z first
image.squeeze() # drop size-1 axes except y and x
RGB images, such as photographs and brightfield slides, are represented as three channels (red, green and blue), so c has a size of 3. To preserve the stored s axis instead, pass samples="keep" to open.
→ Axes
7. Where to go next¶
| If you want to… | Read |
|---|---|
| Understand files, images and indices | Concepts |
| Read large files without running out of memory | Memory and threads |
| Work with pyramidal images (whole-slide, Imaris, CZI) | Resolution levels |
| Read high-content screening plates (Harmony, Columbus, MetaXpress, IN Cell, CellVoyager, Opera Flex, scanR, etc.) | Plates |
| Group files into images before converting them | Multi-file images |
| See complete working programs | Examples |