glasscut.slides package

Subpackages

Submodules

glasscut.slides.slide module

Main Slide class for WSI manipulation.

class glasscut.slides.slide.Slide(path, use_cucim=True)[source]

Bases: object

Represents a whole slide image with magnification-based access.

This class provides an interface to access whole slide images. It abstracts away the backend (OpenSlide or cuCim).

Parameters:
  • path (Union[str, pathlib.Path]) – Path to the slide file

  • use_cucim (bool, optional) – Whether to try using cuCim GPU backend. If False or cuCim is not available, falls back to OpenSlide. Default is True.

__init__(path, use_cucim=True)[source]
Parameters:
Return type:

None

__enter__()[source]

Context manager entry.

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit.

Parameters:
Return type:

None

close()[source]

Close the slide and free resources.

Return type:

None

property name: str

Slide name without extension.

Returns:

Slide filename without extension

Return type:

str

property dimensions: tuple[int, int]

Slide dimensions (width, height) at base magnification.

Returns:

(width, height) in pixels at highest magnification (typically 40x)

Return type:

tuple[int, int]

property magnifications: list[float]

Available magnifications for this slide.

These are calculated from the actual slide’s base magnification (objective power) and the number of pyramid levels.

Returns:

List of magnifications in descending order (e.g., [40.0, 20.0, 10.0, 5.0])

Return type:

list[float]

property mpp: float

Microns per pixel at base magnification.

Returns:

Microns per pixel

Return type:

float

Raises:

SlidelazypropertyError – If MPP cannot be determined from slide metadata

property properties: dict[str, str]

Slide metadata properties.

Returns:

Dictionary of all slide properties

Return type:

dict

property thumbnail: Image

Get thumbnail of the slide.

The thumbnail size is automatically calculated based on slide dimensions.

Returns:

Thumbnail image in RGB format

Return type:

PIL.Image.Image

extract_tile(coords, tile_size, magnification)[source]

Extract a single tile from the slide at specified magnification.

The requested magnification must be available on this slide. If the exact magnification is not available, a MagnificationError is raised.

Parameters:
  • coords (tuple[int, int]) – Coordinates (x, y) at level 0 (upper-left corner of the tile)

  • tile_size (tuple[int, int]) – Desired tile size (width, height) in pixels

  • magnification (int | float) – Target magnification (e.g., 40, 20, 10, 5)

Returns:

Extracted tile object

Return type:

Tile

Raises:

glasscut.slides.utils module

glasscut.slides.utils.build_magnification_mapping(base_magnification, num_levels)[source]

Build a list of available magnifications from pyramid levels.

Parameters:
  • base_magnification (int | float) – The magnification at level 0 (e.g., 40, 20)

  • num_levels (int) – Number of pyramid levels in the slide

Returns:

List of magnifications in descending order (e.g., [40.0, 20.0, 10.0, 5.0])

Return type:

list[int | float]

glasscut.slides.utils.magnification_to_level(magnification, available_magnifications)[source]

Convert magnification to the pyramid level.

This function performs EXACT matching - the requested magnification must be available in the slide, otherwise an error is raised.

Parameters:
  • magnification (int | float) – Target magnification (e.g., 40, 20, 10, 5)

  • available_magnifications (list[int | float]) – List of available magnifications for the slide (in descending order)

Returns:

The level index corresponding to the magnification

Return type:

int

Raises:

MagnificationError – If the requested magnification is not available on this slide

Module contents

Slide reading and manipulation module.

class glasscut.slides.Slide(path, use_cucim=True)[source]

Bases: object

Represents a whole slide image with magnification-based access.

This class provides an interface to access whole slide images. It abstracts away the backend (OpenSlide or cuCim).

Parameters:
  • path (Union[str, pathlib.Path]) – Path to the slide file

  • use_cucim (bool, optional) – Whether to try using cuCim GPU backend. If False or cuCim is not available, falls back to OpenSlide. Default is True.

__init__(path, use_cucim=True)[source]
Parameters:
Return type:

None

__enter__()[source]

Context manager entry.

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit.

Parameters:
Return type:

None

close()[source]

Close the slide and free resources.

Return type:

None

property name: str

Slide name without extension.

Returns:

Slide filename without extension

Return type:

str

property dimensions: tuple[int, int]

Slide dimensions (width, height) at base magnification.

Returns:

(width, height) in pixels at highest magnification (typically 40x)

Return type:

tuple[int, int]

property magnifications: list[float]

Available magnifications for this slide.

These are calculated from the actual slide’s base magnification (objective power) and the number of pyramid levels.

Returns:

List of magnifications in descending order (e.g., [40.0, 20.0, 10.0, 5.0])

Return type:

list[float]

property mpp: float

Microns per pixel at base magnification.

Returns:

Microns per pixel

Return type:

float

Raises:

SlidelazypropertyError – If MPP cannot be determined from slide metadata

property properties: dict[str, str]

Slide metadata properties.

Returns:

Dictionary of all slide properties

Return type:

dict

property thumbnail: Image

Get thumbnail of the slide.

The thumbnail size is automatically calculated based on slide dimensions.

Returns:

Thumbnail image in RGB format

Return type:

PIL.Image.Image

extract_tile(coords, tile_size, magnification)[source]

Extract a single tile from the slide at specified magnification.

The requested magnification must be available on this slide. If the exact magnification is not available, a MagnificationError is raised.

Parameters:
  • coords (tuple[int, int]) – Coordinates (x, y) at level 0 (upper-left corner of the tile)

  • tile_size (tuple[int, int]) – Desired tile size (width, height) in pixels

  • magnification (int | float) – Target magnification (e.g., 40, 20, 10, 5)

Returns:

Extracted tile object

Return type:

Tile

Raises: