Python

How to show PIL Image in ipython notebook

19 September 2026 · 9 min read

How to show PIL Image in ipython notebook

Working with images in Python, particularly within the interactive environment of an IPython notebook (now Jupyter Notebook), is a common task in data science, machine learning, and image processing. The Python Imaging Library (PIL), now known as Pillow, is a powerful library for manipulating images. However, displaying these images directly within the notebook can sometimes be tricky. This article provides a comprehensive guide on how to show PIL Image in IPython notebook effectively, covering various methods and best practices to ensure seamless visualization of your image data. Whether you’re dealing with image analysis, computer vision projects, or simply exploring image datasets, mastering these techniques will significantly enhance your workflow. We’ll explore different approaches, from basic display methods to more advanced techniques, ensuring that you can effectively visualize your PIL images in any IPython notebook environment.

Understanding PIL and IPython Notebook Integration

Before diving into the specifics of displaying PIL images, it’s crucial to understand the roles of both PIL (Pillow) and IPython Notebook. PIL (Pillow) is the go-to library for image manipulation in Python, offering functionalities like image format conversion, resizing, filtering, and more. IPython Notebook, on the other hand, provides an interactive environment where you can execute Python code, display outputs (including images), and document your work. Integrating these two tools allows for a dynamic and visual exploration of image data.

The core challenge lies in the fact that IPython Notebook, by default, doesn’t directly interpret PIL image objects for display. Therefore, we need to convert the PIL image into a format that the notebook can understand and render. This often involves converting the image into a format like PNG or JPEG, or leveraging IPython’s built-in display functionalities. According to a Stack Overflow survey, image processing tasks are frequently cited as a practical application of Python in data science projects [Stack Overflow Blog]. The correct method to display these images efficiently is critical.

To successfully show PIL Image in IPython notebook, you need to understand the different methods available, their advantages, and potential limitations. This understanding will enable you to choose the most appropriate technique based on your specific needs and the complexity of your image processing workflow. We will cover several approaches, including using IPython.display, Matplotlib, and other common techniques.

Methods to Display PIL Images in IPython Notebook

There are several ways to display a PIL Image within an IPython Notebook. Each method leverages different libraries and approaches to achieve the same goal: visual representation of the image. We will cover the most common and effective methods, explaining their steps and providing code examples.

One straightforward method involves using the IPython.display module. This module provides functions for displaying various types of data within the notebook, including images. By converting the PIL image to a suitable format (e.g., PNG) and then using IPython.display.Image, you can directly embed the image in your notebook output. This approach is particularly useful for quick and simple image display tasks. Another method is leveraging Matplotlib, a popular plotting library in Python. Matplotlib can handle PIL images directly, allowing you to display them using the imshow function. This is beneficial if you are already using Matplotlib for other plotting purposes in your notebook. According to a study by Hunter (2007), Matplotlib is a versatile tool for data visualization in scientific computing [IEEE Xplore].

The choice of method depends largely on your specific requirements and the context of your notebook. For instance, if you need to display many images efficiently, a method that minimizes overhead and processing time might be preferred. Here’s a list of common methods:

  • Using IPython.display.Image
  • Using Matplotlib’s imshow
  • Saving the image to a file and then displaying it

Step-by-Step Guide: Using IPython.display.Image

This section provides a detailed, step-by-step guide on using the IPython.display.Image method to show PIL Image in IPython notebook. This is a widely used and effective approach for displaying images directly within the notebook environment. Understanding and mastering this method will allow you to quickly and easily visualize your image data.

The process involves a few key steps: first, you need to load the image using PIL (Pillow). Then, you convert the PIL image to a byte stream in a format that IPython.display.Image can understand (typically PNG or JPEG). Finally, you use the Image function to display the image within the notebook. This approach is clean, efficient, and avoids the need to save the image to a file, making it ideal for interactive image exploration. This method is generally preferred due to its simplicity and efficiency. The following steps outline the process.

Here are the steps to follow:

  1. Import necessary libraries: Start by importing the PIL and IPython.display modules.
  2. Load the image: Use PIL.Image.open() to load your image file.
  3. Convert to byte stream: Create a BytesIO object and save the image to it in PNG or JPEG format.
  4. Display the image: Use IPython.display.Image() with the byte stream to display the image.

Here’s the code snippet:

