OpenSlide Binary Distribution for Python

4.0.0.13 · active · verified Thu Apr 16

openslide-bin is a pip-installable, self-contained build of the OpenSlide C library and its essential dependencies for Windows, macOS, and Linux. It is maintained by the OpenSlide project to simplify the installation of the native OpenSlide library, which is required by the `openslide-python` bindings for reading whole-slide images (WSI). The current version is 4.0.0.13, and releases are tied to OpenSlide C library updates and build fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a whole-slide image using `openslide-python` (which leverages the `openslide-bin` installed native library), extract its properties, generate a thumbnail, and read a specific region. Ensure you have a valid WSI file (e.g., .svs) to test.

import os
import openslide
from PIL import Image

# NOTE: For Windows, if you encounter 'DLL load failed' or 'cannot find OpenSlide library',
# you might need to manually specify the path to the openslide-bin 'bin' directory.
# Example: openslide_bin_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'openslide-win64-20231011', 'bin')
# if os.name == 'nt':
#     os.add_dll_directory(openslide_bin_path)

# Placeholder for a whole-slide image file. Replace with your .svs, .ndpi, etc.
# You can download sample slides from openslide.org/demo
slide_path = os.environ.get('OPENSLIDE_SAMPLE_SLIDE_PATH', 'path/to/your/slide.svs')

try:
    with openslide.OpenSlide(slide_path) as slide:
        print(f"Opened slide: {slide_path}")
        print(f"Dimensions (level 0): {slide.dimensions}")
        print(f"Level count: {slide.level_count}")
        print(f"Properties: {slide.properties.keys()}")

        # Get a thumbnail of the slide
        thumbnail = slide.get_thumbnail((500, 500))
        thumbnail.save('slide_thumbnail.png')
        print("Saved thumbnail to slide_thumbnail.png")

        # Read a region from a specific level
        # For example, read a 256x256 region from the highest resolution (level 0)
        # starting at (0, 0) pixel coordinates.
        region_image = slide.read_region((0, 0), 0, (256, 256))
        region_image.save('slide_region_level0.png')
        print("Saved a 256x256 region from level 0 to slide_region_level0.png")

except openslide.OpenSlideError as e:
    print(f"Error opening slide: {e}")
    print("Please ensure the slide path is correct and the file is a supported OpenSlide format.")
except FileNotFoundError:
    print(f"Error: Slide file not found at {slide_path}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →