File size: 1,594 Bytes
bdb450e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import cv2

# Function to resize image
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]

    if width is None and height is None:
        return image

    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))

    resized = cv2.resize(image, dim, interpolation=inter)
    return resized

# Function to convert and resize images
def convert_and_resize_images_in_folder(folder_path):
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(('.png', '.jpeg', '.bmp', '.tiff', '.gif', '.jpg')):
            file_path = os.path.join(folder_path, filename)
            
            # Read the image
            image = cv2.imread(file_path)
            if image is None:
                print(f"Error reading {file_path}")
                continue
            
            # Resize the image
            resized_image = image_resize(image, height=1080)
            
            # Construct the output file name
            base, ext = os.path.splitext(filename)
            output_file_path = os.path.join(folder_path, base + '.jpg')
            
            # Save the image in JPEG format
            cv2.imwrite(output_file_path, resized_image)
            print(f"Converted and resized {file_path} to {output_file_path}")

if __name__ == "__main__":
    folder_path = '.'  # current folder
    convert_and_resize_images_in_folder(folder_path)