import cv2, math DESIRED_HEIGHT = 480 DESIRED_WIDTH = 480 def read_n_resize(image_file, read=True): image = cv2.imread(image_file) if read else image_file image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if read else image h, w = image.shape[:2] if h < w: img = cv2.resize( image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH))) ) else: img = cv2.resize( image, (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT) ) return img