Skip to content

Commit

Permalink
Release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
erikogabrielsson committed Nov 29, 2023
1 parent c0af2e7 commit fa6be33
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 11 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.0] - 2023-11-29

### Added

- Methods for getting rgb and grayscale (8, 12, and 16 bits) Pillow images.

### Changed

- Scale samples to number of bits in output.
- RLE segment are even number of bytes.
- MCT used for jpeg2000 lossless YBR.

## [0.1.0] - 2023-11-22

### Added
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 = "wsidicom-data"
version = "0.1.0"
version = "0.2.0"
description = "Test data for wsidicom"
authors = ["Erik O Gabrielsson <[email protected]>"]
license = "Apache-2.0"
Expand Down
11 changes: 2 additions & 9 deletions wsidicom_data/create_encoded_test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@

import hashlib

from PIL import Image
from wsidicom.codec import (
Channels,
Encoder,
)
from wsidicom.codec import Encoder

from wsidicom_data.test_data import EncodedTestData, TestData, defined_encoder_settings


def create_encoded_test_files():
for settings in defined_encoder_settings:
test_tile_path = TestData.get_test_tile_path()
image = Image.open(test_tile_path)
if settings.channels == Channels.GRAYSCALE:
image = image.convert("L")
image = TestData.image(settings.bits, settings.samples_per_pixel)
encoder = Encoder.create(settings)
encoded = encoder.encode(image)
output_path = EncodedTestData.get_filepath_for_encoder_settings(settings)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified wsidicom_data/data/encoded/RLE_Lossless-GRAYSCALE-16.rle
Binary file not shown.
43 changes: 42 additions & 1 deletion wsidicom_data/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,56 @@
Settings,
Subsampling,
)
from PIL.Image import Image as PILImage
from PIL import Image


class TestData:
test_data_path = Path(__file__).parent.joinpath("data")

@classmethod
def get_test_tile_path(cls):
def get_test_tile_path(cls) -> Path:
return cls.test_data_path.joinpath("test_tile.png")

@classmethod
def rgb(cls) -> PILImage:
return Image.open(cls.get_test_tile_path())

@classmethod
def grayscale_8(cls) -> PILImage:
return cls.rgb().convert("L")

@classmethod
def grayscale_12(cls) -> PILImage:
return cls._scale_bits(cls._grayscale_32(), 8, 12).convert("I;16L")

@classmethod
def grayscale_16(cls) -> PILImage:
return cls._scale_bits(cls._grayscale_32(), 8, 16).convert("I;16L")

@classmethod
def image(cls, bits: int, samples_per_pixel: int) -> PILImage:
if samples_per_pixel == 1:
if bits == 8:
return cls.grayscale_8()
elif bits == 12:
return cls.grayscale_12()
elif bits == 16:
return cls.grayscale_16()
elif samples_per_pixel == 3 and bits == 8:
return cls.rgb()
raise ValueError(
f"Unsupported bit depth {bits} or samples_per_pixel {samples_per_pixel}."
)

@classmethod
def _grayscale_32(cls) -> PILImage:
return cls.rgb().convert("I")

@classmethod
def _scale_bits(cls, image: PILImage, from_bits: int, to_bits: int) -> PILImage:
return image.point(lambda x: 2 ** (to_bits - from_bits) * x)


class EncodedTestData:
test_data_path = Path(__file__).parent.joinpath("data", "encoded")
Expand Down

0 comments on commit fa6be33

Please sign in to comment.