Merge pull request #1091 from wiredfool/repr_png

iPython display hook
This commit is contained in:
Hugo 2015-01-30 14:37:03 +02:00
commit 95e9379f45
2 changed files with 18 additions and 0 deletions

View File

@ -598,6 +598,16 @@ class Image:
id(self) id(self)
) )
def _repr_png_(self):
""" iPython display hook support
:returns: png version of the image as bytes
"""
from io import BytesIO
b = BytesIO()
self.save(b, 'PNG')
return b.getvalue()
def __getattr__(self, name): def __getattr__(self, name):
if name == "__array_interface__": if name == "__array_interface__":
# numpy array interface support # numpy array interface support

View File

@ -374,6 +374,14 @@ class TestFilePng(PillowTestCase):
im = roundtrip(im) im = roundtrip(im)
self.assertEqual(im.info['icc_profile'], expected_icc) self.assertEqual(im.info['icc_profile'], expected_icc)
def test_repr_png(self):
im = hopper()
repr_png = Image.open(BytesIO(im._repr_png_()))
self.assertEqual(repr_png.format, 'PNG')
self.assert_image_equal(im, repr_png)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()