-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
radarhere
merged 5 commits into
python-pillow:main
from
mtreinish:handle-repr-exceptions
Jul 31, 2023
Merged
Silence exceptions in _repr_jpeg_ and _repr_png_ #7266
radarhere
merged 5 commits into
python-pillow:main
from
mtreinish:handle-repr-exceptions
Jul 31, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
radarhere
reviewed
Jul 6, 2023
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
This was referenced Jul 23, 2023
I've created mtreinish#1 with a suggestion to simplify the code. |
Simplified code
radarhere
approved these changes
Jul 25, 2023
radarhere
changed the title
Handle exceptions in _repr_jpeg_ and _repr_png_
Silence exceptions in _repr_jpeg_ and _repr_png_
Jul 31, 2023
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #7283
In 10.0.0 a
_repr_jpeg_
implementation was added to the Image object to enable the use ofdisplay_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:
Image._repr_png_
and returnNone
instead of raising to users.Image._repr_jpeg_
and returnNone
instead of raising to users.