In Python there are several libraries for handling images, such as OpenCV and Pillow (PIL). This section explains how to get the image size (width and height) for each of them.
You can get the image size (width and height) as a tuple using shape for OpenCV and size for Pillow (PIL), but note that the order of each is different.
The following information is provided here.
- OpenCV
ndarray.shape
:Get the image size (width, height)- For color images
- For grayscale (monochrome) images
- Pillow(PIL)
size
,width
,height
:Get the image size (width, height)
See the following article on how to get the size (capacity) of a file instead of the image size (size).
- Related Articles:Getting the size of a file or directory (folder) in Python
OpenCV: ndarray.shape: Get the image size (width, height)
When an image file is loaded in OpenCV, it is treated as a NumPy array ndarray, and the size of the image (width and height) can be obtained from the attribute shape, which indicates the shape of the ndarray.
Not only in OpenCV, but also when an image file is loaded in Pillow and converted to an ndarray, the size of the image represented by the ndarray is obtained using shape.
For color images
In the case of color images, the following three-dimensional ndarray is used.
- Row (height)
- Row (width)
- Color (3)
shape is a tuple of the above elements.
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, the above may be conventionally assigned as a variable for values that will not be used thereafter. For example, if the number of colors (number of channels) is not used, the following is used.
h, w, _ = im.shape print('width: ', w) print('height:', h) # width: 400 # height: 225
It can also be used as is by specifying it by index (index) without assigning it to a variable.
print('width: ', im.shape[1]) print('height:', im.shape[0]) # width: 400 # height: 225
(width, height)
If you want to get this tuple, you can use slice and write the following: cv2.resize(), etc. If you want to specify the argument by size, use this.
print(im.shape[1::-1]) # (400, 225)
For grayscale (monochrome) images
In the case of grayscale (monochrome) images, the following two-dimensional ndarray is used.
- Row (height)
- Row (width)
The shape will be this tuple.
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 the width and height to variables, you can do so as follows, whether the image is in color or grayscale.
h, w = im.shape[0], im.shape[1] print('width: ', w) print('height:', h) # width: 400 # height: 225
(width, height)
If you want to get this tuple, you can use slices and write it as follows. The following writing style can be used whether the image is in color or grayscale.
print(im_gray.shape[::-1]) print(im_gray.shape[1::-1]) # (400, 225) # (400, 225)
Pillow(PIL): size, width, height: Get the image size (width, height)
Image object obtained by reading an image with Pillow(PIL) has the following attributes.
size
width
height
The size is the following tuple.(width, height)
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 respectively as attributes.width
, height
print('width: ', im.width) print('height:', im.height) # width: 400 # height: 225
The same is true 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