Image contains pixel values in the form of a matrix and the size of this matrix varies from image to image. Sometimes, you might get stuck when you want to get the size of an image in Python. In this short tutorial, we will learn various methods to get the size of an image in Python. We will go through each method and explain it in detail.
How To Get The Size of an Image in Python?
As you know an image is nothing, just a matrix containing pixel values. For RGB images, there are three matrices that overlap one another and form a colorful image.

As shown above, the image has pixel values in the form of a matrix. Finding the size of the image simply means finding the size of the matrix that contains the pixel values. Here we will go through various methods that will help us to get the size of an image in Python.
Method-1: Using the IO module
The IO is a module in Python that help us to manage input and output related to files. We can use this module to find the size of an image. First, let us take an example image and find its size.
# importing the modules
import io
from imgsize import get_size
# opening the image
with io.open('sample.jpg', 'rb') as fobj:
width, height = get_size(fobj)
print(width)
print(height)
Output:
765
326
We have used the IO module to open the sample.jpg image. Let us explain the code step by step.
- The
io
module provides a way to handle file input and output operations in Python, whileimgsize
is a third-party library for getting the dimensions of an image file. - Next, the code opens an image file named
sample.jpg
in binary mode using theio.open()
function and assigns the file object to the variablefobj
. Therb
mode used indicates that the file is opened for reading in binary mode. - The
get_size()
function from theimgsize
module is then called with the file objectfobj
as its argument. The function returns the width and height of the image in pixels as a tuple. The values are assigned to the variableswidth
andheight
respectively. - Finally, the code prints the width and height of the image using the
print()
function
Method-2: Using the PIL module
The PIL is a Python module that helps us to deal with images. We can use the PIL module to get the size of an image in Python. Let us first get the size and then we will explain the code.
from PIL import Image
# get the size of an image in python
with Image.open("sample.jpg") as img:
width, height = img.size
print(f"Image size: {width} x {height}")
Output:
Image size: 765 x 326
This method uses the PIL (Python Imaging Library) library to get the size of an image.
- First, it imports the Image module from the PIL library.
- Then, the code opens the sample.jpg file using the Image.open() method and assigns the resulting image object to the img variable. The with statement is used to ensure that the image file is properly closed after its use.
- The size attribute of the image object is then accessed using the img.size syntax. This returns a tuple containing the width and height of the image in pixels, which are assigned to the width and height variables.
- Finally, the code uses an f-string to print the width and height of the image to the console.
Method-3: Using imageio module
The imageio is a Python module that helps us to read, write and deal with a large set of images. We can use the imageio module to get the size of the image as shown below:
import imageio
# get the size of an image in Python
with imageio.imread("sample.jpg") as img:
height, width, _ = img.shape
print(f"Image size: {width} x {height}")
Output:
Image size: 765 x 326
The method uses the imageio library to get the size of an image file in Python. Let us explain each step in detail.
- The imageio library is first imported at the beginning of the code.
- The code then uses the imread() function from the imageio library to read the image file and store the data in the img variable. The imread() function reads the image data in as a NumPy array.
- The shape attribute of the img variable is then used to extract the dimensions of the image. The shape attribute returns a tuple with the dimensions of the array, including the height, width, and number of color channels.
- The dimensions are then extracted from the tuple using unpacking and assigned to the height and width variables.
- The dimensions of the image are printed to the console using the print() function.
Method-4: Using OpenCV Module
OpenCV is also another import library in Python that helps us to deal with images. We can use this module to find the size of an image.
import cv2
# get the size of an image in Python
img = cv2.imread("sample.jpg")
height, width, _ = img.shape
print(f"Image size: {width} x {height}")
Output:
Image size: 765 x 326
Similar to other methods, this method also helps us to find the size of an image. We have first imported the CV2 module and then used the imread() method to read the image and store the data in a variable and then used the shape() method to get the size of the image.
Method-5: Using the struck module
The struck module in Python is used to convert data types. We can use the struck module along with imgsize module to get the size of an image. Let us first find the size of an image and then we will explain the code in detail.
# importing the modules
import struct
from imgsize.core import ImageSize
from imgsize.formats import jpg, gif, png, bmp
from imgsize.formats import signature, Struct
Size = Struct('<II')
@signature('Example', struct.pack('<BBBBBBB', 0x64, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65))
def get_size_example(cls, fobj):
return Size.unpack_from(fobj)
# get the size of an image in python
imgsize = ImageSize()
imgsize.register(jpg.get_size)
imgsize.register(gif.get_size)
imgsize.register(png.get_size)
imgsize.register(bmp.get_size)
imgsize.register(get_size_example)
with io.open('sample.jpg', 'rb') as fobj:
width, height = imgsize.get_size(fobj)
print(width)
print(height)
Output:
765
326
As you can see, we got the size of the image. Let us now jump into the explanation of the code.
This method is also for getting the size of an image file, but it’s using a more complex approach involving the imgsize library.
- The code first imports the necessary modules: struct, imgsize.core, imgsize.formats.jpg, imgsize.formats.gif, imgsize.formats.png, and imgsize.formats.bmp.
- Next, the code defines a Size object that is created using the struct module. The object contains two unsigned integer fields and is set up to use little-endian byte order.
- The @signature decorator is then used to define a custom signature for the get_size_example() function. The signature is a string of bytes that identifies the file format, and it is packed using the struct.pack() function. In this case, the signature is ‘Example’ and the byte string is b’dxample’.
- The get_size_example() function takes two arguments: cls and fobj. The cls argument is used to reference the class instance, while the fobj argument is the file object containing the image data. The function returns the size of the image as a tuple of width and height values using the unpack_from() method of the Size object.
- After defining the get_size_example() function, the code creates an ImageSize object and registers the built-in size-getting functions for JPG, GIF, PNG, and BMP images using the register() method.
- Finally, the custom get_size_example() function is registered using the register() method as well. The code then opens the sample.jpg file in binary mode and uses the get_size() method of the imgsize object to get the width and height of the image. The values are assigned to the variables width and height and then printed to the console using the print() function.
Summary
In Python, we can get the size of an image using various methods and modules which include OpenCV, imagio, IO, and struck. In this short article, we discussed 5 methods to get the size of an image in Python. We explained each method by taking examples.
Related Articles
- How to make Jarvis with Dialogflow and Python?
- How to create a lattice in Python?
- 5 ways to get a list of locally installed Python modules
- How to convert Decimal to octal in Python?
- Reverse List in Python Without Using inbuilt function