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:
- Using Pillow (PIL)
- Using cv2
- Using imageio.v3
- 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
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()
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()
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()
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()
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:
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:
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()
That’s all!







