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

Added exception explaining that _repr_png_ saves to PNG #5139

Merged
merged 1 commit into from Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Tests/test_file_png.py
Expand Up @@ -537,6 +537,12 @@ def test_repr_png(self):
assert repr_png.format == "PNG"
assert_image_equal(im, repr_png)

def test_repr_png_error(self):
im = hopper("F")

with pytest.raises(ValueError):
im._repr_png_()

def test_chunk_order(self, tmp_path):
with Image.open("Tests/images/icc_profile.png") as im:
test_file = str(tmp_path / "temp.png")
Expand Down
5 changes: 4 additions & 1 deletion src/PIL/Image.py
Expand Up @@ -670,7 +670,10 @@ def _repr_png_(self):
:returns: png version of the image as bytes
"""
b = io.BytesIO()
self.save(b, "PNG")
try:
self.save(b, "PNG")
except Exception as e:
raise ValueError("Could not save to PNG for display") from e
return b.getvalue()

@property
Expand Down