203

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>
WebOrCode
  • 6,852
  • 9
  • 43
  • 70

12 Answers12

344

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()
Neil Traft
  • 18,367
  • 15
  • 63
  • 70
Anzel
  • 19,825
  • 5
  • 51
  • 52
  • 7
    Do not import anything from `IPython.core` it is not stable api, we expose the stable api directly in `IPython.display`. I edited the post myself. – Matt Oct 30 '14 at 10:24
  • you are displaying image from file. I asked from PIL like in my example `pil_im` – WebOrCode Oct 30 '14 at 19:17
  • @Matt, thanks for clarifying this. Although I've never run into problem using `core`, glad I've learnt a new thing :) – Anzel Oct 30 '14 at 21:07
  • @WebOrCode, I've updated my answer to provide solutions using `PIL` as requested in OP. – Anzel Oct 30 '14 at 21:29
  • Yes, there is no problem for now, but we reserve the right to move things in core somewhere else, at least the things in `core` exposed in `display`. and it's shorter to type :-) – Matt Nov 02 '14 at 11:29
  • @zhangxaochen, I guess it's because you're not assigning Image.open() to any name, and notebook is smart enough to preview the image for you. You can instantiate the image without opening omitting .open - I'm away from my laptop so please try and let me know your updates. I may update my answer with additional info if required – Anzel Nov 02 '15 at 17:27
  • @Anzel, Yes I'm just asking if ipython has any one-liner magic to show the image object inline conviniently. ps: `PIL.Image` is a module, not a callable. And if you mean `IPython.display.Image`, it's saying: `Cannot embed the 'gif' image format` – zhangxaochen Nov 03 '15 at 01:29
  • @zhangxaochen, right I see I misunderstood your question before. I meant `Ipython.display.Image` and it currently only supports **png/jpg/jpeg**. So no, there is no one liner shortcut natively from Ipython afaik. – Anzel Nov 03 '15 at 07:15
  • Stable docs here: https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html – dustindorroh May 30 '19 at 05:36
  • @Anzel you could add that `display(im)` actually displays the image. The docs you linked don't contain this bit of info either. – Avio Dec 06 '19 at 15:00
  • @Avio ah interesting I’ve never used it like that. Please feel free to make edits, I’d happily approve and thanks for your info. – Anzel Dec 06 '19 at 15:05
  • Edited. It was not showing any image, probably because I was doing it in a for loop... – Avio Dec 06 '19 at 15:08
  • @Avio, excellent. Perhaps take a few screenshots to show what each scenario will output? Don’t worry if it would require much of your time. I can pick a time to do those and attach under each case. – Anzel Dec 06 '19 at 15:12
  • @Anzel could you clarify this? "If you only require a preview rather than an inline". How else a notebook can show an image besides inlining it? `pil_im.show()` does not do anything in my case. `IPython.display.display` works though. – greatvovan May 22 '20 at 22:02
  • 1
    @greatvovan that preview is to whoever using ipython notebook —no-browser for instance (or jupyter for newer version), not necessarily have to have the web browser, if that makes sense? – Anzel May 22 '20 at 22:05
  • @Anzel I see thanks. I forgot that there was a time people used notebooks without a browser :) – greatvovan May 22 '20 at 22:06
  • @greatvovan yeah and I am one of those who likes to stick with terminal as much as possible :-) – Anzel May 22 '20 at 22:07
76

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)
melMass
  • 3,813
  • 1
  • 31
  • 30
sathyz
  • 1,401
  • 10
  • 12
  • 3
    and if you just want to quickly inspect an image, you can just "return" it, to see its value, and it will be displayed automatically – Ciprian Tomoiagă Aug 29 '18 at 10:23
  • 1
    This is so much simpler than other answers -- there's no need to convert to BytesIO or numpy array. – levis501 Sep 09 '18 at 15:31
  • also it must be more performant (unless the interpreter understands that the data is being transcribed back and fourth) – Baedsch Apr 24 '19 at 13:58
  • Unfortunately I can't get this to work in a colab notebook :-/ – Levon Jun 10 '19 at 15:54
  • 1
    this is the simplest approach - thanks for breaking out just the display object from IPython and the Image from PIL. i was using PIL to create an image from weird data, and just wanted to display it in my notebook - this worked beautifully and simply. – dave campbell Feb 28 '20 at 13:59
26

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

stenci
  • 8,290
  • 14
  • 64
  • 104
WebOrCode
  • 6,852
  • 9
  • 43
  • 70
  • 3
    You can use display.display to display the display.Image you created immediately, avoiding the dependency on being last in the cell: display.display(ip_img). You can see this used here: https://www.tensorflow.org/tutorials/mandelbrot – David F Nov 26 '17 at 15:08
  • @DavidF no, it seems as if you can't. Trying all permutations of above. How does one show an image in the notebook? – Chris Jan 04 '20 at 18:04
  • @DavidF Thanks, I was able to also use `display.display(i)`. – Zach Mar 25 '21 at 22:44
  • @Chris use the imports, function, and register calls. Then generate an image, `img = Image.new('RGB', (60, 30), color = 'red')`, and then in a code cell run `display.display(img)` and you should see a small red rectangle printed to the notebook. – Zach Mar 25 '21 at 22:46
18

much simpler in jupyter using pillow.

from PIL import Image
image0=Image.open('image.png')
image0
Prabhat
  • 4,066
  • 4
  • 34
  • 41
17

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')))
gaziya
  • 171
  • 1
  • 2
10

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)
Dean
  • 141
  • 1
  • 7
6

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)
M . Franklin
  • 170
  • 1
  • 9
2

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))
Rob
  • 3,418
  • 1
  • 19
  • 27
2

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))
Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
1

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()
Punnerud
  • 7,195
  • 2
  • 54
  • 44
-1

Just use

from IPython.display import Image 
Image('image.png')
Quanlong
  • 24,028
  • 16
  • 69
  • 79
  • Your answer is correct in showing an image but as OP wants to use PIL to modify the image further, it sort of doesn't answer the question directly. – Omar Shabab Oct 24 '21 at 13:13
-3

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

$ sudo apt-get install imagemagick