Python opencv (3) get image size

The shape attribute of the image matrix

represents the size of the image, and shape returns the tuple tuple. The first element represents the number of rows of the matrix, the second tuple represents the number of columns of the matrix, and the third element is 3, indicating that the pixel value consists of the three primary colors of light.

import cv2
import numpy as np
fn="baboon.jpg"
if __name__ == '__main__':
    print 'load %s as ...' % fn
    img = cv2.imread(fn)
    sp = img.shape
    print sp
    sz1 = sp[0]#height(rows) of image
    sz2 = sp[1]#width(colums) of image
    sz3 = sp[2]#the pixels value is made up of three primary colors
    print 'width: %d \nheight: %d \nnumber: %d' %(sz1,sz2,sz3)

运行结果:
加载baboon.jpg…

(512、512、3)宽度:512
身高:512年
数字:3

Read More: