Image size (Python, OpenCV)
I would like to get the Image size in python,as I do it with c++.
int w = src->width;
printf("%d", 'w');
Using openCV and numpy it is as easy as this:
import cv2
img = cv2.imread('path/to/img',0)
height, width = img.shape[:2]
For me the easiest way is to take all the values returned by image.shape:
height, width, channels = img.shape
if you don't want the number of channels (useful to determine if the image is bgr or grayscale) just drop the value:
height, width, _ = img.shape
Use the function GetSize
from the module cv
with your image as parameter. It returns width, height as a tuple with 2 elements:
width, height = cv.GetSize(src)
I use numpy.size() to do the same:
import numpy as np
import cv2
image = cv2.imread('image.jpg')
height = np.size(image, 0)
width = np.size(image, 1)