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


# A function to resize the images with longer side to num_pixels pixels with cubic interpolation and save to a new directory
def resize_images(path, new_path, num_pixels=300):
    if not os.path.exists(new_path):
        os.makedirs(new_path)
    for filename in os.listdir(path):
        if not filename.startswith('.') and (filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png')):
            img = cv2.imread(os.path.join(path, filename))
            height, width, channels = img.shape
            if height > width:
                new_height = num_pixels
                new_width = int(width * new_height / height)
            else:
                new_width = num_pixels
                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')
resize_images('./misc/article', './misc/article/resized', 500)