Skip to content

Commit

Permalink
Compressed size (#84)
Browse files Browse the repository at this point in the history
* Add property for compressed size of image

* Change from cytomine to openslide test data

* Revert to installed turbojpeg
  • Loading branch information
erikogabrielsson authored Jan 29, 2025
1 parent 0e6d043 commit 6e5469d
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/pytests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ jobs:
- name: Set up Ubuntu
if: matrix.os == 'ubuntu-latest'
run: sudo apt-get install -y libturbojpeg
- name: Set up libjpegturbo OSX
if: matrix.os == 'macos-latest'
run: brew unlink jpeg-turbo && brew install jpeg-turbo --HEAD
- name: Set up libjpegturbo Windows
shell: pwsh
if: matrix.os == 'windows-latest'
Expand Down
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] -
## [Unreleased]

## [0.14.0] - 2025-01-29

### Added

- Property `compressed_size` returning the size of all frames, with jpeg headers if separated.

## [0.13.4] - 2024-11-06

Expand Down Expand Up @@ -231,7 +237,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial release of opentile.

[Unreleased]: https://github.com/imi-bigpicture/opentile/compare/v0.13.4..HEAD
[Unreleased]: https://github.com/imi-bigpicture/opentile/compare/v0.14.0..HEAD
[0.14.0]: https://github.com/imi-bigpicture/opentile/compare/v0.13.3..v0.14.0
[0.13.4]: https://github.com/imi-bigpicture/opentile/compare/v0.13.2..v0.13.3
[0.13.3]: https://github.com/imi-bigpicture/opentile/compare/v0.13.2..v0.13.3
[0.13.2]: https://github.com/imi-bigpicture/opentile/compare/v0.13.1..v0.13.2
Expand Down
2 changes: 1 addition & 1 deletion opentile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
from opentile.opentile import OpenTile
from opentile.metadata import Metadata

__version__ = "0.13.4"
__version__ = "0.14.0"
13 changes: 13 additions & 0 deletions opentile/tiff_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Base image classes."""

from functools import cached_property
import math
from abc import ABCMeta, abstractmethod
from pathlib import Path
Expand Down Expand Up @@ -165,6 +166,18 @@ def pyramid_index(self) -> int:
images not in pyramidal series."""
return self._pyramid_index

@cached_property
def compressed_size(self) -> int:
"""Return the size of the compressed image data."""
frames = sum(self._page.databytecounts)
if self._page.jpegheader is not None:
jpeg_header_length = len(self._page.jpegheader)
elif self._page.jpegtables is not None:
jpeg_header_length = len(self._page.jpegtables)
else:
jpeg_header_length = 0
return frames + len(self._page.dataoffsets) * jpeg_header_length

@abstractmethod
def get_tile(self, tile_position: Tuple[int, int]) -> bytes:
"""Should return image bytes for tile at tile position.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "opentile"
version = "0.13.4"
version = "0.14.0"
description = "Read tiles from wsi-TIFF files"
authors = ["Erik O Gabrielsson <erik.o.gabrielsson@sectra.com>"]
license = "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions tests/download_test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

FILES: Dict[str, Dict[str, Any]] = {
"svs/CMU-1/CMU-1.svs": {
"url": "https://data.cytomine.coop/open/openslide/aperio-svs/CMU-1.svs", # NOQA
"url": "https://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/CMU-1.svs", # NOQA
"md5": {"CMU-1.svs": "751b0b86a3c5ff4dfc8567cf24daaa85"},
},
"ndpi/CMU-1/CMU-1.ndpi": {
"url": "https://data.cytomine.coop/open/openslide/hamamatsu-ndpi/CMU-1.ndpi", # NOQA
"url": "https://openslide.cs.cmu.edu/download/openslide-testdata/Hamamatsu/CMU-1.ndpi", # NOQA
"md5": {"CMU-1.ndpi": "fb89dea54f85fb112e418a3cf4c7888a"},
},
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_histech_tiff_tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ def test_sumples_per_pixel(self, level: TiffImage):

# Assert
assert samples_per_pixel == 3

def test_compressed_size(self, level: TiffImage):
# Arrange

# Act
compressed_size = level.compressed_size

# Assert
assert compressed_size == 425684721
9 changes: 9 additions & 0 deletions tests/test_ndpi_tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,12 @@ def test_overview(self, tiler: NdpiTiler):

# Assert
assert md5(overview).hexdigest() == "c663698334b10cd57484a2d503b3bafa"

def test_compressed_size(self, level: TiffImage):
# Arrange

# Act
compressed_size = level.compressed_size

# Assert
assert compressed_size == 262256667
9 changes: 9 additions & 0 deletions tests/test_ome_tiff_tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ def test_sumples_per_pixel(self, level: TiffImage):

# Assert
assert samples_per_pixel == 3

def test_compressed_size(self, level: TiffImage):
# Arrange

# Act
compressed_size = level.compressed_size

# Assert
assert compressed_size == 104115549
9 changes: 9 additions & 0 deletions tests/test_philips_tiff_tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,12 @@ def test_metadata_aquisition_datetime(self, tiler: PhilipsTiffTiler):

# Assert
assert aquisition_datetime == datetime(2013, 7, 1, 18, 59, 4)

def test_compressed_size(self, level: TiffImage):
# Arrange

# Act
compressed_size = level.compressed_size

# Assert
assert compressed_size == 486105413
10 changes: 10 additions & 0 deletions tests/test_svs_tiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from opentile.formats import SvsTiler
from opentile.formats.svs.svs_image import SvsTiledImage
from opentile.tiff_image import TiffImage
from opentile.geometry import Point

from .filepaths import svs_file_path
Expand Down Expand Up @@ -181,3 +182,12 @@ def test_metadata_aquisition_datetime(self, tiler: SvsTiler):

# Assert
assert aquisition_datetime == datetime(2009, 12, 29, 9, 59, 15)

def test_compressed_size(self, level: TiffImage):
# Arrange

# Act
compressed_size = level.compressed_size

# Assert
assert compressed_size == 163831603

0 comments on commit 6e5469d

Please sign in to comment.