Skip to content

Commit

Permalink
Merge pull request #924 from girder/pil-tiff-orientation
Browse files Browse the repository at this point in the history
Better handle tiffs with orientation flags in pil source.
  • Loading branch information
manthey authored Aug 10, 2022
2 parents e89ad59 + 2566f40 commit f2fc24d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions sources/pil/large_image_source_pil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ def __init__(self, path, maxSize=None, **kwargs):
if not os.path.isfile(largeImagePath):
raise TileSourceFileNotFoundError(largeImagePath) from None
raise TileSourceError('File cannot be opened via PIL.')
minwh = min(self._pilImage.width, self._pilImage.height)
maxwh = max(self._pilImage.width, self._pilImage.height)
# Throw an exception if too small or big before processing further
if minwh <= 0:
raise TileSourceError('PIL tile size is invalid.')
maxWidth, maxHeight = getMaxSize(maxSize, self.defaultMaxSize())
if maxwh > max(maxWidth, maxHeight):
raise TileSourceError('PIL tile size is too large.')
# If the rotation flag exists, loading the image may change the width
# and height
if getattr(self._pilImage, '_tile_orientation', None) not in {None, 1}:
self._pilImage.load()
# If this is encoded as a 32-bit integer or a 32-bit float, convert it
# to an 8-bit integer. This expects the source value to either have a
# maximum of 1, 2^8-1, 2^16-1, 2^24-1, or 2^32-1, and scales it to
Expand All @@ -133,10 +145,7 @@ def __init__(self, path, maxSize=None, **kwargs):
self.tileWidth = self.sizeX
self.tileHeight = self.sizeY
self.levels = 1
# Throw an exception if too big
if self.tileWidth <= 0 or self.tileHeight <= 0:
raise TileSourceError('PIL tile size is invalid.')
maxWidth, maxHeight = getMaxSize(maxSize, self.defaultMaxSize())
# Throw an exception if too big after processing
if self.tileWidth > maxWidth or self.tileHeight > maxHeight:
raise TileSourceError('PIL tile size is too large.')

Expand Down

0 comments on commit f2fc24d

Please sign in to comment.