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 _repr_jpeg_() for IPython display_jpeg #7135

Merged
merged 11 commits into from
May 16, 2023
13 changes: 13 additions & 0 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,19 @@ def closure(mode, *args):
im.load()
ImageFile.LOAD_TRUNCATED_IMAGES = False

def test_repr_jpg(self):
im = hopper()

with Image.open(BytesIO(im._repr_jpg_())) as repr_jpg:
assert repr_jpg.format == "JPEG"
assert_image_equal(im, repr_jpg)

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

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


@pytest.mark.skipif(not is_win32(), reason="Windows only")
@skip_unless_feature("jpg")
Expand Down
23 changes: 20 additions & 3 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,19 +633,36 @@ def _repr_pretty_(self, p, cycle):
)
)

def _repr_png_(self):
def _repr_image(self, format):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change format to something that doesn't shadow a builtin?

"""iPython display hook support

n3011 marked this conversation as resolved.
Show resolved Hide resolved
:param format: Image format.
:returns: png version of the image as bytes
n3011 marked this conversation as resolved.
Show resolved Hide resolved
"""
b = io.BytesIO()
try:
self.save(b, "PNG")
self.save(b, format)
except Exception as e:
msg = "Could not save to PNG for display"
msg = f"Could not save to {format} for display"
raise ValueError(msg) from e
return b.getvalue()

def _repr_png_(self):
"""iPython display hook support for PNG format.

:returns: png version of the image as bytes
n3011 marked this conversation as resolved.
Show resolved Hide resolved
"""
return self._repr_image("PNG")

def _repr_jpg_(self):
"""iPython display hook support for JPEG format.
hugovk marked this conversation as resolved.
Show resolved Hide resolved

:returns: jpg version of the image as bytes
n3011 marked this conversation as resolved.
Show resolved Hide resolved
"""
return self._repr_image("JPEG")

_repr_jpeg_ = _repr_jpg_
n3011 marked this conversation as resolved.
Show resolved Hide resolved

@property
def __array_interface__(self):
# numpy array interface support
Expand Down