Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

How to Resize Hundreds of Images in Batch with Python

Resize every image in a folder to a target size using Pillow, creating a new subfolder for processed files.

Easy Python 3.9+ Jun 27, 2026 Automation & scripting 1 views 0 copies

Requires third-party packages — install first
pip install Pillow

Python code

21 lines
Python 3.9+
import os
from PIL import Image

def resize_images_in_batch(directory, output_size=(800, 600)):
    if not os.path.exists(directory):
        print(f"Directory {directory} does not exist.")
        return
    output_dir = os.path.join(directory, "resized")
    os.makedirs(output_dir, exist_ok=True)
    for filename in os.listdir(directory):
        if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
            filepath = os.path.join(directory, filename)
            with Image.open(filepath) as img:
                img_resized = img.resize(output_size, Image.Resampling.LANCZOS)
                output_path = os.path.join(output_dir, filename)
                img_resized.save(output_path)
                print(f"Resized and saved: {output_path}")

if __name__ == "__main__":
    target_dir = "images"  
    resize_images_in_batch(target_dir, (800, 600))

Output

stdout
Resized and saved: images/resized/photo1.jpg
Resized and saved: images/resized/photo2.png
Resized and saved: images/resized/screenshot.bmp

How it works

This script loops through all files in a given directory, filtering by common image extensions. For each image it opens the file with Pillow's Image.open, resizes it using the high-quality LANCZOS resampling filter, and saves the result into a resized subfolder. Using os.makedirs(..., exist_ok=True) avoids errors if the subfolder already exists. The script is designed as a reusable function that takes a directory path and target output size as parameters.

Common mistakes

  • Forgetting to install Pillow (`pip install Pillow`) before running the script.
  • Using `os.listdir()` without filtering for image file extensions, which may try to resize directories or non-image files.
  • Overwriting original images instead of saving resized copies to a separate folder.

Variations

  1. Use `pathlib.Path.rglob('*')` to recursively process images in subdirectories.
  2. Resize proportionally based on max width or height instead of forcing exact dimensions.

Real-world use cases

  • Preprocessing product photos for an e-commerce site to ensure uniform thumbnail sizes.
  • Downscaling high-resolution camera images before uploading them to a cloud storage bucket.
  • Generating compressed image assets for a mobile app to reduce bundle size.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.