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 = "Photo" # Replace with your directory path size = (512, 512) # Replace with your desired size resize_images_in_directory(directory, size)