How to Display an Image in Python

Manav Narula Feb 02, 2024
  1. Use the PIL Module to Display an Image in Python
  2. Use the opencv Module to Display an Image in Python
  3. Use the Ipython.Display to Display an Image in Python
  4. Use the Matplotlib Library to Display an Image in Python
How to Display an Image in Python

Images can show some charts or figures, train and test Machine Learning models, and develop different applications. In Python, we have many modules available to handle image handling.

In this tutorial, we will discuss how to display an image in Python using different modules.

Use the PIL Module to Display an Image in Python

We have the PIL library in Python, which has methods available to store, display or alter images. This method will create an image object and open the required image by specifying its path.

We can then use the show() function, which will open the required image in a new window. For example:

from PIL import Image

# creating a object
im = Image.open("sample.jpeg")

im.show()

Use the opencv Module to Display an Image in Python

The opencv module is used in Python for machine learning and image processing functions. Its imread() function read images and its imshow() function can display images in a new window. For example,

import cv2

img = cv2.imread("sample.jpeg", 0)
cv2.imshow("sample.jpeg", img)

Use the Ipython.Display to Display an Image in Python

Suppose we are working in a Python Notebook and want to display the image within the notebook. In that case, we can use the Ipython.display module with different methods to display additional files in the interactive notebook. We will show how to use the display() function to show the image using an image object.

import IPython.display as display
from PIL import Image

display.display(Image.open("sample.jpeg"))

We can also directly use the Ipython.display() module and import its sub-package Image to display the picture without creating an object using the PIL module. The following code shows how:

from IPython.display import Image

Image("sample.jpeg")

Use the Matplotlib Library to Display an Image in Python

The Matplotlib library is considered to be a potent tool for visualizations in Python. It can create immensely detailed and beautiful plots with a few lines of code. It can also be used to plot images after reading them from the computer. We can use the mpimg sub-package, which deals with image manipulation, to display an image. We will read the image using the imread() function and then display it using the imshow() function. Using the show() function also displays the interactive notebook’s required image. For example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread("sample.jpeg")
imgplot = plt.imshow(img)
plt.show()
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python Image