Skip to content

Commit

Permalink
Merge pull request #1399 from girder/harden-readers
Browse files Browse the repository at this point in the history
Improve checks for formats we shouldn't read
  • Loading branch information
manthey authored Dec 6, 2023
2 parents 2c33d35 + 83d5b09 commit 018fef8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Improvements
- Reduce stderr noise in PIL and rasterio sources ([#1397](../../pull/1397))
- Harden OME tiff reader ([#1398](../../pull/1398))
- Improve checks for formats we shouldn't read ([#1399](../../pull/1399))

### Changes
- Use an enum for priority constants ([#1400](../../pull/1400))
Expand Down
2 changes: 1 addition & 1 deletion docs/config_options.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Configuration Options
=====================

Some functionality of large_image is controlled through configuration parameters. These can be read or set via python using functions in the ``large_image.config`` module, `getConfig <./large_image/large_image.html#large_image.config.getConfig>`_ and `setConfig <./large_image/large_image.html#large_image.config.setConfig>`_.
Some functionality of large_image is controlled through configuration parameters. These can be read or set via python using functions in the ``large_image.config`` module, `getConfig <./_build/large_image/large_image.html#large_image.config.getConfig>`_ and `setConfig <./_build/large_image/large_image.html#large_image.config.setConfig>`_.

Configuration parameters:

Expand Down
24 changes: 12 additions & 12 deletions sources/pil/large_image_source_pil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#############################################################################

import contextlib
import json
import math
import os
Expand Down Expand Up @@ -202,18 +203,17 @@ def _fromRawpy(self, largeImagePath):
try:
import rawpy

rgb = rawpy._rawpy.RawPy(1)
rgb.open_file(largeImagePath)
rgb = rgb.postprocess()
rgb = large_image.tilesource.utilities._imageToNumpy(rgb)[0]
if rgb.shape[2] == 2:
rgb = rgb[:, :, :1]
elif rgb.shape[2] > 3:
rgb = rgb[:, :, :3]
self._pilImage = PIL.Image.fromarray(
rgb.astype(np.uint8) if rgb.dtype != np.uint16 else rgb,
('RGB' if rgb.dtype != np.uint16 else 'RGB;16') if rgb.shape[2] == 3 else
('L' if rgb.dtype != np.uint16 else 'L;16'))
with contextlib.redirect_stderr(open(os.devnull, 'w')):
rgb = rawpy.imread(largeImagePath).postprocess()
rgb = large_image.tilesource.utilities._imageToNumpy(rgb)[0]
if rgb.shape[2] == 2:
rgb = rgb[:, :, :1]
elif rgb.shape[2] > 3:
rgb = rgb[:, :, :3]
self._pilImage = PIL.Image.fromarray(
rgb.astype(np.uint8) if rgb.dtype != np.uint16 else rgb,
('RGB' if rgb.dtype != np.uint16 else 'RGB;16') if rgb.shape[2] == 3 else
('L' if rgb.dtype != np.uint16 else 'L;16'))
except Exception:
pass

Expand Down
2 changes: 2 additions & 0 deletions sources/tifffile/large_image_source_tifffile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
if sidx not in self._zarrcache:
if len(self._zarrcache) > 10:
self._zarrcache = {}
if self.frames > 1:
series.keyframe.nodata = None
za = zarr.open(series.aszarr(), mode='r')
hasgbs = hasattr(za[0], 'get_basic_selection')
self._zarrcache[sidx] = (za, hasgbs)
Expand Down
2 changes: 1 addition & 1 deletion test/test_source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
'skipTiles': r'(sample_image\.ptif|one_layer_missing_tiles)'},
'tifffile': {
'read': r'',
'noread': r'((\.(nc|nd2|yml|yaml|json|czi|png|jpg|jpeg|jp2|dcm|zarr\.db|zarr\.zip)$)' +
'noread': r'((\.(nc|nd2|yml|yaml|json|czi|png|jpg|jpeg|jp2|dcm|zarr\.db|zarr\.zip)|(nokeyframe\.ome\.tiff|XY01\.ome\.tif)$)' + # noqa
(r'|bad_axes' if sys.version_info < (3, 9) else '') +
r')',
},
Expand Down

0 comments on commit 018fef8

Please sign in to comment.