Automation Lab 2024-02-01

Bulk Image Resizer: Automate Your Web Assets

Estimated Time
10 MIN
Environment
Python 3.x, Pillow

If you're a content creator or web developer, you know the pain of resizing images one by one. Today, we'll build a tool that scans a folder and resizes everything to a standard width while keeping the aspect ratio.

1. The Tool We'll Use: Pillow

Pillow is the "friendly fork" of PIL (Python Imaging Library). It's robust and easy to use.

pip install Pillow

2. The Automation Script

This script looks for .jpg and .png files in an 'input' folder and saves the smaller versions in an 'output' folder.

from PIL import Image
import os

def resize_images(input_dir, output_dir, width):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    for filename in os.listdir(input_dir):
        if filename.endswith(('.jpg', '.png', '.jpeg')):
            img = Image.open(os.path.join(input_dir, filename))
            w_percent = (width / float(img.size[0]))
            h_size = int((float(img.size[1]) * float(w_percent)))

            img = img.resize((width, h_size), Image.Resampling.LANCZOS)
            img.save(os.path.join(output_dir, filename))
            print(f"Processed: {filename}")

resize_images('input', 'output', 800)

3. Why This is Useful

By automating this, you ensure that every image on your site has the same maximum width, which improves page load speed and SEO consistency. You can even extend this to add watermarks!

Download Full Automation Pack

Get all source codes, advanced configurations, and exclusive troubleshooting guides.

FREE DOWNLOAD

Secure Verification Required