import cv2 def img_resize(img_path): img = cv2.imread(img_path) width = img.shape[1] height = img.shape[0] if height != 48: ratio = 35 / height height = 35 width = int(width * ratio) dim = (width, height) resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) width = resized.shape[1] height = resized.shape[0] if width < 960: left = int((960 - width) / 2) top = int((48 - height) / 2) ret = cv2.copyMakeBorder(resized, top, 48 - height - top, left, 960 - width - left, cv2.BORDER_CONSTANT, value=(255, 255, 255)) cv2.imwrite(img_path, ret)