Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/handle-repr-exceptions
Browse files Browse the repository at this point in the history
Simplified code
  • Loading branch information
mtreinish committed Jul 24, 2023
2 parents 6215cd3 + e5c94ec commit 2a55b14
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/PIL/Image.py
Expand Up @@ -641,30 +641,23 @@ def _repr_image(self, image_format, **kwargs):
b = io.BytesIO()
try:
self.save(b, image_format, **kwargs)
except Exception as e:
msg = f"Could not save to {image_format} for display"
raise ValueError(msg) from e
except Exception:
return None
return b.getvalue()

def _repr_png_(self):
"""iPython display hook support for PNG format.
:returns: PNG version of the image as bytes
"""
try:
return self._repr_image("PNG", compress_level=1)
except Exception:
return None
return self._repr_image("PNG", compress_level=1)

def _repr_jpeg_(self):
"""iPython display hook support for JPEG format.
:returns: JPEG version of the image as bytes
"""
try:
return self._repr_image("JPEG")
except Exception:
return None
return self._repr_image("JPEG")

@property
def __array_interface__(self):
Expand Down

0 comments on commit 2a55b14

Please sign in to comment.