Python opencv2 (cv2) wrapper get image size?

How to get size of image in cv2 wrapper in Python OpenCV (numpy). Is there a correct way to do that than numpy.shape(). How to get it in format dimensions: (width, height) list?


cv2 uses numpy for manipulating images, so the proper and best way to get the size of an image is using numpy.shape. Assuming you are working with BGR images, here is an example:

    >>> import numpy as np
    >>> import cv2
    >>> img = cv2.imread('foo.jpg')
    >>> height, width, channels = img.shape
    >>> print height, width, channels
      600 800 3

In case you were working with binary images, img will have two dimensions, and therefore you must change the code to: height, width = img.shape


Back to homepage or read more recommendations: