How to find width and height of an image using Python?
Finding the width and height of an image in Python means reading the image and determining its dimensions in pixels. For example an image might have a height of 135 pixels and a width of 600 pixels. Let’s explore some common methods to find an image’s width and height.
1. Using OpenCV (cv2)
OpenCV is useful and fast computer vision library. It allows you to read an image and easily access its dimensions. The cv2.imread() function loads the image and .shape property gives you a tuple containing the image's height, width and number of color channels.
Image Used:

import cv2
path = "image.jpg"
img = cv2.imread(path)
a,b = img.shape[:2]
print(a,b)
Output:
135,600
We read the image using cv2.imread(path) which loads it as a multi-dimensional array of pixel data. The .shape property returns the image dimensions with the first value as height and the second as width.
2. Using Pillow ( PIL)
Pillow (PIL) is a popular Python library for basic image processing. It can open an image and directly provide its dimensions. The .size property returns a tuple (width, height). We will use same image used above.
from PIL import Image
path = "image.jpg"
img = Image.open(path)
a, b = img.size
print(a,b)
Output:
135,600
We open the image using Image.open(path) which loads it as a PIL image object. The .size property returns a tuple with width and height. We unpack these as a, b = img.size.
3. Using ImageMagick's identify
ImageMagickis an external tool commonly used for fast image processing via command line. The identify command can quickly return an image’s width and height. In Python you can use the subprocess module to run this command and capture the result.
Image Used:

import subprocess
path = "image.jpg"
res = subprocess.check_output(['identify', '-format', '%w %h', path])
a, b = map(int, res.split())
print(a,b)
Output:
400,400
We run the identify command via subprocess.check_output which returns the width and height as a string. We split this string and convert the values to integers using map(int, res.split()).
4. Using imread()
Matplotlib is a popular library for data visualization but it can also read images as NumPy arrays using plt.imread(). You can then access the image dimensions using .shape. We will use same image used above.
import matplotlib.pyplot as plt
path = "image.jpg"
img = plt.imread(path)
a, b = img.shape[:2]
print(a,b)
import matplotlib.pyplot as plt
path = "image.jpg"
img = plt.imread(path)
a, b = img.shape[:2]
print(a,b)
Output:
400,400
We read the image with plt.imread(path) which loads it as a NumPy array of pixel data. The .shape[:2] gives the height and width which we extract as a, b.