Creating a bulk image resizer can be done efficiently using Python with the help of libraries like Pillow for image processing. Below is a detailed, fully functional Python script that resizes all images in a given folder to a specified size and saves them into an output folder.
Explanation:
-
Pillow (PIL) library is used for opening and resizing images.
-
The script scans through all image files in the
input_folder. -
It resizes each image:
-
If
keep_aspect_ratioisTrue, it usesthumbnail()which maintains aspect ratio. -
If
False, it forces the image to the exact dimensions.
-
-
Resized images are saved to the
output_folderwith the same filename. -
Handles common image formats.
-
Errors during processing individual images are caught and printed.
How to use:
-
Install Pillow if you don’t have it:
-
Place images in a folder, e.g.,
input_images. -
Adjust
input_dir,output_dir,width, andheightin the script or pass them dynamically. -
Run the script.
This script can be further enhanced with GUI, multi-threading, or CLI arguments if needed. Would you like me to add that?