OpenSlide Python is a Python interface to the OpenSlide library.
OpenSlide is a C library that provides a simple interface for reading whole-slide images, also known as virtual slides, which are high-resolution images used in digital pathology. These images can occupy tens of gigabytes when uncompressed, and so cannot be easily read using standard tools or libraries, which are designed for images that can be comfortably uncompressed into RAM. Whole-slide images are typically multi-resolution; OpenSlide allows reading a small amount of image data at the resolution closest to a desired zoom level.
OpenSlide can read virtual slides in several formats:
OpenSlide Python is released under the terms of the GNU Lesser General Public License, version 2.1.
An open whole-slide image.
If any operation on the object fails, OpenSlideError is raised. OpenSlide has latching error semantics: once OpenSlideError is raised, all future operations on the OpenSlide, other than close(), will also raise OpenSlideError.
close() is called automatically when the object is deleted. The object may be used as a context manager, in which case it will be closed upon exiting the context.
Parameters: | filename (str) – the file to open |
---|---|
Raises: |
|
Return a string describing the format vendor of the specified file. This string is also accessible via the PROPERTY_NAME_VENDOR property.
If the file is not recognized, return None.
Parameters: | filename (str) – the file to examine |
---|
The number of levels in the slide. Levels are numbered from 0 (highest resolution) to level_count - 1 (lowest resolution).
A (width, height) tuple for level 0 of the slide.
A list of (width, height) tuples, one for each level of the slide. level_dimensions[k] are the dimensions of level k.
A list of downsample factors for each level of the slide. level_downsamples[k] is the downsample factor of level k.
Metadata about the slide, in the form of a Mapping from OpenSlide property name to property value. Property values are always strings. OpenSlide provides some standard properties, plus additional properties that vary by slide format.
Images, such as label or macro images, which are associated with this slide. This is a Mapping from image name to RGBA Image.
Unlike in the C interface, these images are not premultiplied.
Return an RGBA Image containing the contents of the specified region.
Unlike in the C interface, the image data is not premultiplied.
Parameters: |
---|
Return the best level for displaying the given downsample.
Parameters: | downsample (float) – the desired downsample factor |
---|
Return an Image containing an RGB thumbnail of the slide.
Parameters: | size (tuple) – the maximum size of the thumbnail as a (width, height) tuple |
---|
Close the OpenSlide object.
The openslide module provides attributes containing the names of some commonly-used OpenSlide properties.
The name of the property containing a slide’s comment, if any.
The name of the property containing an identification of the vendor.
The name of the property containing the “quickhash-1” sum.
The name of the property containing a slide’s background color, if any. It is represented as an RGB hex triplet.
The name of the property containing a slide’s objective power, if known.
The name of the property containing the number of microns per pixel in the X dimension of level 0, if known.
The name of the property containing the number of microns per pixel in the Y dimension of level 0, if known.
The name of the property containing the X coordinate of the rectangle bounding the non-empty region of the slide, if available.
The name of the property containing the Y coordinate of the rectangle bounding the non-empty region of the slide, if available.
The name of the property containing the width of the rectangle bounding the non-empty region of the slide, if available.
The name of the property containing the height of the rectangle bounding the non-empty region of the slide, if available.
An error produced by the OpenSlide library.
Once OpenSlideError has been raised by a particular OpenSlide, all future operations on that OpenSlide (other than close()) will also raise OpenSlideError.
OpenSlide does not support the requested file. Subclass of OpenSlideError.
A wrapper around an Image object that provides an OpenSlide-compatible API.
Parameters: | file – a filename or Image object |
---|---|
Raises IOError: | if the file cannot be opened |
Return an OpenSlide for whole-slide images and an ImageSlide for other types of images.
Parameters: | filename (str) – the file to open |
---|---|
Raises: |
|
OpenSlide Python provides functionality for generating individual Deep Zoom tiles from slide objects. This is useful for displaying whole-slide images in a web browser without converting the entire slide to Deep Zoom or a similar format.
A Deep Zoom generator that wraps an OpenSlide or ImageSlide object.
Parameters: |
|
---|
The number of Deep Zoom levels in the image.
The total number of Deep Zoom tiles in the image.
A list of (tiles_x, tiles_y) tuples for each Deep Zoom level. level_tiles[k] are the tile counts of level k.
A list of (pixels_x, pixels_y) tuples for each Deep Zoom level. level_dimensions[k] are the dimensions of level k.
Return a string containing the XML metadata for the Deep Zoom .dzi file.
Parameters: | format (str) – the delivery format of the individual tiles (png or jpeg) |
---|
Return the OpenSlide.read_region() arguments corresponding to the specified tile.
Most applications should use get_tile() instead.
Parameters: |
---|
Several Deep Zoom examples are included with OpenSlide Python:
A program to generate and store a complete Deep Zoom directory tree for a slide. It can optionally store an HTML page with a zoomable slide viewer, a list of slide properties, and the ability to view associated images.
This program is intended as an example. If you need to generate Deep Zoom trees for production applications, consider using VIPS instead.