Skip to content

Commit

Permalink
Improve checks for formats we shouldn't read
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed Dec 6, 2023
1 parent 1cb84ec commit 83d5b09
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))

### Bug Fixes
- Fix an issue emitting geojson annotations ([#1395](../../pull/1395))
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]

Check warning on line 208 in sources/pil/large_image_source_pil/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/pil/large_image_source_pil/__init__.py#L207-L208

Added lines #L207 - L208 were not covered by tests
if rgb.shape[2] == 2:
rgb = rgb[:, :, :1]

Check warning on line 210 in sources/pil/large_image_source_pil/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/pil/large_image_source_pil/__init__.py#L210

Added line #L210 was not covered by tests
elif rgb.shape[2] > 3:
rgb = rgb[:, :, :3]
self._pilImage = PIL.Image.fromarray(

Check warning on line 213 in sources/pil/large_image_source_pil/__init__.py

View check run for this annotation

Codecov / codecov/patch

sources/pil/large_image_source_pil/__init__.py#L212-L213

Added lines #L212 - L213 were not covered by tests
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 83d5b09

Please sign in to comment.