diff --git a/Tests/test_image.py b/Tests/test_image.py index 2d46d760d..31b286d31 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -89,6 +89,17 @@ class TestImage: # with pytest.raises(MemoryError): # Image.new("L", (1000000, 1000000)) + def test_repr_pretty(self): + class Pretty: + def text(self, text): + self.pretty_output = text + + im = Image.new("L", (100, 100)) + + p = Pretty() + im._repr_pretty_(p, None) + assert p.pretty_output == "" + def test_open_formats(self): PNGFILE = "Tests/images/hopper.png" JPGFILE = "Tests/images/hopper.jpg" diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 302bd638e..02b71e612 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -641,6 +641,22 @@ class Image: id(self), ) + def _repr_pretty_(self, p, cycle): + """IPython plain text display support""" + + # Same as __repr__ but without unpredicatable id(self), + # to keep Jupyter notebook `text/plain` output stable. + p.text( + "<%s.%s image mode=%s size=%dx%d>" + % ( + self.__class__.__module__, + self.__class__.__name__, + self.mode, + self.size[0], + self.size[1], + ) + ) + def _repr_png_(self): """iPython display hook support