Read tiles from wsi-TIFF files
Project description
opentile
opentile is a Python library for reading tiles from wsi tiff files. The aims of the proect are:
- Allow compressed tiles to be losslessly read from wsi tiffs using 2D coordinates (tile position x, y).
- Provide unified interface for relevant metadata.
- Support all file formats supported by tifffile that has a non-overlapping tile structure.
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
Important note
Please note that this is an early release and the API is not frozen yet. Function names and functionality is prone to change.
Requirements
opentile requires python >=3.8 and uses numpy, Pillow, TiffFile, and PyTurboJPEG (with lib-turbojpeg >= 2.1 ), imagecodecs, defusedxml, and ome-types.
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 and that have non-overlapping tile layout are likely to be added in future release.
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 croped 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.
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 Asperio 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. Associated images (label, overview) are currently not handled correctly.
3DHistech tiff Only the pyramidal levels are supported (not overviews or labels).
OME tiff Metadata parsing is not yet implemented.
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})
If turbo jpeg library path is not found.
from opentile import OpenTile
tile_size = (1024, 1024)
turbo_path = 'C:/libjpeg-turbo64/bin/turbojpeg.dll'
tiler = OpenTile.open(path_to_ndpi_file, tile_size, turbo_path)
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 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 poetry and pytest and pytest-watch installed in the virtual environment.
git clone https://github.com/imi-bigpicture/opentile.git
poetry install
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 dont 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:
poetry 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file opentile-0.13.4.tar.gz
.
File metadata
- Download URL: opentile-0.13.4.tar.gz
- Upload date:
- Size: 33.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ae56512ae40a39c55ff115abdaf6f3ca5052393f102646f5bd37768c7376303 |
|
MD5 | 0af4b67dd5e22afab631b15c058799a9 |
|
BLAKE2b-256 | 976ffb2527e3bdd0027a7dc268bab8e3f2554cf8e2b610b54aa8a59041a82f01 |
File details
Details for the file opentile-0.13.4-py3-none-any.whl
.
File metadata
- Download URL: opentile-0.13.4-py3-none-any.whl
- Upload date:
- Size: 55.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f89de52524c231bcbef5119ca0d9901d57a569c365a2e2619ff20a0b4f6693a5 |
|
MD5 | e91c578c6a66790c4ab58c09de302b58 |
|
BLAKE2b-256 | e194440830c1922659d6a358819c70ae8449bc00eff7a6216345ca2c088ea94c |