Skip to content

Commit

Permalink
Merge pull request #5139 from radarhere/repr_png
Browse files Browse the repository at this point in the history
Added exception explaining that _repr_png_ saves to PNG
  • Loading branch information
hugovk committed Dec 29, 2020
2 parents 8d62314 + fdce845 commit 85d61ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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

0 comments on commit 85d61ca

Please sign in to comment.