Slides and OME-Zarr¶
The idea in plain words¶
A whole-slide scan is far too large to open as one picture: a single mouse lung scene is 37298 × 35806 pixels, about 4 GB of RGB. It is therefore stored as a pyramid: the full-resolution image (level 0) plus smaller copies (level 1 is half the size, level 2 a quarter, ...), each cut into tiles (chunks) on disk. OME-Zarr is the open, community standard for such pyramids.
A big image becomes a folder of small files. A reader fetches only the
files it needs, one at a time, and works on each piece without ever holding
the whole image in memory.
Illustration: Wavu, Oxford-Zeiss Centre of Excellence (GPL-3.0)
pyHisto's Slide is a window onto one OME-Zarr pyramid. It never loads the
whole image; it reads only the boxes you ask for, at the level you ask for.
import pyHisto as histo
slide = histo.open_slide("slide.ome.zarr")
print(slide)
print(slide.n_levels, [slide.level_shape(l) for l in range(slide.n_levels)])
print([slide.downsample(l) for l in range(slide.n_levels)], slide.mpp, slide.channel_order)
Slide('slide.ome.zarr', levels=4, shape0=(35806, 37298), mpp=0.1725, stored=BGR)
4 [(35806, 37298), (17903, 18649), (8951, 9324), (4475, 4662)]
[1.0, 2.0, 4.0, 8.0] 0.1725 BGR
This store, written by tanoa from an Axioscan CZI, has 4 levels; level 0 is 0.1725 µm per pixel; and its channels are stored as B, G, R.
What Slide sees in a tanoa store. Level 0 is cut into chunks of
1024 × 1024 pixels, and 4 × 4 chunks are packed into one shard, a single
file on disk (one channel per file). read_region touches only the chunks
under the requested box, and uses the channel labels to turn the stored
B, G, R planes into a true-RGB tile.
Getting there: tanoa¶
pyHisto does not read CZI, LIF, OIB or TIFF files. Conversion is a separate
step done by tanoa, which writes OME-Zarr (NGFF 0.5, Zarr v3) with the
physical pixel size and the channel names. In a batch,
tanoa.to_ome_zarr(src, dst, series=...) writes one store per scene.
True RGB: channel labels decide¶
Zeiss scanners store brightfield colour as Bgr24: blue first. tanoa keeps
that order and labels the channels Blue, Green, Red. Slide maps the stored
channels to R, G, B using those labels, in exactly one place
(read_region), so everything downstream sees true RGB.
Why channel order matters¶
Colour images are three planes of numbers, but files do not agree on their order. Many microscopy and imaging formats store colour as blue, green, red (BGR): Zeiss "Bgr24" CZI files, Windows bitmaps, OpenCV. Most analysis tools, QuPath's stain vectors included, assume red, green, blue (RGB).
If the order is wrong, red and blue are swapped silently. The image still looks plausible to software, but every pixel's optical density points in a different direction, the stain vectors no longer match the dyes, and the separated "PSR" channel mixes in the counterstain. On PSR-stained lung sections, swapping red and blue changed the measured collagen fraction by roughly 8–12 percentage points.
Swapped colours are easy to spot by eye: collagen that should be red looks blue or purple, and a Fast Green counterstain looks orange.
How pyHisto handles it:
- channel labels such as
Red/Green/BlueorR/G/B(in any order) are read from the OME-Zarr metadata and used; - a 3-channel store without labels raises an error unless you pass
channel_order="RGB"or"BGR"; - an explicit
channel_orderthat contradicts the labels also raises an error.
So the channel order is always known, never guessed.
Levels, downsample and pixel size¶
| Call | Returns | Notes |
|---|---|---|
slide.level_shape(level) |
(height, width) |
in pixels of that level |
slide.downsample(level) |
size factor relative to level 0 | from the physical scales in the metadata (here 1, 2, 4, 8) |
slide.mpp |
µm per pixel at level 0 | from the metadata, or open_slide(..., mpp=0.25) |
slide.level_mpp(level) |
µm per pixel at that level | mpp × downsample(level) |
slide.read_region(level, x, y, w, h) |
(h, w, 3) uint8 true-RGB array |
x, y, w, h are in pixels of that level |
open_slide(..., mpp=0.25) |
a Slide |
overrides the pixel size in the metadata |
For the example slide above:
| Level | Shape (height × width, px) | Downsample | µm per pixel |
|---|---|---|---|
| 0 | 35806 × 37298 | 1 | 0.1725 |
| 1 | 17903 × 18649 | 2 | 0.345 |
| 2 | 8951 × 9324 | 4 | 0.69 |
| 3 | 4475 × 4662 | 8 | 1.38 |
No-data: the black canvas¶
A mosaic scan covers only the tiles where tissue was detected; the rest of the rectangular canvas is stored as 0, 0, 0 (black). This is not tissue and not glass, and pyHisto treats it as no-data:
slide.nodata_mask(level)marks pixels where all channels are 0;- no-data is excluded from the tissue mask, from the background (white level) estimate and from region sampling: a calibration region never contains a no-data pixel.
Arrays instead of files¶
For tests and notebooks, a NumPy or dask array also works, as long as you say what it is:
The array must be uint8, shaped (Y, X, 3) or (3, Y, X). Only uint8 RGB
slides are supported.
Exact rules¶
Each image axis has a name: time, planes (z), channels and the rows and
columns of each plane (y, x). This figure shows the order Wavu uses in
memory; an OME-Zarr store lists the same axes as t, c, z, y, x. A
brightfield slide has one time point and one plane, so pyHisto works with
the channels and y × x.
Illustration: Wavu, Oxford-Zeiss Centre of Excellence (GPL-3.0)
- Accepted stores: tanoa NGFF 0.5 (axes
t,c,z,y,x), legacy pyHisto 4Dc,z,y,x, NGFF 0.4-style top-level attributes. - Channel order comes from the
omerochannel labels orchannel_order; never guessed. - Downsample factors = physical
scaleof each level / scale of level 0 (if the scales are missing: level-0 width / level width). - Reads are lazy: only the chunks overlapping the requested window are read.
thumbnail(max_px)reads the coarsest level still at leastmax_pxlong and box-averages it, in row strips (bounded memory).
See the pyHisto.slide API.