- Python Pillow – Get Image Size
- Syntax – Image.size
- Examples
- 1. Get size of given image
- 2. Access width and height from image size
- Summary
- Get image size (width, height) with Python, OpenCV, Pillow (PIL)
- OpenCV: Get image size (width, height) with ndarray.shape
- For color images
- For grayscale (monochrome) images
- Pillow (PIL): Get image size (width, height) with size , width , height
- Getting the image size (width and height) with Python, OpenCV and Pillow(PIL)
- 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)
Python Pillow – Get Image Size
To get the size of image using Python Pillow, use size property of Image object. The size property returns the width and height of the image.
In this tutorial, we shall learn how to get the size of image, in other words, width and height of an image, using Pillow library.
Syntax – Image.size
The syntax to use size property of PIL Image object is given below.
im = Image.open("sample-image.png") im.size
Examples
1. Get size of given image
In the following program, we will read and image and then print its size using size property of the Image.
Python Program
from PIL import Image #read the image im = Image.open("sample-image.png") #image size print(im.size)
2. Access width and height from image size
You can access the height and width from size property using index. In the following example, we will get width and height of the image.
Python Program
from PIL import Image #read the image im = Image.open("sample-image.png") #image size width = im.size[0] height = im.size[1] print('Width of the image is:', width) print('Height of the image is:', height)
Width of the image is: 640 Height of the image is: 400
Summary
In this tutorial of Python Examples, we learned how to get the size of an image using Python Pillow library, with the help of well detailed example programs.
Get image size (width, height) with Python, OpenCV, Pillow (PIL)
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)) # print(im.shape) print(type(im.shape)) # (225, 400, 3) #
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:
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) #
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) # 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
Getting the image size (width and height) with Python, OpenCV and Pillow(PIL)
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
- 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).
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.
shape is a tuple of the above elements.
import cv2 im = cv2.imread('data/src/lena.jpg') print(type(im)) # print(im.shape) print(type(im.shape)) # (225, 400, 3) #
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.For grayscale (monochrome) images
In the case of grayscale (monochrome) images, the following two-dimensional ndarray is used.
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) #
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.
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) # 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 , heightprint('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
- ndarray.shape : Get the image size (width, height)