Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Read an Image in Python using Various Ways

  • 09 Oct, 2025
  • Com 0
Read an Image in Python

In Python, reading an image involves loading an image file (e.g., JPEG, PNG, GIF) from disk or a URL into memory as a numerical array or image object.

Here are four ways to read an image:

  1. Using Pillow (PIL)
  2. Using cv2
  3. Using imageio.v3
  4. Using matplotlib

Method 1: Using Pillow (PIL)

Pillow is a fork of PIL that provides an Image.open() method that you can use to load an image. Internally, it reads an image as a numpy array, and then we can modify that array using different operations. Based on these operations, the image will be edited.

from PIL import Image

img = Image.open('krunal.jpg')

print(img.format, img.size, img.mode)
# Output: JPEG (1196, 1196) RGB 

img.show()  # Displays the image

Displaying an image using Pillow

Grayscale image

For loading an image as grayscale, chain the Image.open() with .convert() method and pass mode=’L’.

For displaying an image, we used the PIL’s .show() method.

from PIL import Image

gray_img = Image.open('krunal.jpg').convert('L')

print(gray_img.format, gray_img.size, gray_img.mode)

# Output: None (1196, 1196) L

gray_img.show()

Loading an image as grayscale using Pillow

Reading an image from a URL

If your input image resides in a remote location, you may need to load it from a URL.

That’s where the requests module and the BytesIO modules are helpful, along with Pillow, of course.

The requests module helps us send a GET request to the remote server, and BytesIO helps us read the image in bytes.

Finally, PIL’s image.open() method reads the bytes in a structured format.

from PIL import Image
import requests
from io import BytesIO

response = requests.get('https://appdividend.com/wp-content/uploads/2024/10/krunal.jpg')

img = Image.open(BytesIO(response.content))

print(img.format, img.size, img.mode)
# Output: JPEG (1196, 1196) RGB

img.show()

Reading an image from URL using Pillow

Method 2: Using OpenCV (cv2)

The cv2 module provides the .imread() method to read or load an image as a NumPy array in BGR order (not RGB). For displaying the image, we can use the cv2.imshow() method.

import cv2

img = cv2.imread('krunal.jpg')  # Returns NumPy array (height, width, channels)

print(img.shape)
# Output: (1196, 1196, 3)

cv2.imshow('Image', img)  # Displays in a window
cv2.waitKey(0)  # Wait for key press
cv2.destroyAllWindows()

Read an image using cv2.imread() method

In this code, we printed the shape of the image using the img.shape attribute.

Here, you can press any key on the keyboard to stop the image from displaying.

Loading a grayscale image

By passing flag 0 to the cv2.imread() method, it will load it as a grayscale image.

import cv2

gray_img = cv2.imread('krunal.jpg', 0)

print(gray_img.shape)
# Output: (1196, 1196)

cv2.imshow('Image', gray_img)  # Displays in a window
cv2.waitKey(0)  # Wait for key press
cv2.destroyAllWindows()

Gray image using cv2.imread() method

Method 3: Using imageio.v3

The imageio.v3 is a modern version of the imageio library that provides the .imread() method to load an image as a numpy array. It is simple and supports GIFs/videos too.

Here is the homer.gif file that we will use for this program:

gif image

import imageio.v3 as iio
import matplotlib.pyplot as plt

# Read all frames from the image (e.g., GIF)
img = iio.imread("homer.gif")

print("Image shape:", img.shape)  
# Output: Image shape: (10, 320, 320, 3)

# Display each frame one by one
for i, frame in enumerate(img):
    plt.imshow(frame)
    plt.axis("off")
    plt.title(f"Frame {i+1}/{len(img)}")
    plt.draw()
    plt.pause(0.3)   # Pause 0.3 sec between frames (adjust speed)
    plt.clf()        # Clear the figure for the next frame

plt.close()

Here is the output video that shows the rendering of a GIF on the canvas:

https://appdividend.com/wp-content/uploads/2025/10/gif-recording.mp4

To display the loaded GIF image, we have used the matplotlib library, as it provides a canvas on which we can display the image.

Since the image is a GIF, it has multiple frames. So, at 0.3 seconds, it will change the frame in the display canvas.

Method 4: Using Matplotlib

You can use matplotlib’s plt.imread() method for plotting, loading as NumPy arrays (RGB or float).

import matplotlib.pyplot as plt

img = plt.imread('krunal.jpg')  # NumPy array (height, width, channels)

print(img.shape)
# Output: (1196, 1196, 3)

plt.imshow(img)

plt.show()

Using Matplotlib

That’s all!

Post Views: 34
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.save(): Saving an Array to a File
How to Close a File in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend