How to Display Images in Python

09/08/2021

Contents

In this article, you will learn how to display images in Python.

There are several ways to display images in Python, depending on what you’re trying to do and what libraries you have available.

Using the Matplotlib library

This library is commonly used for plotting and visualizing data, and it provides functions to display images as well. Here is an example of how you can display an image using Matplotlib:

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

img = mpimg.imread('image.jpg')
imgplot = plt.imshow(img)
plt.show()

Using the OpenCV library

This library is commonly used for computer vision tasks and it provides functions to display images as well. Here is an example of how you can display an image using OpenCV:

import cv2

img = cv2.imread('image.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Using the Pillow library

This library is a fork of the Python Imaging Library (PIL) and it provides functions for handling images. Here is an example of how you can display an image using Pillow:

from PIL import Image

img = Image.open("image.jpg")
img.show()

Using IPython display

If you’re working in an IPython environment such as Jupyter notebooks, you can use the IPython.display module to display images. Here is an example:

from IPython.display import Image
Image(filename='image.jpg')