glasscut package¶
Subpackages¶
Submodules¶
glasscut.exceptions module¶
- exception glasscut.exceptions.GlassCutException[source]¶
Bases:
ExceptionGlassCut custom exception main class
- exception glasscut.exceptions.SlidePropertyError[source]¶
Bases:
GlassCutExceptionRaised when a required slide property is not available.
- exception glasscut.exceptions.BackendError[source]¶
Bases:
GlassCutExceptionRaised when there’s an issue with the slide backend.
- exception glasscut.exceptions.MagnificationError[source]¶
Bases:
GlassCutExceptionRaised when requested magnification is not available.
- exception glasscut.exceptions.TileSizeOrCoordinatesError[source]¶
Bases:
GlassCutExceptionRaised when tile size or coordinates are invalid.
- exception glasscut.exceptions.FilterCompositionError[source]¶
Bases:
GlassCutExceptionRaised when a filter composition for the class is not available
glasscut.tile module¶
- class glasscut.tile.Tile(image, coords, magnification, tissue_detector=None, **kwargs)[source]¶
Bases:
objectProvide Tile object representing a tile generated from a Slide object.
- Parameters:
image (Image.Image) – Image describing the tile
coords (tuple[int, int]) – Coordinates (x, y) of the tile at level 0 (upper-left corner)
magnification (int | float) – Magnification at which the tile was extracted
tissue_detector (TissueDetector | None, optional) – Strategy for tissue detection. Defaults to OtsuTissueDetector.
kwargs (Any)
- __init__(image, coords, magnification, tissue_detector=None, **kwargs)[source]¶
Initialize a Tile.
- Parameters:
image (Image.Image) – The tile image in RGB format
coords (tuple[int, int] | None) – Coordinates (x, y) of the tile at level 0. Can be None for utility tiles.
magnification (int | float | None) – Magnification at which the tile was extracted. Can be None for utility tiles.
tissue_detector (TissueDetector | None, optional) – Strategy for detecting tissue. If None, uses OtsuTissueDetector.
**kwargs (Any) – Optional keyword-only extensions. Supported key:
precomputed_tissue_ratio(float), used to skip recomputing tissue ratio.
- Return type:
None
- set_precomputed_tissue_ratio(tissue_ratio)[source]¶
Set a precomputed tissue ratio to avoid re-running detection.
- Parameters:
tissue_ratio (float)
- Return type:
None
- has_enough_tissue(tissue_threshold=0.2)[source]¶
Check if the tile has enough tissue.
This method checks if the proportion of the detected tissue over the total area of the tile is above a specified threshold (by default 20%).
- Parameters:
tissue_threshold (float, optional) – Number between 0.0 and 1.0 representing the minimum required percentage of tissue over the total area of the image, default is 0.2
- Returns:
enough_tissue – Whether the image has enough tissue, i.e. if the proportion of tissue over the total area of the image is more than
tissue_threshold.- Return type:
- save(path)[source]¶
Save tile at given path.
The format to use is determined from the filename extension (to be compatible to PIL.Image formats). If no extension is provided, the image will be saved in png format.
- Parameters:
path (str or pathlib.Path) – Path to which the tile is saved.
- Return type:
None
glasscut.utils module¶
- glasscut.utils.lazyproperty(f)[source]¶
Decorator like @property, but evaluated only on first access.
Like @property, this can only be used to decorate methods having only a self parameter, and is accessed like an attribute on an instance, i.e. trailing parentheses are not used. Unlike @property, the decorated method is only evaluated on first access; the resulting value is cached and that same value returned on second and later access without re-evaluation of the method.
Like @property, this class produces a data descriptor object, which is stored in the __dict__ of the class under the name of the decorated method (‘fget’ nominally). The cached value is stored in the __dict__ of the instance under that same name.
Because it is a data descriptor (as opposed to a non-data descriptor), its __get__() method is executed on each access of the decorated attribute; the __dict__ item of the same name is “shadowed” by the descriptor.
While this may represent a performance improvement over a property, its greater benefit may be its other characteristics. One common use is to construct collaborator objects, removing that “real work” from the constructor, while still only executing once. It also de-couples client code from any sequencing considerations; if it’s accessed from more than one location, it’s assured it will be ready whenever needed.
A lazyproperty is read-only. There is no counterpart to the optional “setter” (or deleter) behavior of an @property. This is critically important to maintaining its immutability and idempotence guarantees. Attempting to assign to a lazyproperty raises AttributeError unconditionally. The parameter names in the methods below correspond to this usage example:
class Obj(object): @lazyproperty def fget(self): return 'some result' obj = Obj()
Not suitable for wrapping a function (as opposed to a method) because it is not callable.
- glasscut.utils.np_to_pil(np_img)[source]¶
Convert a NumPy array to a PIL Image.
Handles the conversion of different numpy array types (bool, float64, uint8, etc.) to a properly formatted PIL Image.
- Parameters:
np_img (np.ndarray) – The image represented as a NumPy array.
- Returns:
The image represented as PIL Image
- Return type:
Examples
>>> import numpy as np >>> from glasscut.utils import np_to_pil >>> float_array = np.random.rand(100, 100, 3) >>> pil_image = np_to_pil(float_array)
- class glasscut.utils.Profiler(enabled=True)[source]¶
Bases:
objectLightweight phase-based profiler with thread-safe accumulation.
Zero overhead when enabled is
False— all methods return immediately.Example
>>> profiler = Profiler(enabled=True) >>> for i in range(100): ... with profiler.phase("compute"): ... _ = i ** 2 >>> profiler.print_summary()
- Parameters:
enabled (bool)
- enabled¶
Module contents¶
GlassCut: Fast and flexible histopathology image tiling library.
A lightweight, extensible library for extracting tiles from whole slide images (WSI) with support for multiple tiling strategies, and parallel processing.
- Core Components:
- Slide I/O:
Slide: WSI reader with backend abstraction
Tile: Individual tile image with metadata
- Tiling Strategies:
Tiler: Abstract base class for tiling strategies
GridTiler: Regular grid tiling with optional overlap
- Tissue Detection:
OtsuTissueDetector: Otsu method tissue detection
- Stain Normalization:
MacenkoNormalizer: Stain color normalization
ReinhardtNormalizer: Fast color transfer-based normalization
See documentation at: https://github.com/CamiloSinningUN/glasscut
- class glasscut.Slide(path, use_cucim=True)[source]¶
Bases:
objectRepresents 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.
- __exit__(exc_type, exc_val, exc_tb)[source]¶
Context manager exit.
- Parameters:
exc_type (type[BaseException] | None)
exc_val (BaseException | None)
exc_tb (TracebackType | None)
- Return type:
None
- property name: str¶
Slide name without extension.
- Returns:
Slide filename without extension
- Return type:
- 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.
- property mpp: float¶
Microns per pixel at base magnification.
- Returns:
Microns per pixel
- Return type:
- 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:
- 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:
- 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:
- Returns:
Extracted tile object
- Return type:
- Raises:
MagnificationError – If the requested magnification is not available on this slide
TileSizeOrCoordinatesError – If the coordinates or tile size are invalid
- class glasscut.Tile(image, coords, magnification, tissue_detector=None, **kwargs)[source]¶
Bases:
objectProvide Tile object representing a tile generated from a Slide object.
- Parameters:
image (Image.Image) – Image describing the tile
coords (tuple[int, int]) – Coordinates (x, y) of the tile at level 0 (upper-left corner)
magnification (int | float) – Magnification at which the tile was extracted
tissue_detector (TissueDetector | None, optional) – Strategy for tissue detection. Defaults to OtsuTissueDetector.
kwargs (Any)
- __init__(image, coords, magnification, tissue_detector=None, **kwargs)[source]¶
Initialize a Tile.
- Parameters:
image (Image.Image) – The tile image in RGB format
coords (tuple[int, int] | None) – Coordinates (x, y) of the tile at level 0. Can be None for utility tiles.
magnification (int | float | None) – Magnification at which the tile was extracted. Can be None for utility tiles.
tissue_detector (TissueDetector | None, optional) – Strategy for detecting tissue. If None, uses OtsuTissueDetector.
**kwargs (Any) – Optional keyword-only extensions. Supported key:
precomputed_tissue_ratio(float), used to skip recomputing tissue ratio.
- Return type:
None
- set_precomputed_tissue_ratio(tissue_ratio)[source]¶
Set a precomputed tissue ratio to avoid re-running detection.
- Parameters:
tissue_ratio (float)
- Return type:
None
- has_enough_tissue(tissue_threshold=0.2)[source]¶
Check if the tile has enough tissue.
This method checks if the proportion of the detected tissue over the total area of the tile is above a specified threshold (by default 20%).
- Parameters:
tissue_threshold (float, optional) – Number between 0.0 and 1.0 representing the minimum required percentage of tissue over the total area of the image, default is 0.2
- Returns:
enough_tissue – Whether the image has enough tissue, i.e. if the proportion of tissue over the total area of the image is more than
tissue_threshold.- Return type:
- save(path)[source]¶
Save tile at given path.
The format to use is determined from the filename extension (to be compatible to PIL.Image formats). If no extension is provided, the image will be saved in png format.
- Parameters:
path (str or pathlib.Path) – Path to which the tile is saved.
- Return type:
None
- class glasscut.Tiler[source]¶
Bases:
ABCAbstract base class for tile extraction strategies.
A Tiler is responsible for determining which tiles to extract from a slide and providing them to the user. Different tiling strategies can be implemented by subclassing this class.
- abstractmethod extract(slide, *, n_workers=4, batch_size=128)[source]¶
Extract tiles from slide.
This is the primary extraction API for all tilers. Implementations can use batching and parallelism internally.
- Parameters:
- Yields:
Tile – Individual tile objects with image, coordinates, and metadata
- Raises:
MagnificationError – If the requested magnification is not available on this slide
TileSizeOrCoordinatesError – If generated coordinates are invalid for the slide
Example: –
>>> slide = Slide("slide.svs") >>> tiler = GridTiler(tile_size=(512, 512), overlap=50) >>> for tile in tiler.extract(slide): ... tile.save(f"tile_{tile.coords}.png")
- Return type:
- abstractmethod get_tile_boxes(slide)[source]¶
Get all tile boxes without extracting images.
This method computes which tile regions would be extracted without actually reading images from the slide. Useful for planning, filtering, or batch processing.
- Parameters:
slide (Slide) – The slide object
- Returns:
list[tuple[int, int, int, int]] – List of tile boxes as
(x, y, width, height)in level-0 space.Example – >>> tiler = GridTiler(tile_size=(512, 512)) >>> boxes = tiler.get_tile_boxes(slide) >>> print(f”Will extract {len(boxes)} tiles”)
- Return type:
- visualize(slide, scale_factor=32, colors=None, linewidth=1)[source]¶
Visualize tile grid on a slide thumbnail.
This method creates a thumbnail of the slide and draws the tile grid on top of it. Useful for verifying tiling strategy before processing.
- Parameters:
slide (Slide) – The slide object to visualize
scale_factor (int, optional) – Scale factor for thumbnail downsampling. Default is 32.
colors (list[tuple[int, int, int]] | None, optional) – RGB colors for tile rectangles. If None, uses a cycle of colors. Default is None.
alpha (int, optional) – Transparency alpha value for rectangles (0-255). Default is 200.
linewidth (int, optional) – Width of rectangle lines in pixels. Default is 1.
- Returns:
PIL.Image.Image – Thumbnail image with tile grid drawn on it
Example – >>> tiler = GridTiler(tile_size=(512, 512)) >>> viz_image = tiler.visualize(slide) >>> viz_image.show()
- Return type:
- class glasscut.GridTiler(tile_size=(512, 512), magnification=20, overlap=0, min_tissue_ratio=0.2, tissue_detector=None, transforms=None, show_progress=True, debug=False)[source]¶
Bases:
TilerExtract tiles using a regular grid.
- Parameters:
tile_size (tuple[int, int], optional) – Default tile size as
(width, height)in pixels at requested magnification. Default is(512, 512).magnification (int | float, optional) – Magnification used for extraction and coordinate generation. Default is
20.overlap (int, optional) – Overlap between neighboring tiles in pixels at requested magnification. Default is
0.min_tissue_ratio (float, optional) – Minimum tissue ratio in
[0.0, 1.0]required for preselection. Default is0.2.tissue_detector (TissueDetector | None, optional) – Tissue detector used for preselection mask. Defaults to OtsuTissueDetector.
show_progress (bool, optional) – Whether to display a loading bar while extracting tiles. Default is True.
debug (bool, optional) – When True, record and print per-phase timing breakdown (tissue mask, candidate grid, tile extraction, transforms). Default is False.
- __init__(tile_size=(512, 512), magnification=20, overlap=0, min_tissue_ratio=0.2, tissue_detector=None, transforms=None, show_progress=True, debug=False)[source]¶
- get_tile_candidates(slide)[source]¶
Return preselected boxes with tissue ratio as
(x, y, w, h, ratio).
- class glasscut.OtsuTissueDetector[source]¶
Bases:
TissueDetectorOtsu-based tissue detection
This detector applies Otsu thresholding with optional morphological operations. It’s fast, robust, and works well for standard histopathology images.
- class glasscut.DatasetGenerator(dataset_id, output_dir, *, tiler, n_workers=4, batch_size=128, save_thumbnails=True, save_masks=True, save_processed_json=True, show_progress=True, verbose=True)[source]¶
Bases:
objectGenerate a tile dataset from one or more slide files.
- Parameters:
- __init__(dataset_id, output_dir, *, tiler, n_workers=4, batch_size=128, save_thumbnails=True, save_masks=True, save_processed_json=True, show_progress=True, verbose=True)[source]¶
Initialize generator from direct parameters.
- Parameters:
dataset_id (str) – Dataset identifier.
output_dir (str | Path) – Output root directory.
tiler (Tiler) – Preconfigured tiler instance used for extraction.
n_workers (int, optional) – Number of workers for batched tile extraction. Default is
4.batch_size (int, optional) – Number of tiles per extraction batch. Default is
128.save_thumbnails (bool, optional) – Whether to save slide thumbnail artifacts.
save_masks (bool, optional) – Whether to save tissue mask artifacts.
save_processed_json (bool, optional) – Whether to save
processed.jsonat dataset root.show_progress (bool, optional) – Whether to display progress bars for slides and tiles.
verbose (bool, optional) – Whether to enable info-level logs.
- Return type:
None
- class glasscut.LiveSlideDataset(slide_paths, *, tiler, n_workers=4, batch_size=128, use_cucim=True)[source]¶
Bases:
objectSlide-level in-memory dataset.
Each
__getitem__call opens one slide, extracts all tiles in memory using the configured tiler, and returns aLiveSlideSample.- Parameters:
- class glasscut.LiveSlideSample(slide_id, slide_name, slide_path, dimensions, mpp, magnifications, tiles)[source]¶
Bases:
objectContainer for one in-memory slide sample.
- Parameters:
- class glasscut.MacenkoStainNormalizer[source]¶
Bases:
TransformerStainMatrixMixin,StainNormalizerStain normalizer using M. Macenko et al.’s method.
This method performs unsupervised color deconvolution to identify stain vectors, then normalizes stain concentrations to a reference image. It works well for standard H&E stained histopathology images.
The algorithm: 1. Converts image to optical density (OD) space 2. Performs principal component analysis on OD values 3. Identifies stain vectors using angular decomposition 4. Normalizes stain concentrations to match reference
Examples
>>> from PIL import Image >>> from glasscut.stain_normalizers import MacenkoStainNormalizer >>> normalizer = MacenkoStainNormalizer() >>> ref_image = Image.open("reference.png") >>> normalizer.fit(ref_image) >>> test_image = Image.open("test.png") >>> normalized_image = normalizer.transform(test_image)
- stain_color_map = {'dab': array([0.27, 0.57, 0.78]), 'eosin': array([0.07, 0.99, 0.11]), 'hematoxylin': array([0.65, 0.7 , 0.29]), 'null': array([0., 0., 0.])}¶
- stain_matrix(img_rgb, background_intensity=240, **kwargs)[source]¶
Estimate stain matrix using Macenko’s method.
- Parameters:
img_rgb (PIL.Image.Image) – Input image in RGB or RGBA format.
background_intensity (int, optional) – Background transmitted light intensity. Default is 240.
**kwargs – Additional keyword arguments.
alpha (int, optional) – Minimum angular percentile. Default is 1.
beta (float, optional) – Threshold for OD magnitude filtering. Default is 0.15.
stains (list of str, optional) – List of stain names in order. Default is
["hematoxylin", "eosin"].
- Returns:
Calculated 3×3 stain matrix with stain vectors as columns.
- Return type:
np.ndarray
- Raises:
ValueError – If stains is not a 2-element list.
ValueError – If image is not RGB or RGBA.
- class glasscut.ReinhardtStainNormalizer[source]¶
Bases:
StainNormalizerStain normalizer using E. Reinhardt et al.’s color transfer method.
This method normalizes stain appearance by matching the mean and standard deviation of each channel in LAB color space between source and target images. The normalization is performed only on tissue regions.
The algorithm is: 1. Identify tissue using tissue masking 2. Convert to LAB color space 3. Compute per-channel mean and std on tissue 4. Normalize source statistics to match target statistics 5. Convert back to RGB
- target_means¶
Target mean values per LAB channel.
- Type:
np.ndarray or None
- target_stds¶
Target standard deviation values per LAB channel.
- Type:
np.ndarray or None
Notes
This method is computationally fast and suitable for real-time preview during stain normalization parameter tuning. However, it may not preserve color relationships as well as matrix-based methods for complex stains.
Examples
>>> from PIL import Image >>> from glasscut.stain_normalizers import ReinhardtStainNormalizer >>> normalizer = ReinhardtStainNormalizer() >>> ref_image = Image.open("reference.png") >>> normalizer.fit(ref_image) >>> test_image = Image.open("test.png") >>> normalized_image = normalizer.transform(test_image)
- fit(target_image, **kwargs)[source]¶
Fit stain normalizer using target image.
- Parameters:
target_image (Image.Image) – Target image for stain normalization. Can be RGB or RGBA.
**kwargs – Additional arguments (unused for Reinhardt method).
- Return type:
None
- transform(image, **kwargs)[source]¶
Normalize staining of image.
- Parameters:
image (Image.Image) – Image to normalize. Can be RGB or RGBA.
**kwargs – Additional arguments (unused for Reinhardt method).
- Returns:
Image with normalized stain.
- Return type:
Image.Image
- static rgb_to_lab(img_rgb)[source]¶
Convert RGB image to LAB color space.
- Parameters:
img_rgb (Image.Image) – Input image in RGB or RGBA format.
- Returns:
Array representation of the image in LAB space.
- Return type:
np.ndarray
- Raises:
ValueError – If the input image is grayscale.