In this OpenCV Tutorial, we will learn how to get image size in OpenCV Python using NumPy Array shape property, with examples for color images, grayscale images, alpha channel images, and safe dimension unpacking.

OpenCV Python Get Image Size Using img.shape

In image processing applications, it is often necessary to know the size of an image that is loaded, resized, cropped, converted, or passed through a model. In OpenCV Python, an image read with cv2.imread() is stored as a NumPy ndarray, so the most direct way to get the image size is to use the ndarray shape property.

For a normal color image, img.shape returns the dimensions in this order: (height, width, channels). This is important because many OpenCV functions, such as cv2.resize(), expect size values in (width, height) order. Mixing these two orders is a common cause of wrong image dimensions.

When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape or size, use ndarray.shape to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.

Basic Syntax to Get OpenCV Image Dimensions

In the following code snippet, we have read an image to img ndarray. And then we used ndarray.shape to get the dimensions of the image.

</>
Copy
img = cv2.imread('/path/to/image.png')
dimensions = img.shape

If the image has three dimensions, you can unpack the values as height, width, and channels.

</>
Copy
height, width, channels = img.shape

Example to Find Height, Width, and Channels of an Image in OpenCV

In this example, we have read an image and used ndarray.shape to get the dimension.

OpenCV Python - Get Image Size Shape

We can access height, width and number of channels from img.shape: Height is at index 0, Width is at index 1; and number of channels at index 2.

Python Program

</>
Copy
import cv2

# read image
img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED)

# get dimensions of image
dimensions = img.shape

# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]

print('Image Dimension    : ',dimensions)
print('Image Height       : ',height)
print('Image Width        : ',width)
print('Number of Channels : ',channels)

Output

Image Dimension    :  (149, 200, 4)
Image Height       :  149
Image Width        :  200
Number of Channels :  4

img.shape returns (Height, Width, Number of Channels)

where

  1. Height represents the number of pixel rows in the image or the number of pixels in each column of the image array.
  2. Width represents the number of pixel columns in the image or the number of pixels in each row of the image array.
  3. Number of Channels represents the number of components used to represent each pixel.

In the above example, Number of Channels = 4 represent Alpha, Red, Green and Blue channels.

OpenCV img.shape Order: Height First, Width Second

The value returned by img.shape follows NumPy array order, not the usual spoken order of image size. For a color image, the result is:

</>
Copy
(height, width, channels)

For the sample image used above, (149, 200, 4) means the image has 149 rows, 200 columns, and 4 channel values for every pixel. Therefore, the image size is 200 pixels wide and 149 pixels high.

ExpressionMeaningValue in this example
img.shape[0]Image height in pixels149
img.shape[1]Image width in pixels200
img.shape[2]Number of channels4

Get Image Width and Height Safely After cv2.imread()

If the file path is wrong or the image cannot be read, cv2.imread() returns None. In that case, img.shape raises an error. It is better to check the image before reading its dimensions, especially in scripts that process many files.

</>
Copy
import cv2

img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED)

if img is None:
    raise FileNotFoundError('Image could not be loaded. Check the file path.')

height = img.shape[0]
width = img.shape[1]

print('Width  : ', width)
print('Height : ', height)
Width  :  200
Height :  149

This example prints the image size in the common width x height style while still reading the values from the OpenCV array in height, width order.

Get Size of Grayscale Image in OpenCV Python

A grayscale image usually has only two dimensions in OpenCV: (height, width). It does not have a channel value at index 2. If you try to access img.shape[2] for a grayscale image, you may get an index error.

</>
Copy
import cv2

img = cv2.imread('/home/img/python.png', cv2.IMREAD_GRAYSCALE)

if img is None:
    raise FileNotFoundError('Image could not be loaded. Check the file path.')

height, width = img.shape

print('Image Dimension : ', img.shape)
print('Image Height    : ', height)
print('Image Width     : ', width)
Image Dimension :  (149, 200)
Image Height    :  149
Image Width     :  200

When you write reusable image processing code, handle both grayscale and color images. A simple way is to check the length of img.shape.

</>
Copy
if len(img.shape) == 2:
    height, width = img.shape
    channels = 1
else:
    height, width, channels = img.shape

print(height, width, channels)

Get OpenCV Image Size as Width x Height

Some programs, image viewers, and documentation describe image size as width x height. OpenCV’s shape values can be converted to this format easily.

</>
Copy
import cv2

img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED)

if img is None:
    raise FileNotFoundError('Image could not be loaded. Check the file path.')

height, width = img.shape[:2]

print(f'Image size: {width} x {height} pixels')
Image size: 200 x 149 pixels

The slice img.shape[:2] is useful because it works for both grayscale and color images. It always gives the first two dimensions: height and width.

Difference Between img.shape, img.size, and img.dtype in OpenCV

When using NumPy arrays in OpenCV, shape, size, and dtype mean different things. Use shape to get image dimensions.

PropertyWhat it returnsExample meaning
img.shapeArray dimensions(149, 200, 4) means height 149, width 200, 4 channels
img.sizeTotal number of array elements149 * 200 * 4
img.dtypeData type of pixel valuesUsually uint8 for standard images

Do not use img.size when you need image width and height. It returns the total count of stored values, not the pixel dimensions.

Check Image Size After Resize or Crop Operations

After resizing or cropping an image, use shape again to verify the new dimensions. This is useful when preparing images for computer vision models, thumbnails, or fixed-size processing pipelines.

</>
Copy
import cv2

img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED)

if img is None:
    raise FileNotFoundError('Image could not be loaded. Check the file path.')

resized = cv2.resize(img, (100, 80))

original_height, original_width = img.shape[:2]
new_height, new_width = resized.shape[:2]

print('Original size : ', original_width, 'x', original_height)
print('Resized size  : ', new_width, 'x', new_height)
Original size :  200 x 149
Resized size  :  100 x 80

OpenCV Image Size FAQs

How do I get the width and height of an image in OpenCV Python?

Read the image using cv2.imread(), then use height, width = img.shape[:2]. The first value is height and the second value is width.

Why does OpenCV img.shape return height before width?

OpenCV images are NumPy arrays, and array shape follows row-column order. Rows represent image height and columns represent image width, so the order is (height, width) for grayscale images and (height, width, channels) for color images.

How can I get the number of channels in an OpenCV image?

For a color image, use img.shape[2]. For grayscale images, there may be no third dimension, so check len(img.shape) before accessing the channel index.

What is the difference between image size and file size in OpenCV?

Image size usually means pixel dimensions such as width and height. File size means how much storage space the image file uses on disk, such as KB or MB. img.shape gives pixel dimensions, not file size.

Can img.shape measure the real-world size of an object in an image?

No. img.shape gives the pixel dimensions of the full image array. Measuring a real-world object requires calibration, reference scale, or camera geometry in addition to pixel measurements.

Editorial QA Checklist for OpenCV Image Size Tutorial

  • Verify that img.shape is explained as (height, width, channels) for color images.
  • Confirm that grayscale examples do not access img.shape[2].
  • Check that examples using cv2.imread() include a clear path-failure guard when added for production-style usage.
  • Make sure width-height display examples convert from OpenCV order correctly.
  • Keep the difference between pixel dimensions, total array elements, and file size clear.

Summary of Getting Image Size in OpenCV Python

Concluding this OpenCV Python Tutorial, we have learnt how to get the image shape using OpenCV ndarray.shape. Use img.shape[:2] when you need height and width, use img.shape[2] only when a channel dimension exists, and remember that OpenCV reports image dimensions as height first and width second.