mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-02 02:43:06 +03:00
Merge pull request #7135 from n3011/jpg_repr
This commit is contained in:
commit
a58034b86c
|
@ -922,6 +922,19 @@ class TestFileJpeg:
|
||||||
im.load()
|
im.load()
|
||||||
ImageFile.LOAD_TRUNCATED_IMAGES = False
|
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")
|
@pytest.mark.skipif(not is_win32(), reason="Windows only")
|
||||||
@skip_unless_feature("jpg")
|
@skip_unless_feature("jpg")
|
||||||
|
|
|
@ -160,6 +160,18 @@ TODO
|
||||||
Other Changes
|
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
|
Support reading signed 8-bit TIFF images
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -633,19 +633,34 @@ class Image:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _repr_png_(self):
|
def _repr_image(self, image_format):
|
||||||
"""iPython display hook support
|
"""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()
|
b = io.BytesIO()
|
||||||
try:
|
try:
|
||||||
self.save(b, "PNG")
|
self.save(b, image_format)
|
||||||
except Exception as e:
|
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
|
raise ValueError(msg) from e
|
||||||
return b.getvalue()
|
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
|
@property
|
||||||
def __array_interface__(self):
|
def __array_interface__(self):
|
||||||
# numpy array interface support
|
# numpy array interface support
|
||||||
|
@ -1108,7 +1123,6 @@ class Image:
|
||||||
Available methods are :data:`Dither.NONE` or :data:`Dither.FLOYDSTEINBERG`
|
Available methods are :data:`Dither.NONE` or :data:`Dither.FLOYDSTEINBERG`
|
||||||
(default).
|
(default).
|
||||||
:returns: A new image
|
:returns: A new image
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user