Skip to content

Commit

Permalink
Merge pull request #5289 from radarhere/ipythonviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 7, 2021
2 parents 3a27118 + a1463ff commit 6108596
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
18 changes: 17 additions & 1 deletion Tests/test_imageshow.py
Expand Up @@ -62,4 +62,20 @@ def test_viewer():

def test_viewers():
for viewer in ImageShow._viewers:
viewer.get_command("test.jpg")
try:
viewer.get_command("test.jpg")
except NotImplementedError:
pass


def test_ipythonviewer():
pytest.importorskip("IPython", reason="IPython not installed")
for viewer in ImageShow._viewers:
if isinstance(viewer, ImageShow.IPythonViewer):
test_viewer = viewer
break
else:
assert False

im = hopper()
assert test_viewer.show(im) == 1
1 change: 1 addition & 0 deletions docs/reference/ImageShow.rst
Expand Up @@ -9,6 +9,7 @@ All default viewers convert the image to be shown to PNG format.

.. autofunction:: PIL.ImageShow.show

.. autoclass:: IPythonViewer
.. autoclass:: WindowsViewer
.. autoclass:: MacViewer

Expand Down
13 changes: 10 additions & 3 deletions docs/releasenotes/8.2.0.rst
Expand Up @@ -21,10 +21,17 @@ TODO
API Additions
=============

TODO
^^^^
ImageShow.IPythonViewer
^^^^^^^^^^^^^^^^^^^^^^^

TODO
If IPython is present, this new ``ImageShow.Viewer`` subclass will be
registered. It displays images on all IPython frontends. This will be helpful
to users of Google Colab, allowing ``im.show()`` to display images.

It is lower in priority than the other default Viewer instances, so it will
only be used by ``im.show()`` or ``ImageShow.show()`` if none of the other
viewers are available. This means that the behaviour of ``ImageShow`` will stay
the same for most Pillow users.

Security
========
Expand Down
17 changes: 17 additions & 0 deletions src/PIL/ImageShow.py
Expand Up @@ -225,6 +225,23 @@ def get_command_ex(self, file, title=None, **options):
if shutil.which("xv"):
register(XVViewer)


class IPythonViewer(Viewer):
"""The viewer for IPython frontends."""

def show_image(self, image, **options):
ipython_display(image)
return 1


try:
from IPython.display import display as ipython_display
except ImportError:
pass
else:
register(IPythonViewer)


if __name__ == "__main__":

if len(sys.argv) < 2:
Expand Down

0 comments on commit 6108596

Please sign in to comment.