Skip to content

Reading Whole Slide Images (WSIs)

HistoKit provides a unified interface for reading and processing histopathological images, including multi-resolution Whole Slide Images (WSIs) and single-resolution images.

The Slide class provides access to image data, slide metadata, resolution levels, and associated images, regardless of the underlying file format.

Under the hood, HistoKit uses OpenSlide and Pillow (PIL) as image-reading backends.

Supported image formats

HistoKit supports both multi-resolution and single-resolution images.

Image type Example formats
Multi-resolution images SVS, NDPI, TIFF, TIF
Single-resolution images TIFF, TIF, JPEG, PNG, BMP

Tutorial

Open In Colab Open Example

Loading a slide

A histopathological image can be loaded by creating an instance of the Slide class.

from histokit.slide.slide import Slide

slide = Slide("path/to/slide.svs")

Once loaded, the slide provides access to its properties, magnification, spatial resolution, and available resolution levels.

Attribute Description
properties Metadata extracted from the original image file.
mag Magnification of the highest-resolution image.
mpp Spatial resolution expressed in microns per pixel.
level_dimensions Image dimensions at each resolution level.
level_mag Magnification corresponding to each resolution level.

Associated images, such as thumbnails and slide labels, can also be accessed when available in the original file.

Resolution selection

HistoKit provides three ways to specify the desired image resolution.

Parameter Description Example
mag Optical magnification. mag=5
mpp Spatial resolution in microns per pixel. mpp=0.5
level Native resolution level of the image pyramid. level=1

Magnification-based reading

The desired resolution is specified directly as a magnification value, such as 1.25x, 5x, 10x, or 20x.

image = slide.read_region(mag=5)

This approach is particularly useful when working with histopathological workflows defined at specific magnifications.

MPP-based reading

Microns per pixel (MPP) describes the physical size represented by a single image pixel.

image = slide.read_region(mpp=0.5)

Using MPP is particularly useful when processing images acquired using different scanners, as it allows images to be read at a consistent physical resolution.

A smaller MPP value corresponds to a higher spatial resolution.

Level-based reading

Multi-resolution WSIs typically store images as a pyramid of precomputed resolution levels.

image = slide.read_region(level=1)

Resolution levels are indexed starting from 0, where level 0 represents the highest-resolution image.

Unlike magnification-based and MPP-based reading, level-based reading directly selects an existing resolution level.

Image reading modes

HistoKit provides two main methods for extracting image data:

  • read_region() — reads an entire image or a selected region at the requested resolution.
  • read_object() — reads a region defined at one magnification and extracts the corresponding tissue area at another resolution.

Reading an entire image

When no bounding box is provided, read_region() reads the entire image at the requested resolution.

image = slide.read_region(mag=1.25)

This mode is particularly useful for generating slide previews, performing tissue segmentation, and conducting low-resolution image analysis.

Reading a region of interest (ROI)

A specific region can be extracted by providing a bounding box using the bbox parameter.

image = slide.read_region(
    mag=5,
    bbox=(0, 0, 500, 500),
)

This mode is useful for extracting image patches, inspecting selected tissue regions, and processing large images without loading the entire slide into memory.

When reading regions of the same pixel dimensions at different magnifications, the physical tissue area covered by the extracted image changes.

At lower magnifications, the extracted region covers a larger physical area of the tissue.

Reading an object across magnifications

In many histopathological workflows, regions of interest are identified at a lower magnification and subsequently analyzed at a higher magnification.

The read_object() method simplifies this process by automatically transforming bounding box coordinates between magnifications.

image = slide.read_object(
    mag=20,
    bbox=(0, 0, 500, 500),
    mag_bbox=5,
)

The mag_bbox parameter specifies the magnification at which the bounding box was originally defined, while mag specifies the desired output magnification.

The output resolution can also be specified using MPP.

image = slide.read_object(
    mpp=0.5,
    bbox=(0, 0, 500, 500),
    mag_bbox=5,
)

Unlike read_region(), this method preserves the physical tissue area represented by the bounding box while adjusting the output image dimensions to the requested resolution.

This is particularly useful for:

  • Extracting tissue regions detected at lower magnifications.
  • Multi-resolution image analysis.
  • Extracting high-resolution regions from low-resolution segmentation results.
  • Region-based feature extraction.

Comparison of reading modes

Feature read_region() read_object()
Reading an entire slide Yes Not its intended use
Reading a selected region Yes Yes
Magnification-based reading Yes Yes
MPP-based reading Yes Yes
Bounding box defined at another magnification No Yes
Automatic bounding box transformation No Yes
Preserving the same physical tissue area across magnifications Not automatically Yes

Automatic resolution scaling

The requested magnification or MPP does not necessarily need to correspond to an existing resolution level in the original image pyramid.

When the requested resolution is not available, HistoKit selects an available resolution level and automatically rescales the image to the desired resolution.

For example, an image stored at 5x magnification can be rescaled to 2.5x magnification.

image = slide.read_region(mag=2.5)

This allows users to work with consistent resolutions regardless of the original image pyramid structure.

Interpolation methods

By default, HistoKit uses Lanczos interpolation for image rescaling.

The interpolation method can be changed according to the requirements of the application.

from PIL import Image

slide.rescale_method = Image.Resampling.NEAREST

Common interpolation methods include:

Method Description
LANCZOS High-quality image rescaling (default).
NEAREST Nearest-neighbor interpolation, useful for masks and categorical data.
BILINEAR Linear interpolation between neighboring pixels.
BICUBIC Smooth interpolation using a larger pixel neighborhood.

Changing the rescale_method attribute affects only the current Slide instance.

Memory considerations

Working with large images

Whole Slide Images can contain billions of pixels. Reading an entire slide at its highest magnification may require a significant amount of RAM.

Whenever possible, read selected regions or use lower magnifications instead of loading the entire highest-resolution image into memory.