Skip to main content
Core types for representing resolutions and regions in Whole Slide Images (WSIs). This module defines lightweight, immutable classes for commonly used WSI attributes such as pyramid levels, physical pixel size (MPP), and objective magnification. Classes:
  • Level — Pyramid level index (0 = highest resolution).
  • MPP — Microns per pixel (µm/pixel).
  • Magnification — Objective power (e.g., 20x).
  • WSIDims — Width and height dimensions.
  • WSIBounds — A bounding box for tissue regions.
  • MeasurementUnit — Enum for pixels or microns.
  • RegionLocation — X/Y coordinates of a region.
  • RegionShape — Dimensions and units of a region.
  • RegionSpec — Full specification (Location + Shape + Resolution).
  • Region — Container for PIL image data and its RegionSpec.

Level

Represents a pyramid level index in a WSI. Level 0 is the highest resolution. Higher indices are lower resolutions.
int
The pyramid level index (0-based).
Raises:
  • ValueError — If index is negative.
Example:
Notes:
  • Level 0 is the highest resolution.
  • Higher indices correspond to lower resolution levels in the pyramid.
  • This class stores the level index as a primitive integer and is immutable.

MPP

A positive float representing the physical size of a pixel in microns per pixel (µm/pixel) in a whole slide image (WSI). MPP values indicate the real-world size of one image pixel and must be strictly greater than 0.
float
The pixel size in microns to represent as MPP.
Raises:
  • ValueError — If value is not positive.
Example:

Magnification

Objective power of a microscope used to acquire a whole slide image (WSI). Magnification indicates the target resolution (e.g., 20x, 40x). It is always stored as a positive float. Typical magnifications:
  • 10x: low-resolution overview
  • 20x: standard diagnostic resolution
  • 40x: high-resolution for detailed analysis
float
The objective power.
Raises:
  • ValueError — If value is not positive.
Example:

WSIDims

Dimensions of a slide or level at full resolution.
int
The width of the image in pixels.
int
The height of the image in pixels.
Example:

WSIBounds

The non-empty region (Bounding Box) of the slide in pixels. This represents the “tissue-containing” area of a slide, allowing users to ignore large background areas.
int
The X coordinate of the top-left corner.
int
The Y coordinate of the top-left corner.
int
The width of the bounding box.
int
The height of the bounding box.
Example:

dims

Extracts width and height as a WSIDims object.
WSIDims
The dimensions of the bounding box.
Example:

MeasurementUnit

Enumeration of measurement units for WSI tiles and regions.

has_value

Checks if a string is a valid MeasurementUnit.
str
required
The string to check.
bool
True if valid, False otherwise.
Example:

RegionLocation

Defines the location of a region within a WSI.
int
The top coordinate of the top-left corner of the region.
int
The left coordinate of the top-left corner of the region.
int
The width of the region in pixels.
int
The height of the region in pixels.
Example:

x

Alias for left coordinate.

y

Alias for top coordinate.

RegionShape

Defines the shape of a region within a WSI.
int
The width of the region in pixels.
int
The height of the region in pixels.
MeasurementUnit
The unit of measurement for the shape dimensions (e.g., PIXELS, UM).
Example:

shape

Returns the width and height as a tuple.
tuple[int, int]
tuple[int, int]: A tuple containing (width, height).

to_um

Converts pixels to microns.
float
The pixel count to convert.
MPP
required
The microns-per-pixel scale.
float
The length in microns.
Example:

to_pixels

Converts microns to pixels.
float
The length in microns to convert.
MPP
required
The microns-per-pixel scale.
float
The length in pixels.
Example:

RegionMaskSpec

Defines the mask applied/corresponding to the region e.g., during extraction can be binary or multiclass mask.
float
Pixels belonging to the mask relative to the total region area.
np.ndarray | None
The mask applied to the region, or None if mask storage was not requested.

RegionSpec

Defines the specifications of a region within a WSI.
RegionLocation
The location of the region within the slide.
RegionShape
The shape of the region.
Resolution
The resolution at which the region is defined (Level, MPP, or Magnification).
Example:

Region

Defines a region of interest within a WSI, including its image data and specifications. This class encapsulates both the pixel data of a region and its metadata, such as location, shape, and resolution.
PIL.Image.Image
The pixel data of the region as a PIL Image.
RegionSpec
The specifications of the region, including location, shape, resolution, and mask.
Example:

location

Convenience property to access the region’s location directly.

shape

Convenience property to access the region’s shape directly.

resolution

Convenience property to access the region’s resolution directly.

measurement_unit

Convenience property to access the region’s measurement unit directly.

mask

Convenience property to access the region’s mask directly.
Union[np.ndarray, None]
np.ndarray | None: The mask array, or None if mask storage was not requested during extraction.

mask_ratio

Convenience property to access the region’s mask ratio directly.