5 Ways To Display Images in Python

Read Image Using Python Considerable Ways

In this article, we shall study the different ways how you can read and display images in Python. We can achieve this in numerous ways. The reason is due to the abundant library support. We will also explore how we can use them in crossbreeding with each other.

Ways to Display Images Using Python

The following is a list of libraries of Python that enable us to process the images and do the corresponding tasks.

  1. OpenCV
  2. Matplotlib
  3. Pillow
  4. Scikit-Image
  5. Tensorflow

Let’s now see how to display an image in a Python GUI window easily. There may be many other modules and/or hacks to view images too, so don’t limit yourself to just these 5 modules!

1. OpenCV to Display Images in Python

This is a very famous, beginner-friendly and open-source, and powerful package that is responsible for image processing. With a small set of commands, we can take our Computer Vision journey to next level. There are two main functions OpenCV provides to read and display images.

  1. cv2.imread()
  2. cv2.imshow()


import sys # to access the system
import cv2
img = cv2.imread("sheep.png", cv2.IMREAD_ANYCOLOR)

while True:
    cv2.imshow("Sheep", img)
    sys.exit() # to exit from all the processes

cv2.destroyAllWindows() # destroy all windows


Displaying Image Through OpenCV
Displaying the image through OpenCV


  1. Import the OpenCV package to access the functions. Also, import the sys module for additional packages.
  2. Create a variable as img that holds our image. Call the cv2.imread() function and deliver the image path/image name as a first parameter. Then set the cv2.IMREAD_ANYCOLOR is the next parameter to read every color of the image.
  3. Then set a while loop and that will help us render the image an infinite number of times till we exit the system.
  4. Then use the cv2.imshow() function inside the while loop. It takes two parameters, the image title and the image path variable img.
  5. The cv2.waitkey() method waits till we exit or click on the close button.
  6. Then call the sys.exit() method to safely exit the technique.
  7. Finally, we destroy all the created windows using cv2.destroyAllWindows().

2. Matplotlib

This package is mainly for data visualization. But, through the plotting techniques, we can view the image in a graphical format where each pixel lies on 2D x-y axes.

Thie library also has the equivalent functions as that of open cv. Just the package name changes.

  1. matplotlib.image.imread()
  2. matplotlib.pyplot.imshow()


from matplotlib import pyplot as plt
from matplotlib import image as mpimg

plt.title("Sheep Image")
plt.xlabel("X pixel scaling")
plt.ylabel("Y pixels scaling")

image = mpimg.imread("sheep.png")


Displaying Image Through Matplotlib 1
Displaying image through Matplotlib


  1. Import the Matplotlib packages’ pylot and image modules.
  2. Set the title of the image as Sheep Image using plt.title() method.
  3. As matplotlib reads the image in x-y plane. We need labels xlabel() and ylabel() functions to mention the axes and the pixels.
  4. Create a variable as an image that holds our image. Call the mpimg.imread() function and give the image path/image name as a first parameter.
  5. Then set a while loop and that will help us render the image an infinite number of times till we exit the system.
  6. Then use the plt.imshow() function that takes image variable img. But it will show it in the backend.
  7. To view it on the screen use the plt.show() method and we have our image with properly scaled parameters on the screen.

3. Pillow

This library often offers simple methods for Image manipulations. We can say that it is an image-only library because of its simplicity and adaptability. The functions we are gonna using are open() and show() from PILLOW’s Image module. This action is just within three lines of code.


from PIL import Image
img = Image.open("sheep.png")


Displaying Image Through Pillow
Displaying image through PILLOW


  1. Import the module Image from PIL.
  2. Create a variable img and then call the function open() in it. Give the path that has the image file.
  3. Call the show() function in joint with img variable through the dot operator “.”.
  4. It displays the image through the built-in Photo app in your respective OS.

4. Scikit-Image

Scikit-Image is a sub-module of Scikit-Learn. It is built upon Python and supportive library Matplotlib thus it derives some of its functionalities. Methods are similar to that of the previous packages we saw before.


from skimage import io

img = io.imread("sheep.png")


Displaying Image Through Skimage 1
Displaying image through Skimage

5. Tensorflow

This is a powerful Machine Learning library especially from Google.Inc. It works on different aspects of Machine Learning, Deep Learning, and related concepts. It also has built-in datasets to start a hassle-free journey of Data Science and ML engineering. It works specifically on the computer’s GPU CUDA cores. This makes the model training more efficient and gives less stress to the CPU.

We will be using this library in joint with the Matplotlib module. Because this makes image plotting and displaying much easier.


from warnings import filterwarnings
import tensorflow as tf
from tensorflow import io
from tensorflow import image
from matplotlib import pyplot as plt

tf_img = io.read_file("sheep.png")
tf_img = image.decode_png(tf_img, channels=3)
# plt.show()


  1. Import TensorFlow. Then from TensorFlow also import io and image. 
  2. Import matplotlib’s pyplot module for plotting purposes.
  3. (Optional) also, use the warnings package to avoid unnecessary warnings.
  4. Create a TensorFlow image variable “tf_img” and call the io.read_file() method. Give the image path inside it.
  5. It is read as a default file. To view it as the image we need to use the decode_png() function from the image to get recognized by the system. Make sure you use the correct decider function. They are different for each image type. Use channels = 3. for default GPU usage.
  6. Finally, display the captured image through the plt.imshow() method.


Displaying Image Through Tensorflow And Matplotlib
Displaying Image through Tensorflow and Matplotlib


So, these are the different considerable ways through which we can perform image processing. Python has a ton of options for each unique task. Comment down which method and library do you like the most we implemented in this article.