Skip to content

Commit

Permalink
improve handling of (transparent) snapshots in IPython/Jupyter Notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelquast committed Dec 4, 2023
1 parent 140a7dc commit d2f3e79
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions eomaps/eomaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3462,21 +3462,13 @@ def snapshot(self, *layer, transparent=False, clear=False):
else:
sn = self._get_snapshot()
try:
from IPython.display import display, Image as IpyImage
from io import BytesIO

# fix issues with PILLOW 10.0 and transparent snapshots
# in jupyter notebooks (TODO should be fixed in PILLOW 10.1.0)
# ...bug causes unwanted errors in _repr__jpeg_ for RGBA images
# the fix enforces png as format
temp = BytesIO()
Image.fromarray(sn, "RGBA").save(temp, format="png")

display(
IpyImage(temp.getvalue()),
display_id=True,
clear=clear,
)
from IPython.display import display_png, clear_output

Check warning on line 3465 in eomaps/eomaps.py

View check run for this annotation

Codecov / codecov/patch

eomaps/eomaps.py#L3465

Added line #L3465 was not covered by tests

if clear:
clear_output(wait=True)

Check warning on line 3468 in eomaps/eomaps.py

View check run for this annotation

Codecov / codecov/patch

eomaps/eomaps.py#L3467-L3468

Added lines #L3467 - L3468 were not covered by tests
# use display_png to avoid issues with transparent snapshots
display_png(Image.fromarray(sn, "RGBA"), raw=False)

Check warning on line 3470 in eomaps/eomaps.py

View check run for this annotation

Codecov / codecov/patch

eomaps/eomaps.py#L3470

Added line #L3470 was not covered by tests

except Exception:
_log.exception(
"Unable to display the snapshot... is the script "
Expand Down

0 comments on commit d2f3e79

Please sign in to comment.