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 article 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
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))
.
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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.
1 2 3 4 5 6 7 8 9 | h, w, c = im.shape print('width: ', w) print('height: ', h) print('channel:', c) # width: 400 # height: 225 # channel: 3 |
If you want to assign width and height to variables, you can apply the following to either color or grayscale images:
1 2 3 4 5 6 7 | 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.
1 2 3 4 5 6 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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
.
1 2 3 4 5 6 | print('width: ', im.width) print('height:', im.height) # width: 400 # height: 225 |
Grayscale (monochrome) images can be processed in the same way.
1 2 3 4 5 6 7 8 9 10 | 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 |
Source: https://note.nkmk.me/en/python-opencv-pillow-image-size/