note.nkmk.me

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

Posted: 2019-05-14 / Tags: Python, Image Processing, OpenCV, Pillow, NumPy

Python has a library that handles images such as OpenCV and Pillow (PIL). Here, the method of acquiring the image size (width, height) will be described.

In OpenCV, the image size (width, height) can be obtained as a tuple with the attribute shape of ndarray and the attribute size of PIL.Image in Pillow (PIL). Note that the order of width and height is different.

This post describes the following contents.

  • OpenCV: Get image size (width, height) with ndarray.shape
    • For color image
    • For grayscale (monochrome) images
  • Pillow (PIL): Get image size (width, height) with size,width, height

See the following post for image resizing.

Sponsored Link

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

When an image file is read by OpenCV, it is treated as NumPy array ndarray. The size (width, height) of the image can be acquired from the attribute shape indicating the shape of ndarray.

Not limited to OpenCV, the size of the image represented by ndarray, such as when an image file is read by Pillow and converted to ndarray, is obtained by shape.

For color image

In the case of a color image, it is a 3D ndarray of row (height) x column (width) x color (3). shape is a tuple of (row (height), column (width), color (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'>

When assigning 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, values that are not used after that may be assigned to _ by convention. An example where the number of colors (number of channels) is not used is as follows.

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

It is also possible to use an index (subscript) without assigning to a variable.

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 slice like the following example.

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

When setting the size to cv2.resize() etc., it needs to be (width, height).

See the following post for details of slicing.

For grayscale (monochrome) images

For grayscale (monochrome) images, it is a 2D ndarray of rows (height) x columns (width). shape is a tuple of (row (height), column (width)).

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

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

Basically 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

If you want to assign width and height to variables, you can apply the following to either color or 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 slice. The image can be either color or grayscale if it is written as follows.

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

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

size is 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

The width and height can also be acquired with the attributes width and height.

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

Grayscale (monochrome) images can be processed in the same way.

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
Sponsored Link
Share

Related Categories

Related Posts