File size: 1,377 Bytes
d00d87c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from PIL import Image
from tqdm import tqdm


def resize_images_in_directory(directory, size):
    """
    Resizes all images in the specified directory to the given size.

    Parameters:
    directory (str): The path to the directory containing images.
    size (tuple): The desired size for the resized images, e.g., (512, 512).
    """
    if not os.path.exists(directory):
        print(f"The directory {directory} does not exist.")
        return

    # Get list of all files in the directory
    files = os.listdir(directory)

    # Filter out non-image files
    image_files = [
        file
        for file in files
        if file.lower().endswith(("png", "jpg", "jpeg", "tiff", "bmp", "gif"))
    ]

    if not image_files:
        print("No image files found in the directory.")
        return

    for image_file in tqdm(image_files):
        image_path = os.path.join(directory, image_file)
        try:
            with Image.open(image_path) as img:
                img_resized = img.resize(size, Image.ANTIALIAS)
                img_resized.save(image_path)
        except Exception as e:
            print(f"Failed to process {image_file}: {e}")


if __name__ == "__main__":
    directory = "Validation/CGI"  # Replace with your directory path
    size = (256, 256)  # Replace with your desired size
    resize_images_in_directory(directory, size)