diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bbfba00..04f01cf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/wsidicom/instance/dataset.py b/wsidicom/instance/dataset.py index 64286890..c83ff32e 100644 --- a/wsidicom/instance/dataset.py +++ b/wsidicom/instance/dataset.py @@ -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