Get Image Size (Width, Height) with Python, OpenCV, Pillow (PIL)
This article explains how to get the image size (width and height) in Python using 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 differs between 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 using ndarray.shape
In OpenCV, images are treated as NumPy arrays (ndarray
), and the image size (width and height) can be obtained from the shape
attribute, which returns a tuple of dimensions.
Besides OpenCV, if you load an image with other libraries such as Pillow and convert it into an ndarray
, you can also get the image size using the shape
attribute.
Note that OpenCV loads color image files in BGR order, not RGB.
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
Alternatively, you can directly access them by index.
print('width: ', im.shape[1])
print('height:', im.shape[0])
# width: 400
# height: 225
If you want to get a tuple 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:
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 is essentially the same for grayscale images 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 can be applied to 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 using size
, width
, height
A PIL.Image
object, obtained by reading an image using Pillow (PIL), provides 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 approach remains 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