python from PIL import Image from io import BytesIO import IPython.display Load the image image = Image.open(“your_image.jpg”) Create a BytesIO object buffered = BytesIO() Save the image to the BytesIO object in PNG format image.save(buffered, format=“PNG”) Display the image using IPython.display.Image image_data = buffered.getvalue() IPython.display.Image(image_data) This code snippet shows a straightforward way to convert and display the PIL image. Remember to replace “your_image.jpg” with the actual path to your image file. This method is efficient because it doesn’t require saving the image to disk, making it suitable for iterative workflows in IPython Notebook.

Alternative Approach: Using Matplotlib

Another popular method for displaying images in IPython Notebook is using Matplotlib. Matplotlib is primarily known as a plotting library, but it also provides functionalities for displaying images. This approach can be particularly useful if you are already using Matplotlib for other visualization tasks in your notebook. Furthermore, Matplotlib provides more control over how the image is displayed, including options for adjusting the colormap, interpolation, and other visual parameters. Using Matplotlib offers a cohesive workflow for both plotting and image display.

To use Matplotlib for displaying PIL images, you simply need to load the image using PIL (Pillow) and then use the imshow function from Matplotlib to display it. Matplotlib can directly handle PIL image objects, making the process quite straightforward. This method seamlessly integrates with other Matplotlib plots and visualizations. Learn more about data visualization.

Here’s an example:

python import matplotlib.pyplot as plt from PIL import Image Load the image image = Image.open(“your_image.jpg”) Display the image using Matplotlib plt.imshow(image) plt.axis(‘off’) Turn off axis labels plt.show() This code snippet demonstrates how to load a PIL image and display it using Matplotlib’s imshow function. The plt.axis(‘off’) line is optional but often used to remove the axis labels for a cleaner display. This method is particularly useful if you need to incorporate image display into a larger Matplotlib-based visualization workflow. For example, if you are plotting data alongside images for analysis, this approach allows for seamless integration. You can adjust parameters like interpolation and colormap directly within Matplotlib to enhance the visual representation of your image.

Troubleshooting Common Issues

While displaying PIL images in IPython Notebook is generally straightforward, you may encounter some common issues. Addressing these issues promptly will ensure a smooth and efficient workflow. Here are some common problems and their solutions.

One frequent issue is encountering errors when loading the image. This can be due to various reasons, such as an incorrect file path, a corrupted image file, or missing image codecs. Ensure that the file path is correct and that the image file is not corrupted. Another common problem is the image not displaying correctly, which can be due to incorrect image format conversion or issues with Matplotlib configuration. Double-check the image format conversion step and ensure that Matplotlib is configured correctly. According to a report by Anaconda, environment management issues are a common source of frustration for data scientists [Anaconda State of Data Science Report]. Proper environment setup is very important.

Here are some troubleshooting tips:

  • File Path Errors: Double-check the file path to your image.
  • Codec Issues: Ensure you have the necessary image codecs installed.
  • Matplotlib Configuration: Verify that Matplotlib is configured correctly in your environment.

Featured Snippet: A common issue is the image not displaying correctly. This can be due to incorrect image format conversion or issues with Matplotlib configuration. Double-check the image format conversion step and ensure that Matplotlib is configured correctly. To resolve this, ensure the file path is accurate and that the image file is not corrupted. If using Matplotlib, verify your environment setup and try reinstalling the library.

Infographic here
FAQ: Displaying PIL Images in IPython Notebook ----------------------------------------------
Why is my PIL image not displaying in the notebook?
This can be due to several reasons, including incorrect file paths, missing image codecs, or issues with the display method (e.g., incorrect format conversion). Double-check these aspects to troubleshoot the issue.
Which method is the most efficient for displaying PIL images?
The IPython.display.Image method is often considered the most efficient as it directly embeds the image without requiring saving to a file.
Can I display multiple PIL images at once?
Yes, you can display multiple images by using subplots in Matplotlib or by calling IPython.display.Image multiple times.
How do I resize the displayed PIL image?
You can resize the image using PIL's resize method before displaying it, or you can adjust the figure size in Matplotlib.
Displaying PIL images in IPython Notebook is a fundamental skill for anyone working with image data in Python. By understanding the different methods available and troubleshooting common issues, you can ensure a smooth and efficient workflow. Whether you choose to use IPython.display.Image, Matplotlib, or other techniques, the ability to visualize your images directly within the notebook will greatly enhance your productivity and insights.

We’ve explored various methods and best practices to effectively show PIL Image in IPython notebook. Understanding these techniques enables seamless visualization, enhancing your workflow and insights. Now, take this knowledge and apply it to your projects! Experiment with different approaches, explore image datasets, and push the boundaries of your image processing capabilities. Consider exploring other related topics like image enhancement techniques, image segmentation, and object detection to further expand your skills. Happy coding!

Question & Answer :
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> 

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()