Skip to main content

opentile

opentile is a Python library for reading tiles from wsi tiff files. The aims of the project are:

  • Allow compressed tiles to be losslessly read from WSI TIFFS using 2D coordinates (tile position x, y),
  • Provide a unified interface for relevant metadata,
  • Support file formats supported by tifffile that have a non-overlapping tile structure, as well as formats whose tiles overlap their neighbours (Trestle, Ventana). These are re-gridded losslessly where possible, and otherwise the de-overlapped tile placement is exposed.

opentile does not provide methods for reading regions from images (e.g. get_region()). See openslide-python, tiffslide, or wsidicomizer for such use.

Currently implemented file formats are listed and described under Implemented file formats.

Installing opentile

opentile is available on PyPI:

pip install opentile

Alternatively, it can be installed via conda:

conda install -c conda-forge opentile

opentile-jpeg

This repository also contains opentile-jpeg, the lossless JPEG operations (cropping, re-tiling, filling, optimizing) that opentile uses. It is a separate package with its own release, and includes libjpeg-turbo, so no system install is needed.

Important note

Please note that this is an early release and the API is not frozen yet. Function names and functionality are prone to change.

Requirements

opentile requires python >=3.12 and uses numpy, Pillow, TiffFile, opentile-jpeg, imagecodecs, defusedxml, and ome-types.

The minimum supported Python and dependency versions follow SPEC 0.

Limitations

Files with z-stacks are currently not fully supported for all formats.

Implemented file formats

The following description of the workings of the implemented file formats does not include the additional specifics for each format that is handled by tifffile. Additional formats supported by tifffile are likely to be added in future releases.

opentile presents each level as a regular, non-overlapping grid of tile_size tiles. When TiffImage.overlap is None (the common case) the stored tiles already form that regular grid and are served directly. When it instead returns a TileOverlap — because the stored tiles overlap their neighbors (Ventana, and Trestle levels that can not be re-gridded) or use a different native tiling (JPEG XR Ndpi) — the raw stored tiles are still served by grid position, and the TileOverlap gives the composed level size and the placement of each stored tile, so a consumer can compose (de-overlap and/or stitch) them into the regular grid.

Hamamatsu Ndpi The Ndpi-format uses non-rectangular tile size typically 8 pixels high, i.e. stripes. To form tiles, first multiple stripes are concatenated to form a frame covering the tile region. Second, if the stripes are longer than the tile width, the tile is cropped out of the frame. The concatenation and crop transformations are performed losslessly.

A ndpi-file can also contain non-tiled images. If these are part of a pyramidal series, opentile tiles the image.

Ndpi levels can also be compressed with JPEG XR instead of jpeg. These native tiles cannot be re-tiled losslessly, so they are served by their native grid position with a zero-overlap TileOverlap (see above) for a consumer to decode and stitch into the regular grid; a coarsest single-frame level is served as one tile. The macro (overview) is served as native JPEG XR, while the label, which is cropped out of the macro, is decoded and served as uncompressed pixels (there is no lossless crop for JPEG XR, and re-encoding would add loss).

The macro page in ndpi-files images the whole slide including label. A label and overview is created by cropping the macro image.

Philips tiff The Philips tiff-format allows tiles to be sparse, i.e. missing. For such tiles, opentile instead provides a blank (currently white) tile image using the same jpeg header as the rest of the image.

Aperio svs Some Aperio svs-files have corrupt tile data at edges of non-base pyramidal levels. This is observed as tiles with 0-byte length and tiles with incorrect pixel data. opentile detects such corruption and instead returns downscaled image data from lower levels.

3DHistech tiff Only the pyramidal levels are supported (not overviews or labels).

Huron tiff Huron (MACROscan) tiff-files are identified by an Image Dimensions = field in the description, which is Aperio-like but does not start with Aperio . The natively tiled levels (JPEG 2000 or jpeg) are served as-is and the pixel spacing is read from the Resolution field. The thumbnail, label, and macro associated images are stored uncompressed and are decoded and served as raw pixels.

