Batch Process Product Photos in Python with Pillow

A photographer delivers sixty raw product shots for a new collection, each one 4000 by 6000 pixels and four to six megabytes. The website needs 1200 pixel white-background squares, ideally under 200 KB each. Done by hand, that means opening Photoshop, cropping, resizing and exporting one file at a time. Sixty photos takes about two and a half hours, and the whole routine starts again with the next collection.

Work that is rule-based, repetitive and needs no judgement is exactly what Python is good at. With one library, Pillow, you write the script once. After that, every new collection is a matter of dropping photos into a folder and double-clicking a file. Two or three minutes, done.

Why compression matters: numbers beat feelings

A four megabyte original from a phone or camera is completely normal. Sixty of them is 240 MB. A customer opening your category page on mobile data will wait half a minute for the images to fill in, and most will simply leave.

The rule of thumb most teams work to: every extra second of load time costs a few percentage points of conversion. A product image taken from four megabytes down to 150 KB looks virtually identical on a phone screen, but loads more than ten times faster. Compression is not about saving disk space, it is about closing the sale.

Step one: install Pillow

pip install Pillow

Pillow is the standard image library for Python. It is free, works the same on Windows and Mac, and needs no Photoshop licence or any other paid software.

Step two: batch resize, keep the ratio and pad with white

The most common mistake is resizing straight to 1200 by 1200, which squashes the photo and distorts the product. The correct approach is to scale the longest side down to 1200 first, then centre the result on a white canvas, so the leftover space is filled with white on the sides or top and bottom.

from PIL import Image, ImageOps import os SRC = "D:/product_raw" DST = "D:/product_web" SIZE = 1200 os.makedirs(DST, exist_ok=True) for name in os.listdir(SRC): if not name.lower().endswith((".jpg", ".jpeg", ".png")): continue src_path = os.path.join(SRC, name) img = Image.open(src_path) img = ImageOps.exif_transpose(img).convert("RGB") img.thumbnail((SIZE, SIZE), Image.LANCZOS) canvas = Image.new("RGB", (SIZE, SIZE), (255, 255, 255)) x = (SIZE - img.width) // 2 y = (SIZE - img.height) // 2 canvas.paste(img, (x, y)) out = os.path.join(DST, os.path.splitext(name)[0] + ".jpg") canvas.save(out, "JPEG", quality=88, optimize=True, progressive=True) print(name, "->", os.path.basename(out), os.path.getsize(out) // 1024, "KB")

Three parameters are worth understanding. thumbnail preserves the aspect ratio and only ever shrinks, so a small image is never blown up into a blurry mess. LANCZOS is the sharpest resampling filter for downscaling. progressive=True stores the JPEG so it can render in passes, which means the customer sees something useful before the file has fully arrived.

Step three: EXIF rotation, which every phone photo will hit

iPhones and most Android phones store the pixels sideways and record a rotate ninety degrees flag in EXIF. Browsers follow that flag automatically, Pillow does not, so portrait shots come out lying on their side.

One line fixes it: ImageOps.exif_transpose(img). It has to run before convert. If you convert to RGB first and transpose afterwards, some older Pillow versions have already dropped the EXIF block and the fix no longer applies.

Step four: add a translucent watermark

The simplest protection against image theft is a semi-transparent shop name. Load a PNG with a transparent background, reduce its opacity, and paste it into a corner.

wm = Image.open("logo.png").convert("RGBA") wm.thumbnail((240, 240), Image.LANCZOS) alpha = wm.split()[3].point(lambda p: int(p * 0.45)) wm.putalpha(alpha) pos = (canvas.width - wm.width - 40, canvas.height - wm.height - 40) canvas.paste(wm, pos, wm)

The third argument to paste must be the watermark itself, otherwise the transparent areas turn into a black rectangle. It is the single most common surprise on a first attempt. Around forty-five percent opacity is a reasonable balance: enough to deter reuse, light enough that the product still reads clearly.

Step five: export WebP and halve the file again

At the same visual quality, a WebP file is roughly sixty percent the size of a JPEG. Current browsers support it well, and keeping the JPEG as a fallback covers the rest.

canvas.save(out.replace(".jpg", ".webp"), "WEBP", quality=82, method=6)

method controls compression effort. Higher numbers produce smaller files but take longer. In a batch run, a few extra seconds for another ten percent off every file is a good trade.

Three traps almost everyone hits

First, transparent PNG turning black. A PNG carries an alpha channel, and converting straight to JPEG fills transparent areas with black. Always go through the white canvas, never save the converted image directly as JPEG.

Second, overwriting the originals. The output folder must be separate from the source folder. People have pointed both at the same path, turned sixty full-resolution originals into 1200 pixel files, and had to ask the photographer to shoot again.

Third, filenames with spaces and Chinese characters. Some servers and marketplace platforms fail on upload, or quietly skip a few files. Normalise everything to lowercase English with hyphens, with one replace call in the script.

Summary

Python Tutorial: batch processing product photos needs no advanced technique, just Pillow and a for loop. The whole pipeline is five steps: install Pillow, scale proportionally and pad onto a white canvas, correct EXIF rotation, stamp a translucent watermark, export WebP. Once it is written, every new collection is one double-click, sixty photos in two or three minutes, each one down from four megabytes to around 120 KB. The same script works for staff portraits, property listings and menu photos, with nothing changed but the folder path and the target size.