Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing when reading arrays rather than images from tiff files #1423

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Improvements
- Read config values from the environment variables ([#1422](../../pull/1422))
- Optimizing when reading arrays rather than images from tiff files ([#1423](../../pull/1423))

## 1.27.0

Expand Down
2 changes: 1 addition & 1 deletion sources/ometiff/large_image_source_ometiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False,
numpyAllowed=numpyAllowed, sparseFallback=sparseFallback,
**kwargs)
try:
tile = dir.getTile(x, y)
tile = dir.getTile(x, y, asarray=numpyAllowed == 'always')
format = 'JPEG'
if isinstance(tile, PIL.Image.Image):
format = TILE_FORMAT_PIL
Expand Down
2 changes: 1 addition & 1 deletion sources/tiff/large_image_source_tiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False,
allowStyle = False
format = TILE_FORMAT_PIL
else:
tile = dir.getTile(x, y)
tile = dir.getTile(x, y, asarray=numpyAllowed == 'always')
format = 'JPEG'
if isinstance(tile, PIL.Image.Image):
format = TILE_FORMAT_PIL
Expand Down
13 changes: 9 additions & 4 deletions sources/tiff/large_image_source_tiff/tiff_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,16 @@ def imageHeight(self):
def pixelInfo(self):
return self._pixelInfo

def getTile(self, x, y):
def getTile(self, x, y, asarray=False):
"""
Get the complete JPEG image from a tile.

:param x: The column index of the desired tile.
:type x: int
:param y: The row index of the desired tile.
:type y: int
:param asarray: If True, read jpeg compressed images as arrays.
:type asarray: boolean
:return: either a buffer with a JPEG or a PIL image.
:rtype: bytes
:raises: InvalidOperationTiffError or IOTiffError
Expand All @@ -774,11 +776,14 @@ def getTile(self, x, y):
tileNum = self._toTileNum(x, y)

if (not self._tiffInfo.get('istiled') or
self._tiffInfo.get('compression') not in (
libtiff_ctypes.COMPRESSION_JPEG, 33003, 33005, 34712) or
self._tiffInfo.get('compression') not in {
libtiff_ctypes.COMPRESSION_JPEG, 33003, 33005, 34712} or
self._tiffInfo.get('bitspersample') != 8 or
self._tiffInfo.get('sampleformat') not in {
None, libtiff_ctypes.SAMPLEFORMAT_UINT}):
None, libtiff_ctypes.SAMPLEFORMAT_UINT} or
(asarray and self._tiffInfo.get('compression') not in {33003, 33005, 34712} and (
self._tiffInfo.get('compression') != libtiff_ctypes.COMPRESSION_JPEG or
self._tiffInfo.get('photometric') != libtiff_ctypes.PHOTOMETRIC_YCBCR))):
return self._getUncompressedTile(tileNum)

imageBuffer = io.BytesIO()
Expand Down