The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a bulk image resizer

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.


python
import os from PIL import Image def bulk_image_resizer(input_folder, output_folder, target_width, target_height, keep_aspect_ratio=True): """ Resize all images in the input_folder and save them to the output_folder. Args: input_folder (str): Path to the folder containing original images. output_folder (str): Path to the folder where resized images will be saved. target_width (int): The desired width of resized images. target_height (int): The desired height of resized images. keep_aspect_ratio (bool): Whether to maintain aspect ratio or force resize. """ # Create output folder if it does not exist os.makedirs(output_folder, exist_ok=True) # Supported image extensions valid_extensions = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff') for filename in os.listdir(input_folder): if filename.lower().endswith(valid_extensions): input_path = os.path.join(input_folder, filename) output_path = os.path.join(output_folder, filename) try: with Image.open(input_path) as img: if keep_aspect_ratio: img.thumbnail((target_width, target_height)) else: img = img.resize((target_width, target_height)) img.save(output_path) print(f"Resized and saved: {filename}") except Exception as e: print(f"Error processing {filename}: {e}") if __name__ == "__main__": # Example usage: input_dir = "input_images" # Folder with images to resize output_dir = "resized_images" # Folder to save resized images width = 800 # Desired width height = 600 # Desired height bulk_image_resizer(input_dir, output_dir, width, height, keep_aspect_ratio=True)

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_ratio is True, it uses thumbnail() which maintains aspect ratio.

    • If False, it forces the image to the exact dimensions.

  • Resized images are saved to the output_folder with the same filename.

  • Handles common image formats.

  • Errors during processing individual images are caught and printed.


How to use:

  1. Install Pillow if you don’t have it:

    nginx
    pip install pillow
  2. Place images in a folder, e.g., input_images.

  3. Adjust input_dir, output_dir, width, and height in the script or pass them dynamically.

  4. 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?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About