import cv2 import os # A function to resize the images with longer side to 500 pixels with cubic interpolation and save to a new directory def resize_images(path, new_path): if not os.path.exists(new_path): os.makedirs(new_path) for filename in os.listdir(path): if not filename.startswith('.'): img = cv2.imread(os.path.join(path, filename)) height, width, channels = img.shape if height > width: new_height = 500 new_width = int(width * new_height / height) else: new_width = 500 new_height = int(height * new_width / width) img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC) cv2.imwrite(os.path.join(new_path, filename), img) resize_images('./images/val2014', './resized_images/val2014')