Image Doubling Example
There are two ways to resize an image. You can specify a new size:
Manually
height, width = src.shape[:2]
dst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)
Scaling factor.
dst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC) where fx is the scale factor along the horizontal axis and fy along the vertical axis.
To reduce the image, it will look best with INTER_AREA interpolation, while to enlarge the image, it will look best with INTER_CUBIC (slow) or INTER_LINEAR (faster, but still looks normal).
Example of image reduction for maximum height / width (maintaining proportions)
import cv2 img = cv2.imread('YOUR_PATH_TO_IMG') height, width = img.shape[:2] max_height = 300 max_width = 300 # only shrink if img is bigger than required if max_height < height or max_width < width: # get scaling factor scaling_factor = max_height / float(height) if max_width/float(width) < scaling_factor: scaling_factor = max_width / float(width) # resize image img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) cv2.imshow("Shrinked image", img) key = cv2.waitKey()
Using your code with cv2
import cv2 as cv im = cv.imread(path) height, width = im.shape[:2] thumbnail = cv.resize(im, (width/10, height/10), interpolation = cv.INTER_AREA) cv.imshow('exampleshq', thumbnail) cv.waitKey(0) cv.destroyAllWindows()
JoΓ£o Cartucho Feb 10 '17 at 17:36 2017-02-10 17:36
source share