import cv2 import numpy as np
def resizeAndPad(img, size, padColor=0): h, w = img.shape[:2] sh, sw = size
# interpolation method if h > sh or w > sw: # shrinking image interp = cv2.INTER_AREA else: # stretching image interp = cv2.INTER_CUBIC
# aspect ratio of image aspect = w / h # if on Python 2, you might need to cast as a float: float(w)/h
# compute scaling and pad sizing if aspect > 1: # horizontal image new_w = sw new_h = np.round(new_w / aspect).astype(int) pad_vert = (sh - new_h) / 2 pad_top, pad_bot = np.floor(pad_vert).astype(int), np.ceil(pad_vert).astype(int) pad_left, pad_right = 0, 0 elif aspect < 1: # vertical image new_h = sh new_w = np.round(new_h * aspect).astype(int) pad_horz = (sw - new_w) / 2 pad_left, pad_right = np.floor(pad_horz).astype(int), np.ceil(pad_horz).astype(int) pad_top, pad_bot = 0, 0 else: # square image new_h, new_w = sh, sw pad_left, pad_right, pad_top, pad_bot = 0, 0, 0, 0
# set pad color if len(img.shape) is 3 and not isinstance(padColor, (list, tuple, np.ndarray)): # color image but only one color provided padColor = [padColor] * 3
# scale and pad scaled_img = cv2.resize(img, (new_w, new_h), interpolation=interp) scaled_img = cv2.copyMakeBorder(scaled_img, pad_top, pad_bot, pad_left, pad_right, borderType=cv2.BORDER_CONSTANT, value=padColor)
return scaled_img
def resize_img_keep_ratio(img_name, target_size, color=(255, 255, 255)): img = cv2.imread(img_name) old_size = img.shape[0:2] # ratio = min(float(target_size)/(old_size)) ratio = min(float(target_size[i]) / (old_size[i]) for i in range(len(old_size))) new_size = tuple([int(i * ratio) for i in old_size]) img = cv2.resize(img, (new_size[1], new_size[0])) pad_w = target_size[1] - new_size[1] pad_h = target_size[0] - new_size[0] top, bottom = pad_h // 2, pad_h - (pad_h // 2) left, right = pad_w // 2, pad_w - (pad_w // 2) img_new = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, None, color) return img_new
if __name__ == '__main__': # 第一种方法 v_img = cv2.imread('v.jpg') # vertical image scaled_v_img = resizeAndPad(v_img, (200, 200), 127)
# 第二种方法(推荐) img = 'D:/test1.jpg' target_size = [500, 100] new_image = resize_img_keep_ratio(img, target_size) cv2.imshow('result', new_image) cv2.waitKey(0)