Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence exceptions in _repr_jpeg_ and _repr_png_ #7266

Merged
merged 5 commits into from
Jul 31, 2023

Conversation

mtreinish
Copy link
Contributor

@mtreinish mtreinish commented Jul 6, 2023

Resolves #7283

In 10.0.0 a _repr_jpeg_ implementation was added to the Image object to enable the use of display_jpeg() in IPython environments. However, in some cases the implementation of this method could result in an exception being raised while trying to generate the jpeg data. The best example is if the image data is in an RGBA format as a result of the object being created by opening a PNG file. In this case trying to save the Image object as a jpeg will error because the jpeg format can't represent the transparency in the alpha channel. This results in an exception being raised in the IPython/Jupyter context when outputting the image object. However, in cases like this IPython allows the repr methods to return None to indicate there is no representation in that format available. [1] This commit updates the _repr_png_ and _repr_jpeg_ methods to catch any exception that might be raised while trying to generate the image data. If an exception is raised we treat that as not being able to generate image data in that format and return None instead.

Related to #7259

[1] https://ipython.readthedocs.io/en/stable/config/integrating.html#custom-methods

Changes proposed in this pull request:

  • Handle any exceptions raised by generating image data in Image._repr_png_ and return None instead of raising to users.
  • Handle any exceptions raised by generating image data in Image._repr_jpeg_ and return None instead of raising to users.

In 10.0.0 a _repr_jpeg_ implementation was added to the Image object to
enable the use of display_jpeg() in IPython environments. However, in
some cases the implementation of this method could result in an
exception being raised while trying to generate the jpeg data. The best
example is if the image data is in an RGBA format as a result of the
object being created by opening a PNG file. In this case trying to save
the Image object as a jpeg will error because the jpeg format can't
represent the transparency in the alpha channel. This results in an
exception being raised in the IPython/Jupyter context when outputing the
image object. However, in cases like this IPython allows the repr
methods to return None to indicate there is no representation in that
format available. [1] This commit updates the _repr_png_ and _repr_jpeg_
methods to catch any exception that might be raised while trying to
generate the image data. If an exception is raised we treat that as not
being able to generate image data in that format and return None
instead.

Related to python-pillow#7259

[1] https://ipython.readthedocs.io/en/stable/config/integrating.html#custom-methods
src/PIL/Image.py Outdated Show resolved Hide resolved
mtreinish and others added 3 commits July 6, 2023 09:00
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
@radarhere
Copy link
Member

I've created mtreinish#1 with a suggestion to simplify the code.

@radarhere radarhere merged commit 07038d7 into python-pillow:main Jul 31, 2023
49 of 50 checks passed
@radarhere radarhere changed the title Handle exceptions in _repr_jpeg_ and _repr_png_ Silence exceptions in _repr_jpeg_ and _repr_png_ Jul 31, 2023
@mtreinish mtreinish deleted the handle-repr-exceptions branch August 10, 2023 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iPython cannot display RGBA image after upgrade to 10.0.0
2 participants