Skip to content

Commit

Permalink
Merge pull request #7135 from n3011/jpg_repr
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed May 16, 2023
2 parents 512776e + b5d622c commit a58034b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Tests/test_file_jpeg.py
Expand Up @@ -922,6 +922,19 @@ def closure(mode, *args):
im.load()
ImageFile.LOAD_TRUNCATED_IMAGES = False

def test_repr_jpeg(self):
im = hopper()

with Image.open(BytesIO(im._repr_jpeg_())) as repr_jpeg:
assert repr_jpeg.format == "JPEG"
assert_image_similar(im, repr_jpeg, 17)

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

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


@pytest.mark.skipif(not is_win32(), reason="Windows only")
@skip_unless_feature("jpg")
Expand Down
12 changes: 12 additions & 0 deletions docs/releasenotes/10.0.0.rst
Expand Up @@ -160,6 +160,18 @@ TODO
Other Changes
=============

Support display_jpeg() in IPython
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In addition to ``display()`` and ``display_png``, ``display_jpeg()`` can now
also be used to display images in IPython::

from PIL import Image
from IPython.display import display_jpeg

im = Image.new("RGB", (100, 100), (255, 0, 0))
display_jpeg(im)

Support reading signed 8-bit TIFF images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
26 changes: 20 additions & 6 deletions src/PIL/Image.py
Expand Up @@ -633,19 +633,34 @@ def _repr_pretty_(self, p, cycle):
)
)

def _repr_png_(self):
"""iPython display hook support
def _repr_image(self, image_format):
"""Helper function for iPython display hook.
:returns: png version of the image as bytes
:param image_format: Image format.
:returns: image as bytes, saved into the given format.
"""
b = io.BytesIO()
try:
self.save(b, "PNG")
self.save(b, image_format)
except Exception as e:
msg = "Could not save to PNG for display"
msg = f"Could not save to {image_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
"""
return self._repr_image("PNG")

def _repr_jpeg_(self):
"""iPython display hook support for JPEG format.
:returns: JPEG version of the image as bytes
"""
return self._repr_image("JPEG")

@property
def __array_interface__(self):
# numpy array interface support
Expand Down Expand Up @@ -1108,7 +1123,6 @@ def quantize(
Available methods are :data:`Dither.NONE` or :data:`Dither.FLOYDSTEINBERG`
(default).
:returns: A new image
"""

self.load()
Expand Down

0 comments on commit a58034b

Please sign in to comment.