How to show PIL Image in ipython notebook
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)
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()