SolutionsCraft
← Automation

Automation · 1 min read

Auto-Resize and Compress Images with a Python Watch-Folder Script

Resizing images by hand before every upload gets old fast. This script watches a folder and does it the moment a file lands — drop a photo in, it comes out resized and compressed, ready to use.

Before you start

You'll need Python 3.9+ and two packages:

pip install pillow watchdog

Pillow handles the actual image processing; watchdog is what lets the script react to new files instead of polling the folder on a timer.

Step 1: Write the handler

# resize_watch.py
from PIL import Image
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
 
MAX_WIDTH = 1600
QUALITY = 82
 
class ImageHandler(FileSystemEventHandler):
    def on_created(self, event):
        if not event.src_path.lower().endswith((".jpg", ".jpeg", ".png")):
            return
        img = Image.open(event.src_path)
        if img.width > MAX_WIDTH:
            ratio = MAX_WIDTH / img.width
            img = img.resize((MAX_WIDTH, int(img.height * ratio)))
        img.save(event.src_path, optimize=True, quality=QUALITY)
 
observer = Observer()
observer.schedule(ImageHandler(), path="./incoming", recursive=False)
observer.start()

Step 2: Run it

python resize_watch.py

Drop a large JPEG into ./incoming and watch it shrink in place within a second or two.

Tip: on_created fires as soon as the file starts being written, which can catch a still-copying file on a slow network drive. If you're watching a folder fed by uploads over a slow connection, add a short delay or check the file size is stable before processing.

What's next

For a one-off batch instead of a always-running watcher, drop the watchdog part entirely and just loop over os.listdir() — that version fits neatly into an n8n Execute Command node, so you can trigger it from the same kind of workflow covered in the Stripe-to-Sheets tutorial.

Get new tutorials by email

No spam, unsubscribe anytime.