File size: 880 Bytes
b2f8c9b
 
 
 
e1a3400
b2f8c9b
 
 
 
 
 
 
 
e1a3400
b2f8c9b
 
e1a3400
b2f8c9b
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import cv2
import os


# A function to resize the images with longer side to 300 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 = 300
                new_width = int(width * new_height / height)
            else:
                new_width = 300
                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')