Mikroscan tiff Mikroscan (SL5) tiff-files use the Aperio pipe-separated description format but with a Mikroscan Image Structure header instead of the Aperio prefix. The natively tiled jpeg levels are served as-is, and pixel spacing, magnification, scanner model/serial, and acquisition datetime are read from the description. The thumbnail, label, and macro associated images are stored uncompressed; since their series are unnamed, they are identified by the second description line (label, macro, or a -> WxH downscale) and are decoded and served as raw pixels.

Motic tiff Motic tiff-files use the Aperio pipe-separated description format but with a Motic <version> header instead of the Aperio prefix, and are otherwise structured like svs. The natively tiled jpeg levels are served as-is; magnification, pixel spacing, and barcode are read from the description (there is no serial number or acquisition datetime). The jpeg thumbnail and lzw label/macro associated images reuse the svs associated-image handling; since their series are unnamed, they are identified by the second description line (label, macro, or a -> WxH downscale).

OME tiff Both tiled and strip-stored (e.g. uncompressed) levels are supported. Each level's focal plane and optical path are read from the OME metadata, so multi-plane z-stacks are exposed as separate focal planes. General (metadata-property) parsing is not yet implemented.

Trestle tiff Trestle tiff-files are identified by a Software tag starting with MedScan. Their tiles overlap their neighbors by a fixed amount per level, given in the OverlapsXY description field. When the jpeg is baseline 8-bit, and the tile step and tile_size are multiples of the jpeg MCU, a level is re-gridded losslessly into non-overlapping tiles by copying jpeg blocks. A re-gridded level has no overlap. Other levels are served as stored, with an overlap placement. Levels cropped by the base level's overlap give their position on the base level as origin_offset. Associated images are stored in separate files and are not read.

Ventana bif Ventana bif-files store a single-file pyramidal tiled BigTIFF whose tiles overlap. The tile joints and the origin of each scanned area are read from the EncodeInfo XMP, and multi-area slides are supported. Base-level tiles are placed at whole-pixel positions solved from the measured joints to their neighbours. Reduced levels use the base positions scaled by the downsample. Stitching metadata that is missing or not valid, or a layout opentile does not place, raises an error instead of giving a guessed layout. Already-stitched (non-overlapping) Ventana tiff files are read as a plain tiled pyramid.

PerkinElmer/Akoya qptiff Qptiff-files are identified by a Software tag starting with PerkinElmer-QPI, and their directories are classified by the ImageType element of the PerkinElmer-QPI-ImageDescription XML, exposing the thumbnail, macro (overview) and label images. Levels of 2K x 2K pixels or smaller are stored as strips rather than tiles and are served as a single tile, as are the associated images; LZW-compressed strips cannot be concatenated and are served decoded (reported as COMPRESSION.NONE). Brightfield (RGB) and fluorescence files are both supported; fluorescence files store one grayscale image per band and expose each band as its own level image, with the band name (e.g. DAPI) as optical_path. Unmixed multispectral component files (float pixels) are not tested.

