Skip to content

Commit

Permalink
Merge pull request #1787 from girder/better-repr
Browse files Browse the repository at this point in the history
Better repr of large_image classes
  • Loading branch information
manthey authored Jan 24, 2025
2 parents 6a1be4d + 25a5d7c commit 8931fa1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Automatically set the JUPYTER_PROXY value ([#1781](../../pull/1781))
- Add a general channelNames property to tile sources ([#1783](../../pull/1783))
- Speed up compositing styles ([#1784](../../pull/1784))
- Better repr of large_image classes ([#1787](../../pull/1787))

### Changes

Expand Down
21 changes: 20 additions & 1 deletion large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,30 @@ def __reduce__(self) -> Tuple[functools.partial, Tuple[str]]:
return functools.partial(type(self), **self._initValues[1]), self._initValues[0]

def __repr__(self) -> str:
return self.getState()
if hasattr(self, '_initValues') and not hasattr(self, '_unpickleable'):
param = [
f'{k}={v!r}' if k != 'style' or not isinstance(v, dict) or
not getattr(self, '_jsonstyle', None) else
f'style={json.loads(self._jsonstyle)}'
for k, v in self._initValues[1].items()]
return (
f'{self.__class__.__name__}('
f'{", ".join(repr(val) for val in self._initValues[0])}'
f'{", " if len(self._initValues[1]) else ""}'
f'{", ".join(param)}'
')')
return '<' + self.getState() + '>'

def _repr_png_(self) -> bytes:
return self.getThumbnail(encoding='PNG')[0]

def __rich_repr__(self) -> Iterator[Any]:
if not hasattr(self, '_initValues') or hasattr(self, '_unpickleable'):
yield self.getState()
else:
yield from self._initValues[0]
yield from self._initValues[1].items()

@property
def geospatial(self) -> bool:
return False
Expand Down
4 changes: 3 additions & 1 deletion sources/pil/large_image_source_pil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ def _fromRawpy(self, largeImagePath):
"""
# if rawpy is present, try reading via that library first
try:
import builtins

import rawpy

with contextlib.redirect_stderr(open(os.devnull, 'w')):
with contextlib.redirect_stderr(builtins.open(os.devnull, 'w')):
rgb = rawpy.imread(largeImagePath).postprocess()
rgb = large_image.tilesource.utilities._imageToNumpy(rgb)[0]
if rgb.shape[2] == 2:
Expand Down
3 changes: 2 additions & 1 deletion sources/pil/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def prerelease_local_scheme(version):
'all': [
'rawpy',
'pillow-heif',
'pillow-jxl-plugin',
'pillow-jxl-plugin < 1.3 ; python_version < "3.8"',
'pillow-jxl-plugin ; python_version >= "3.9"',
'pillow-jpls',
],
'girder': f'girder-large-image{limit_version}',
Expand Down

0 comments on commit 8931fa1

Please sign in to comment.