Displaying Images in Python

From icons to animation, we use images that play an important role to attract users. If we develop a website or an application, it is necessary to use images when. A developer must be aware …

Displaying Images in Python

From icons to animation, we use images that play an important role to attract users. If we develop a website or an application, it is necessary to use images when. A developer must be aware of which method is best to display images for their project’s output.

Here we are working on displaying images in python programming. There are multiple methods to show or display images in python. We use six different python libraries to read the images and display them on screen.

  1. Pillow library.
  2. OpenCV library 
  3. IPython library 
  4. matplot library 
  5. Tkinter GUI 
  6. Scikit-image

1. Pillow library to Display images in python.

Pillow is a powerful imaging library of Python language that helps to display images with editing capabilities. It is the fastest method to process and display images as compared to other imaging libraries. It temporarily stores images in a default program folder and displays that as an output in a standard program such as store image temporarily and display in Windows Photo program instead of displaying it in a separate output window. Once Execution is finished to display the image in a standard file, the temporarily stored files are deleted from the default program that is used to display the image.

Pillow supported image formats

It supports more than thirty image formats. Some popular formats are JPEG, BMP, PNG, PPM, TIFF, and GIF.

  • import image and image class from Pillow library.
  • open an image from files by specifying its path
  • display in the default program.
#Display Images using PIL library
# Import Image class from PIL package
from PIL import Image

# Load image from file 
img = Image.open(r"C:\Users\aisha\Desktop\lake.jpg")
img.show()

Output

Displaying Images in Python

2. OpenCV library to Display images in python

OpenCV is a real-time computer vision library that works on the processing of an image or a video to identify the objects from them. It supports all formats of images. cv2.imshow() command displays the image in a separate window that is fit to the image size.

OpenCV supported image formats

BMP, dib, JPEG files, png, ppm, ppm, sr, ras, tiff

Note: It read image and convert into greyscale if flag value is 0 and change to the colorful image if specified flag value increase.

OpenCV does not display images and responds to an empty window in case of the file is missing or following invalid formats etc.

Install OpenCV in your active environment if it is not pre-installed.

  • Import the latest module of OpenCV that is cv2
  • Load an image from specifying path and load image as a grayscale image by assigning the value 0
  • Display image with im.show()
#Display Images using OpenCV
import cv2
# Load a color image in grayscale
img = cv2.imread(r'C:\Users\aisha\Desktop\lake.jpg', 0)
cv2.imshow(r'C:\Users\aisha\Desktop\lake.jpg', img)

output when flag value is 0.

Displaying Images in Python

output when flag value is default

#Display Images using OpenCV library
import cv2
# Load an image 
img = cv2.imread(r'C:\Users\aisha\Desktop\lake.jpg')
cv2.imshow(r'C:\Users\aisha\Desktop\lake.jpg', img)
Displaying Images in Python

3. IPython library to Display images in python

If you are using a Jupyter notebook for python programming, this library helps to display the image below your code in the notebook.

  • Import display from IPython library that will display image.
  • Show an image that the path is specified in the display command.
#Display Images using IPyton library
from IPython import display
display.Image(r"C:\Users\aisha\Desktop\lakeimage.png")

Output

Displaying Images in Python

4. Matplot library to Display images in python

Matplot is a visualization library, based on NumPy arrays to visualize the data in 2D plots. 

  • import plt and mpimg packages that help to manipulate images.
  • load and read an image by imread command.
  • Display output image with number axes.

Matplot supported image formats

ps, eps, pdf, png, raw, rgba, svg, svgz, jpg, jpeg, tif, and tiff.

Note: you can turn off the number axes by using the plt.axis(“off”) command.

#Display Images using Matplot library
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread(r'C:\Users\aisha\Desktop\lake.jpg')
imgplot = plt.imshow(img)
plt.show()

Output

Displaying Images in Python

5. Tkinter GUI to Display images in python

Tkinter toolkit is the easiest and fastest method to build graphical Interface applications in python. It helps to create a GUI application within a few lines of code.

Tkinter opens only the image size that is assigned in the geometry command.

Tkinter supported image formats

  • PNG
  • PPM
  • PGM
  • GIF
  • Import tkinter module (Note: use tkinter for 3.x python version and Tkinter for less than 3.x python versions)
  • m is the name of an object of the output window. It creates a parent window
  • set the size of the window in which you want to display the image.
  • read the image as an input and store it in the photo variable.
  • create a label that stores the image. It is the child of m widget. 
  • pack that label that works as a container box.
  • use main loop() to run the program infinite times until the output window closed

Note: Below program only displays PNG, PPM, and GIF. It does not support JPEG format images.

#Display Images using tkinter GUI
from tkinter import *

m = Tk()
m.geometry("700x500")
photo = PhotoImage(file= r"C:\Users\aisha\Desktop\lakeimage.png")
    
img_label = Label(image=photo)
img_label.pack()
m.mainloop()
Displaying Images in Python

Tkinter  and Pillow library to a Display JPEG image in python

To display JPEG images in Tkinter,  We also use the PIL library because it supports more than 30 formats.

from tkinter import *
from PIL import Image, ImageTk
m = Tk()

m.geometry("900x500")
image= Image.open(r"C:\Users\aisha\Desktop\lake.jpeg ")
photo = ImageTk.PhotoImage(image)
img_label = Label(image=photo)
img_label.pack()
    
m.mainloop()
Displaying Images in Python

6. Scikit-image library to a Display JPEG image in python

Scikit is a python distributed module for Scipy that is developed independently. It is an open-source Scipy toolkit, containing a large number of image processing algorithms such as geometry, morphology, segmentation, filtration, etc.

Note: Scikit and Skimag are the same package.

Scikit supported image formats

Scikit works with the NumPy array that supports many data types within a particular range. This method displays images with axes values. Its output is the same as the output by using matplot with pyplot package.

  • Before running the scikit-image library, first install NumPy, SciPy, and Scikit-image to your python active environment.
  • Import image libraries from the skimage package.
  • load the file from the specified path.
  • Read the file as an input.
  • Display the file as output by using io.
#Display Images using scikit-image
import os
# Importing io from skimage
import skimage
from skimage import io

# Load lake image from file
file = os.path.join(skimage.data_dir, r"C:\Users\aisha\Desktop\lake.jpg" )
lake = io.imread(file)
# Show the input image
io.imshow(lake)
io.show()

Displaying Images in Python

Conclusion

Displaying images in python is  not a difficult task. In this article, we use six different libraries and packages of python to display images. We use Pillow, Matplot, Tkinter, Scikit, OpenCV, and IPython libraries to display images.

Suggested Read: Remove Brackets from list Python

Leave a Comment