Leica SCN Leica SCN-files (identified by the Leica SCN XML in the first page's ImageDescription) store a single-file pyramidal tiled JPEG BigTIFF with a non-standard XML slide description. The tiles do not overlap. The collection's macro image (view offset 0,0 spanning the whole collection) is exposed as the overview; the largest main image is served as the pyramid levels, with mpp derived from its view size (in nm). Leica SCN has no dedicated label image; the macro images the whole slide including the label, so a label is cropped from its lower end. As the macro is tiled the crop is aligned down to a whole tile row, and the tiles are served unchanged. Z-stacks and multi-channel (fluorescence) SCN files are not supported.

Argos avs Argos avs-files (identified by TIFF tag 65000 holding Argos.Scan.Metadata XML) store a single-file pyramidal tiled BigTIFF with sparse JPEG tiles. Missing tiles (zero offset and byte count) are served as blank (white) tiles, as for Philips. The last two directories are the thumbnail (Map) and overview (Macro) images; Argos has no dedicated label image, so the label is cropped from the right side of the overview. Stacked (z-stack) files store the focal planes on the base series' Z axis, and each plane is exposed as a level image with its focal_plane set from the MinZ/ZRange metadata.

Metadata

File metadata can be accessed through the metadata-property of a tiler. Depending on file format and content, the following metadata is available:

  • Magnification
  • Scanner manufacturer
  • Scanner model
  • Scanner software versions
  • Scanner serial number
  • Acquisition datetime

Basic usage

Load a Ndpi-file using tile size (1024, 1024) pixels.

from opentile import OpenTile

tile_size = (1024, 1024)
tiler = OpenTile.open(path_to_ndpi_file, tile_size)

Load a file using fsspec and with some file options.

from opentile import OpenTile
tiler = OpenTile.open("s3://bucket/key", file_options={"s3": "anon": True})

Get rectangular tile at level 0 and position x=0, y=0.

level = tiler.get_evel(0)
tile = level.get_tile((0, 0))

Close the tiler object.

tiler.close()

Usage as context manager

The tiler can also be used as a context manager:

from opentile import OpenTile

tile_size = (1024, 1024)
with OpenTile.open(path_to_ndpi_file, tile_size) as tiler:
    level = tiler.get_evel(0)
    tile = level.get_tile((0, 0))

Setup environment for development

Requires uv installed.

git clone https://github.com/imi-bigpicture/opentile.git
uv sync --all-extras

By default the tests looks for slides in tests/testdata. This can be overridden by setting the OPENTILE_TESTDIR environment variable. The script 'tests/download_test_images.py' can be used to download publicly available openslide testdata into the set testdata folder:

python tests/download_test_images.py

The test data used for philips tiff is currently not publicly available as we don't have permission to share them. If you have slides in philips tiff format that can be freely shared, we would be happy to use them instead.

To watch unit tests use:

uv run pytest-watch -- -m unittest

Other TIFF python tools

Contributing

We welcome any contributions to help improve this tool for the WSI community!

We recommend first creating an issue before creating potential contributions to check that the contribution is in line with the goals of the project. To submit your contribution, please issue a pull request on the imi-bigpicture/opentile repository with your changes for review.

Our aim is to provide constructive and positive code reviews for all submissions. The project relies on gradual typing and roughly follows PEP8. However, we are not dogmatic. Most important is that the code is easy to read and understand.

Acknowledgement

opentile: Copyright 2021-2024 Sectra AB, licensed under Apache 2.0.

This project is part of a project that has received funding from the Innovative Medicines Initiative 2 Joint Undertaking under grant agreement No 945358. This Joint Undertaking receives support from the European Union’s Horizon 2020 research and innovation programme and EFPIA. IMI website: www.imi.europa.eu

Metadata

Release files for opentile 0.27.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opentile 0.27.0
File Size Uploaded Provenance Actions
opentile-0.27.0.tar.gz 652.8 kB Without provenance Details for opentile-0.27.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opentile 0.27.0
File Interpreter ABI Platform Actions
opentile-0.27.0-py3-none-any.whl Python 3 none any Details for opentile-0.27.0-py3-none-any.whl

Total release size: 801.2 kB

Release files / opentile-0.27.0.tar.gz

Download URL opentile-0.27.0.tar.gz
Size 652.8 kB
Tags Source
SHA-256 checksum
How to use checksums
63771ddaaf10d928d88524064b1e245c14677194f49f91ae42410279b042ab45
BLAKE2b-256 checksum
How to use checksums
43bf66b66b8fd091cf45dfad33ecdc6588aba9665817c589ef16de6355348d92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / opentile-0.27.0-py3-none-any.whl

Download URL opentile-0.27.0-py3-none-any.whl
Size 148.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7dc97f9e8c8ec62f7b94ef8e7ea39eb6dd1efdf2dfd22952a5019e0c830c5a0
BLAKE2b-256 checksum
How to use checksums
59fc23e5cbee8b4b89cf7cc0e96025e576793d728c74e8d9d5c15190aa8041a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for opentile 0.27.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.27.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page