Last modified: Apr 21, 2025 By Alexander Williams

Python Simple Image Batch Processing Guide

Batch processing images saves time. Python makes it easy. This guide shows how to automate image tasks.

Why Use Python for Batch Image Processing?

Python is great for repetitive tasks. It can handle hundreds of images quickly. Libraries like PIL simplify the work.

Batch processing means applying the same action to many files. This includes resizing, converting formats, or adding filters.

Setting Up Your Environment

First, install the Python Imaging Library (PIL). Use pip for installation:


pip install pillow

Pillow is a modern PIL fork. It supports many image formats. It's perfect for batch jobs.

Basic Batch Processing Script

Here's a simple script to resize all images in a folder. It uses the Image module from PIL.


from PIL import Image
import os

input_folder = "input_images"
output_folder = "resized_images"
new_size = (800, 600)

# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)

for filename in os.listdir(input_folder):
    if filename.endswith(('.jpg', '.png', '.jpeg')):
        img = Image.open(os.path.join(input_folder, filename))
        img_resized = img.resize(new_size)
        img_resized.save(os.path.join(output_folder, filename))

This script resizes all JPG and PNG images. It saves them in a new folder. The size is set to 800x600 pixels.

Common Batch Processing Tasks

1. Format Conversion

Convert images to another format. This example converts all to JPEG.


for filename in os.listdir(input_folder):
    if filename.endswith(('.png', '.bmp')):
        img = Image.open(os.path.join(input_folder, filename))
        new_name = os.path.splitext(filename)[0] + ".jpg"
        img.save(os.path.join(output_folder, new_name), "JPEG")

2. Applying Filters

Add filters to multiple images. This applies a grayscale effect.


from PIL import ImageFilter

for filename in os.listdir(input_folder):
    img = Image.open(os.path.join(input_folder, filename))
    gray_img = img.convert("L")
    gray_img.save(os.path.join(output_folder, filename))

For more advanced filters, see our Python Image Analysis Guide.

3. Batch Cropping

Crop all images to a specific size. This crops to a square.


crop_size = (200, 200, 600, 600)  # left, top, right, bottom

for filename in os.listdir(input_folder):
    img = Image.open(os.path.join(input_folder, filename))
    cropped_img = img.crop(crop_size)
    cropped_img.save(os.path.join(output_folder, filename))

Learn more about cropping in our Python Image Cropping Guide.

Advanced Batch Processing

For complex tasks, combine multiple operations. This script resizes and converts to grayscale.


for filename in os.listdir(input_folder):
    img = Image.open(os.path.join(input_folder, filename))
    img = img.resize((800, 600))
    img = img.convert("L")
    img.save(os.path.join(output_folder, filename))

Error Handling

Always include error handling. Some images might be corrupt.


for filename in os.listdir(input_folder):
    try:
        img = Image.open(os.path.join(input_folder, filename))
        img.resize((800, 600)).save(os.path.join(output_folder, filename))
    except Exception as e:
        print(f"Error processing {filename}: {str(e)}")

Performance Tips

Processing many images can be slow. These tips will help:

1. Use os.scandir() instead of os.listdir() for large folders.

2. Process images in parallel using multiprocessing.

3. Reduce image quality for faster processing when possible.

For large projects, check our Python Image Rescaling Guide.

Conclusion

Python makes batch image processing simple. With PIL, you can automate repetitive tasks.

Start with basic operations. Then combine them for complex workflows. Always include error handling.

Batch processing saves hours of manual work. It's perfect for preparing image datasets or processing photos.