Skip to content

Commit

Permalink
Invalid image size handling (#108)
Browse files Browse the repository at this point in the history
* Override image size if likely to be wrong
  • Loading branch information
erikogabrielsson authored Aug 9, 2023
1 parent fe91d24 commit ca329de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Override image size for labels and overviews if it is likely to be wrong.

### Changed

- Use logger instead of issuing warnings.
Expand Down
19 changes: 17 additions & 2 deletions wsidicom/instance/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,23 @@ def image_size(self) -> Size:
The image size
"""
image_size = Size(self.TotalPixelMatrixColumns, self.TotalPixelMatrixRows)
if image_size.width == 0 or image_size.height == 0:
raise WsiDicomFileError(self.filepath, "Image size is zero")
if image_size.width <= 0 or image_size.height <= 0:
raise WsiDicomError("Image size is zero")
if self.tile_type == TileType.FULL:
expected_tiled_size = image_size.ceil_div(self.tile_size)
if expected_tiled_size.area != self.frame_count:
error = (
f"Image size {image_size} does not match tile size "
f"{self.tile_size} and number of frames {self.frame_count} "
f"for tile type {TileType.FULL}."
)
if self.image_type == ImageType.VOLUME or self.frame_count != 1:
# Be strict on volume images.
raise WsiDicomError(error)
# Labels and overviews are likely to have only one tile.
error += " Overriding image size to tile size."
logging.warn(error)
image_size = self.tile_size
return image_size

@cached_property
Expand Down

0 comments on commit ca329de

Please sign in to comment.