How to show PIL Image in ipython notebook

PythonIpythonPython Imaging-LibraryIpython Notebook

Python Problem Overview


This is my code

from PIL import Image
pil_im = Image.open('data/empire.jpg')

I would like to do some image manipulation on it, and then show it on screen.
I am having problem with showing PIL Image in python notebook.

I have tried:

print pil_im

And just

pil_im

But both just give me:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=569x800 at 0x10ECA0710>

Python Solutions


Solution 1 - Python

Updated 2021/11/17

When using PIL/Pillow, Jupyter Notebooks now have a display built-in that will show the image directly, with no extra fuss.

display(pil_im)

Jupyter will also show the image if it is simply the last line in a cell (this has changed since the original post). Thanks to answers from @Dean and @Prabhat for pointing this out.

Other Methods

From File

You can also use IPython's display module to load the image. You can read more from the doc.

from IPython.display import Image 
pil_img = Image(filename='data/empire.jpg')
display(pil_img)
From PIL.Image Object

As OP's requirement is to use PIL, if you want to show inline image, you can use matplotlib.pyplot.imshow with numpy.asarray like this too:

from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image

%matplotlib inline
pil_im = Image.open('data/empire.jpg', 'r')
imshow(np.asarray(pil_im))

If you only require a preview rather than an inline, you may just use show like this:

pil_im = Image.open('data/empire.jpg', 'r')
pil_im.show()

Solution 2 - Python

Use IPython display to render PIL images in a notebook.

from PIL import Image               # to load images
from IPython.display import display # to display images

pil_im = Image.open('path/to/image.jpg')
display(pil_im)

Solution 3 - Python

I found that this is working

# source: http://nbviewer.ipython.org/gist/deeplook/5162445
from io import BytesIO

from IPython import display
from PIL import Image


def display_pil_image(im):
   """Displayhook function for PIL Images, rendered as PNG."""

   b = BytesIO()
   im.save(b, format='png')
   data = b.getvalue()

   ip_img = display.Image(data=data, format='png', embed=True)
   return ip_img._repr_png_()


# register display func with PNG formatter:
png_formatter = get_ipython().display_formatter.formatters['image/png']
dpi = png_formatter.for_type(Image.Image, display_pil_image)

After this I can just do:

pil_im

But this must be last line in cell, with no print after it

Solution 4 - Python

case python3

from PIL import Image
from IPython.display import HTML
from io import BytesIO
from base64 import b64encode

pil_im = Image.open('data/empire.jpg')
b = BytesIO()  
pil_im.save(b, format='png')
HTML("<img src='data:image/png;base64,{0}'/>".format(b64encode(b.getvalue()).decode('utf-8')))

Solution 5 - Python

much simpler in jupyter using pillow.

from PIL import Image
image0=Image.open('image.png')
image0

Solution 6 - Python

In order to simply visualize the image in a notebook you can use display()

%matplotlib inline
from PIL import Image

im = Image.open(im_path)
display(im)

Solution 7 - Python

You can open an image using the Image class from the package PIL and display it with plt.imshow directly.

# First import libraries.
from PIL import Image
import matplotlib.pyplot as plt

# The folliwing line is useful in Jupyter notebook
%matplotlib inline

# Open your file image using the path
img = Image.open(<path_to_image>)

# Since plt knows how to handle instance of the Image class, just input your loaded image to imshow method
plt.imshow(img)

Solution 8 - Python

If you are using the pylab extension, you could convert the image to a numpy array and use matplotlib's imshow.

%pylab # only if not started with the --pylab option
imshow(array(pil_im))

EDIT: As mentioned in the comments, the pylab module is deprecated, so use the matplotlib magic instead and import the function explicitly:

%matplotlib
from matplotlib.pyplot import imshow 
imshow(array(pil_im))

Solution 9 - Python

Based on other answers and my tries, best experience would be first installing, pillow and scipy, then using the following starting code on your jupyter notebook:

%matplotlib inline
from matplotlib.pyplot import imshow
from scipy.misc import imread

imshow(imread('image.jpg', 1))

Solution 10 - Python

A cleaner Python3 version that use standard numpy, matplotlib and PIL. Merging the answer for opening from URL.

import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

pil_im = Image.open('image.jpg')
## Uncomment to open from URL
#import requests
#r = requests.get('https://www.vegvesen.no/public/webkamera/kamera?id=131206')
#pil_im = Image.open(BytesIO(r.content))
im_array = np.asarray(pil_im)
plt.imshow(im_array)
plt.show()

Solution 11 - Python

Just use

from IPython.display import Image 
Image('image.png')

Solution 12 - Python

I suggest following installation by no image show img.show() (from PIL import Image)

$ sudo apt-get install imagemagick

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWebOrCodeView Question on Stackoverflow
Solution 1 - PythonAnzelView Answer on Stackoverflow
Solution 2 - PythonsathyzView Answer on Stackoverflow
Solution 3 - PythonWebOrCodeView Answer on Stackoverflow
Solution 4 - PythongaziyaView Answer on Stackoverflow
Solution 5 - PythonPrabhatView Answer on Stackoverflow
Solution 6 - PythonDeanView Answer on Stackoverflow
Solution 7 - PythonM . FranklinView Answer on Stackoverflow
Solution 8 - PythonRobView Answer on Stackoverflow
Solution 9 - PythonEbrahim ByagowiView Answer on Stackoverflow
Solution 10 - PythonPunnerudView Answer on Stackoverflow
Solution 11 - PythonQuanlongView Answer on Stackoverflow
Solution 12 - PythonDr.-Ing. Hiesik KImView Answer on Stackoverflow