WSIReader abstract base class, which standardizes access to Whole Slide Images (WSI). It handles coordinate translations, resolution scaling (Level, Magnification, MPP), and tissue-aware cropping.
Typical usage example:
with OpenSlideReader(“path/to/slide.svs”) as reader:
Get tissue-only dimensions
dims = reader.dimensions(bounded=True)Read a region relative to the slide origin
region = reader.read_region((0, 0), (512, 512), Level(0), bounded=False)WSIReader
path- Path object pointing to the slide file.props- Metadata properties extracted from the slide.
open_slide
NotImplementedError- If not implemented by subclass.
level_count
int- Number of pyramid levels.
NotImplementedError- If not implemented by subclass.
mpp
Optional[MPP]- The MPP value if found in metadata, else None.
downsample_dimensions
The ratio to scale down by.
Whether to return tissue bounds or full slide size.
level_dimensions
The target pyramid level (0 is highest resolution).
If True, returns tissue area at that level.
ValueError- If level is out of range.
magnification_dimensions
Target magnification (e.g., 40x, 20x, 10x).
If True, returns tissue area at that magnification.
mpp_dimensions
Target microns per pixel.
If True, returns tissue area at that MPP.
dimensions
Acts as the
bounded toggle when a bool.If True, returns tissue bounds.
dimensions
Target pyramid level.
If True, returns tissue bounds.
dimensions
Target optical magnification.
If True, returns tissue bounds.
dimensions
Target microns per pixel.
If True, returns tissue bounds.
dimensions
Desired resolution (Level, Magnification, or MPP).
If True, returns tissue area at that resolution.
get_downsample_factor
The target resolution (Level, Magnification, or MPP).
float- Downsample factor for the given resolution.
level_downsample
Pyramid level index.
float- Downsample factor for the given level.
NotImplementedError- If not implemented by subclass.
mpp_downsample
Target microns per pixel.
float- Downsample factor for the target MPP.
ValueError- If metadata is missing or target is too high-res.
magnification_downsample
Target magnification power.
float- Downsample factor for the target magnification.
ValueError- If metadata is missing or target is out of range.
props
WSIProps- Metadata extracted from the slide.
get_best_level_for_downsample
Target scaling factor.
int- Best pyramid level index for the downsample.
ValueError- If downsample is too small.
get_downsample_scaling_and_level
Desired resolution of the slide. Can be one of:
- Level: integer pyramid level
- Magnification: desired optical magnification
- MPP: microns per pixel
tuple- (downsample_factor (float), scaling_factor (float), best_level (int))- downsample_factor: Factor by which the base level is downsampled.
- scaling_factor: Additional scaling needed at the chosen level.
- best_level: Pyramid level that best matches the resolution.
ValueError- If required slide properties (objective power or MPP) are unavailable.TypeError- If the resolution type is unsupported.
read_region
(x, y) at Level 0.
(width, height) at target resolution.
Resolution level/mpp/magnification.
Unit for the region shape
pixels).
If True, (0,0) is tissue top-left. Else slide top-left.
Region- Object containing the image and metadata for the requested area.
is_supported_file
bool- True if supported, False otherwise.
NotImplementedError- If not implemented by subclass.
close
NotImplementedError- If not implemented by subclass.
__enter__
WSIReader- The reader instance with slide file open.
__exit__
Exception type.
Exception value.
Exception traceback.
get_thumbnail
Max (width, height) for result.
If True, crop to tissue. Else show full slide (incl. white space).
Image.Image- RGB thumbnail image.

