Get image size (width, height) with Python, OpenCV, Pillow (PIL)

Modified: | Tags: Python, Image Processing, OpenCV, Pillow, NumPy

This article explains how to get the image size (width and height) in Python with OpenCV and Pillow (PIL).

You can obtain the image size as a tuple using the shape attribute of ndarray in OpenCV and the size attribute of PIL.Image in Pillow (PIL). It's important to note that the order of width and height is different in OpenCV and Pillow (PIL).

Refer to the following articles for image resizing and getting the size of a file in bytes:

OpenCV: Get image size (width, height) with ndarray.shape

OpenCV treats an image as a NumPy array ndarray. The image size (width, height) can be obtained using the shape attribute, which returns a tuple of dimensions.

In addition to OpenCV, you can also obtain the image size using the shape attribute when using other libraries, such as Pillow, to read an image file and convert it into an ndarray.

Note that OpenCV loads color image files in BGR order, not RGB.

For color images

For color images, the ndarray is a 3D array with dimensions (height, width, 3).

import cv2

im = cv2.imread('data/src/lena.jpg')

print(type(im))
# <class 'numpy.ndarray'>

print(im.shape)
print(type(im.shape))
# (225, 400, 3)
# <class 'tuple'>

To assign each value to a variable, unpack the tuple as follows:

h, w, c = im.shape
print('width:  ', w)
print('height: ', h)
print('channel:', c)
# width:   400
# height:  225
# channel: 3

When unpacking a tuple, it is a common convention to assign unused values to _. In the following example, the number of colors (or channels) is not used:

h, w, _ = im.shape
print('width: ', w)
print('height:', h)
# width:  400
# height: 225

Of course, you can also directly access them by index.

print('width: ', im.shape[1])
print('height:', im.shape[0])
# width:  400
# height: 225

If you want to get tuples in the order of (width, height), you can use slicing, as demonstrated in the following example:

print(im.shape[1::-1])
# (400, 225)

When passing the size as an argument to functions such as cv2.resize(), ensure it follows the (width, height) format.

See the following article for details of slicing:

For grayscale (monochrome) images

For grayscale images, the ndarray is a 2D array with dimensions (height, width).

import cv2

im_gray = cv2.imread('data/src/lena.jpg', cv2.IMREAD_GRAYSCALE)

print(im_gray.shape)
print(type(im_gray.shape))
# (225, 400)
# <class 'tuple'>

The procedure for grayscale images is essentially the same as for color images:

h, w = im_gray.shape
print('width: ', w)
print('height:', h)
# width:  400
# height: 225

print('width: ', im_gray.shape[1])
print('height:', im_gray.shape[0])
# width:  400
# height: 225

To assign width and height to variables, the following approach works for both color and grayscale images.

h, w = im.shape[0], im.shape[1]
print('width: ', w)
print('height:', h)
# width:  400
# height: 225

If you want to get a (width, height) tuple, you can use slicing. The latter approach ([1::-1]) works for both color and grayscale images.

print(im_gray.shape[::-1])
print(im_gray.shape[1::-1])
# (400, 225)
# (400, 225)

Pillow (PIL): Get image size (width, height) with size, width, height

A PIL.Image object, obtained by reading an image using Pillow (PIL), has the size, width, and height attributes.

The size attribute returns a (width, height) tuple.

from PIL import Image

im = Image.open('data/src/lena.jpg')

print(im.size)
print(type(im.size))
# (400, 225)
# <class 'tuple'>

w, h = im.size
print('width: ', w)
print('height:', h)
# width:  400
# height: 225

You can also get the width and height using the width and height attributes:

print('width: ', im.width)
print('height:', im.height)
# width:  400
# height: 225

The process is the same for grayscale (monochrome) images:

im_gray = Image.open('data/src/lena.jpg').convert('L')

print(im.size)
print('width: ', im.width)
print('height:', im.height)
# (400, 225)
# width:  400
# height: 225

Related Categories

Related Articles