How to show a PIL Image in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. How to show a PIL Image in Jupyter Notebook
  2. Showing an inline image with matplotlib in Jupyter Notebook
  3. Displaying an image directly in Jupyter Notebook

# How to show a PIL Image in Jupyter Notebook

To show a PIL image in Jupyter Notebook:

  1. Use the Image.open() method from the Pillow module to open the image file.
  2. Use the display() function from the IPython.display module to show the image.
main.py
from PIL import Image from IPython.display import display pil_image = Image.open('images/thumbnail.webp') display(pil_image)

show pil image in jupyter notebook

The example assumes that you have an images/ folder that stores a thumbnail.webp image.

Make sure you have the Pillow module installed to be able to run the code.

You can issue the following commands in a cell to install the module.

shell
!pip uninstall PIL !pip install Pillow

Or the following commands from your terminal.

shell
pip uninstall PIL pip install Pillow # Or with PIP3 pip3 uninstall PIL pip3 install Pillow

The PIL.Image.open() method is used to open and identify the given image.

The argument the method takes is the path to the image (including the extension).

The method returns an Image object that we can pass to the display() function from the IPython.display module.

main.py
from PIL import Image from IPython.display import display pil_image = Image.open('images/thumbnail.webp') display(pil_image)

In more recent versions of Jupyter, you don't have to import the display function.

The following code sample achieves the same result.

main.py
from PIL import Image pil_image = Image.open('images/thumbnail.webp') display(pil_image)

Jupyter Notebook will also show the image if it is the last line in the cell.

For example, the following 2 lines of code render the specified image.

main.py
from PIL import Image # ✅ Renders image (last line in cell) Image.open('images/thumbnail.webp')

render image on last line of cell

However, this wouldn't work if the image is not the last line in the cell.

main.py
from PIL import Image # ⛔️ Does NOT render the image (not the last line in the cell) Image.open('images/thumbnail.webp') print('bobbyhadz.com')

image not last line in cell

If your image is not the last line in a cell, you should use the display() function.

main.py
from PIL import Image display(Image.open('images/thumbnail.webp')) print('bobbyhadz.com')

use display function if image is not last line in cell

Basically, you should either return the image object from an input cell or pass it to the display function to display it on the frontend.

# Showing an inline image with matplotlib in Jupyter Notebook

You can also use the matplotlib and numpy modules to show an inline image in Jupyter Notebook.

main.py
from PIL import Image from matplotlib.pyplot import imshow import numpy as np %matplotlib inline pil_image = Image.open('images/thumbnail.webp', mode='r') imshow(np.asarray(pil_image)) print('bobbyhadz.com')

Here is the output of running the code in the cell.

display inline image using matplotlib

Make sure you have matplotlib and numpy installed

shell
!pip install matplotlib !pip install numpy

We used the %matplotlib inline magic function to display the image inline, directly below the code cell that produced it.

main.py
%matplotlib inline

The matplotlib.imshow method is used to display the supplied data as an image.

If you want to open the image in a separate window, call the show() method on it.

main.py
from PIL import Image from matplotlib.pyplot import imshow import numpy as np pil_image = Image.open('images/thumbnail.webp', mode='r') pil_image.show()

open image in separate preview window

# Displaying an image directly in Jupyter Notebook

You can also use the Image class from the IPython.display module to display an image directly, without using Pillow.

main.py
from IPython.display import Image, display display(Image(filename='images/pizza.png'))

display image directly

If you need to display multiple images, store the paths in a list and use a for loop.

main.py
from IPython.display import Image, display image_paths = ['images/pizza.png', 'images/house.png'] for image_path in image_paths: display(Image(filename=image_path))

However, this approach only works for png and jpg images.

If you need to display images with other extensions, use markdown.

You can click on the Code dropdown menu and select Markdown.

example.md
![thumbnail](images/thumbnail.webp "Image Title")

display image using markdown

The code sample assumes that you have an images directory that contains a thumbnail.webp image.

The text between the square brackets is used as the alt text of the image.

You can also specify a title that is shown when the user hovers over the image.

If you set the cell to Markdown, you can also use a basic img tag to show an image.

using img tag to show image in jupyter

If you want to read more on how to use markdown in Jupyter cells, check out the following article.

I've also written an article on How to display a List as a Table in Jupyter Notebook.

If you need to display multiple images side by side using markdown, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.