Web Performance VBR vs CBR: Understanding Bitrate for Optimal Media Handling Displaying Images with Python’s Top 5 Image Libraries 4 Ways to Add Images to GitHub README + 1 Bonus Method Converting Images with Python JavaScript Image Optimization Techniques Building an Image Picker in React with react-native-image-crop-picker 6 Ways to Save Images in Python 5 Ways to Import Images in React + Bonus Automation Method Extract Text from Images in Python with Pillow and pytesseract Downloading Image from URL in Python: 5 Ways with Code Examples Image.open Python Function: Syntax and Quick Tutorial Complete Guide to Video SEO & Automating It With Cloudinary A Complete Guide To Website Image Optimization Video Encoding: How It Works, Formats & Best Practices The Developer’s Guide to PDFs Integrating Cloudinary With Gatsby For Image Optimization Mastering Image Optimization With Netlify And Cloudinary Seamlessly Integrate Cloudinary With Netlify For Optimised Website Assets Ultimate Guide to Asset Optimization Using Cloudinary and Netlify Adding Video To Magento Understanding Magento Media Adding a Video Widget to Your Website: What You Need to Know SDR vs. HDR: Differences and 5 Considerations for Content Creators Audio Manipulation In PHP Image Management Systems: Key Capabilities and Best Practices Video CDN: Why You Need It and Top 5 Video CDNs Video Optimization: Why You Need It and 5 Critical Best Practices Multi CDN: 8 Amazing Benefits, Methods, and Best Practices What Is an Optimized Website and 6 Ways to Optimize Yours Understanding Image Hosting for Websites Sprite Generation with CSS and 4 Automated Tools 8 Image SEO Optimization Tips to Improve Your Search Rankings Website Speed: 5 Reasons Your Site is Slow and How to Fix It Web Performance: What is it, Trends and Insights for 2024

Displaying Images with Python’s Top 5 Image Libraries

Python Image Display

Importance of Displaying Images in Python Applications

There are many reasons you might display images in Python applications. For example:

  • In an application that requires image processing, the ability to display these images at various stages of processing helps developers debug and improve their algorithms.
  • In the field of data science and machine learning, Python is extensively used for tasks like image recognition and object detection. Here, the ability to display images is not a core part of the application, which allows developers to visualize the output of their models and make necessary adjustments.
  • In web development, a key aspect of creating user-friendly applications is the ability to handle and display images efficiently.

Displaying images in Python is straightforward, but the process is slightly different depending on the image processing library you are using. We’ll review the most common options.

This is part of a series of articles about image optimization

In this article:

Python Image Display

Displaying Images in Popular Python Libraries

Let’s see how to display an image in Python using the most popular image-related Python libraries.

1. Using Pillow to Display Images in Python

Pillow, also known as PIL (Python Imaging Library), offers extensive file format support, an efficient internal representation, and powerful image processing capabilities.

To install Pillow, simply type pip install pillow in your terminal. To display an image, you can use the Image module’s open function to load the image and the show function to display it.

from PIL import Image
# Load an image
img = Image.open('image.jpg')
# Display the image
img.show()

In this code, Image.open('image.jpg') opens and identifies the given image file, while img.show() displays it. If the file cannot be opened and identified, an OSError exception is raised.

Python Image Display

2. Using OpenCV to Display Images in Python

OpenCV is a library of programming functions for real-time computer vision applications. It’s a powerful tool for image processing that allows you to manipulate images in various ways, including resizing, cropping, and rotating, and apply thousands of machine learning algorithms to images and videos. A basic feature of OpenCV is its ability to display images.

To start with, you need to install OpenCV. You can do this by typing pip install opencv-python in your terminal. After installation, we can use the imread function to read the image and imshow function to display the image.

import cv2


# Load an image
img = cv2.imread('image.jpg')


# Display the image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Python Image Display

In the code above, the waitKey(0) function waits until the user presses a key, while destroyAllWindows() closes all the windows we created. If the image path is wrong, imread will return an empty matrix.

3. Using Matplotlib to Display Images in Python

Matplotlib is a plotting library for Python and is often used for data visualization. However, it also offers functionality for image reading and displaying. Unlike OpenCV, it displays images in RGB mode, making it more suitable for image processing tasks that require color differentiation.

Firstly, you need to install Matplotlib with pip install matplotlib. After that, you can use the pyplot module’s imshow function to display the image.

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


# Load an image
img = mpimg.imread('image.jpg')


# Display the image
imgplot = plt.imshow(img)
plt.show()

In this code, mpimg.imread('image.jpg') reads the image, and plt.imshow(img) displays the image. plt.show() finally renders the image.

Python Image Display

Note: If you get the warning “FigureCanvasAgg is non-interactive”, use the following command in Linux/Debian: pip install PyQt5

4. Using Scikit-Image to Display Images in Python

Scikit-Image is another powerful library for image processing in Python. It provides a comprehensive set of tools for image processing, including utilities to display images.

First, install Scikit-Image with pip install scikit-image. After that, you can use io.imread to read an image and io.imshow to display it.

from skimage import io


# Load an image
img = io.imread('image.jpg')


# Display the image
io.imshow(img)
io.show()

Python Image Display

In this code, io.imread('image.jpg') reads the image, and io.imshow(img) displays it. io.show() renders the image.

5. Using Tensorflow to Display Images in Python

Tensorflow is a powerful framework for machine learning and neural networks, and it also supports image processing. You can read and display images using the Tensorflow Keras API’s load_img and imshow functions.

First, install Tensorflow with pip install tensorflow. To install the CPU version, use pip install tensorflow-cpu. Then, use the load_img function to load an image and imshow function to display it.

import tensorflow as tf
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
# Load an image
img = image.load_img('image.jpg')


# Display the image
plt.imshow(img)
plt.show()

In this code, image.load_img('image.jpg') loads an image in PIL format. plt.imshow(img) and plt.show() are used to display the image.

Python Image Display

Related content: Read our guide to image management

Fully Automated Python Image Resize and Cropping with Cloudinary

Displaying images in Python is simple enough, but when you need to crop, resize, or apply other manipulations in standard Python libraries, you’ll find it can be quite difficult, especially at large scale.

Cloudinary is a cloud-based service for managing images and videos, which offers a generous free plan and a Python SDK. It lets you upload your images and apply built-in effects, filters, and modifications. You can also resize images automatically, focusing on the most important elements with AI or adapt them to your website design without having to manually crop or scale them.

You can set the target dimensions of your resized image by specifying width, height, and/or the target aspect ratio as qualifiers of your resize transformation.

You can change the dimensions of an uploaded image by setting the image’s height, width, and/or aspect ratio, and Cloudinary automatically resizes or crops the image to fit into the requested size.

For example, this original image is 1200 x 1200 pixels:

Python Image Display

Resizing the image to 200 x 200 pixels, using crop, scale, fill and pad results in the following images:

Python Image Display

Get started with automated resize and cropping today!

  1. Get a free Cloudinary account
  1. Install the Python SDK
  1. Deliver the crop transformations shown above as follows:
    # Focus on the model in a portrait crop.

    CloudinaryImage("docs/model.jpg").image(gravity="person", height=600, width=450, crop="fill")

    # Detect the face for a thumbnail crop.

    CloudinaryImage("docs/model.jpg").image(gravity="face", height=250, width=250, crop="thumb")

    # Crop to a banner, automatically focusing on a region of interest.

    CloudinaryImage("docs/model.jpg").image(gravity="auto", height=150, width=600, crop="fill")

Learn more in Cloudinary’s Python quick start guide

Last updated: Mar 19, 2024