Skip to content
D-CSIL™

AI Guide · 2026-11-26 · 1:04 PM CT

Shrink your photos for the web with Pillow, in six steps

Paul Derrington, Jr.

By Paul Derrington, Jr.

CEO | Chief Engineer | Research Engineer | Editor, D-CSIL

5 min read

Follow @PaulDerrington on X

Key Takeaways

A camera photo is usually far bigger than any web page needs. This guide uses Pillow, the standard Python image library, to fix orientation, shrink a whole folder of images without stretching them, and save JPEG and WebP copies at a sensible quality — originals untouched. Every step was checked against Pillow's official documentation and run on a test folder before publishing.

Steps 1-2 — Install Pillow and look before you resize

Step 1: install Pillow with pip, exactly as the official installation page gives it: python3 -m pip install --upgrade pip, then python3 -m pip install --upgrade Pillow. Confirm the install and see which formats your build supports by running python3 -m PIL — the Image reference recommends that command for printing the available formats.

Step 2: open one image and look at it before changing anything: from PIL import Image, then with Image.open("photo.jpg") as im: print(im.format, im.size, im.mode). The tutorial is explicit about what those three attributes mean: format is the source file type, size is a (width, height) tuple in pixels, and mode is the pixel type — "RGB" for true color, "RGBA" when there is an alpha channel. One warning from the same tutorial matters for batch work later: Image.open is lazy. It reads the header and identifies the file, but does not decode the pixel data until you actually process it, which is why you do the real work inside the with block.

Steps 3-4 — Fix the orientation, then shrink without stretching

Step 3: run ImageOps.exif_transpose(im) from PIL.ImageOps before you resize. Phone photos often carry an EXIF Orientation tag instead of stored-upright pixels; the ImageOps reference says exif_transpose transposes the image according to that tag and removes the orientation data, so the saved copy cannot be flipped sideways again by a viewer that reads the tag differently.

Step 4: shrink with im.thumbnail((1200, 1200)). The handbook tutorial shows thumbnail() modifying the image in place to fit inside the size you pass, keeping the original aspect ratio — it caps the long edge, it does not stretch. In the test run for this guide, a 2400 x 1200 image came out at 1200 x 600 and a 1800 x 1800 image at 1200 x 1200. If you would rather not modify the original object, the ImageOps reference offers contain(), which returns a resized copy set to the maximum width and height within the requested size while maintaining aspect ratio. Pick a cap that matches where the image will actually display; 1200 pixels is a sane default for article and product pages.

Step 5 — Save JPEG and WebP at a quality you picked on purpose

Save two formats. WebP first, because it accepts the image as-is: im.save("photo.webp", "WEBP", quality=82, method=6). The file-format documentation puts WebP quality on a 0-100 scale with a default of 80, and method is the quality/speed trade-off from 0 (fast) to 6 (slower, better), defaulting to 4 — so method=6 costs a little time on a batch and buys smaller files.

JPEG needs one extra move: convert first with im.convert("RGB") if the mode is not already RGB, then save with quality=82, optimize=True, progressive=True. JPEG has no alpha channel, and saving an RGBA image as JPEG fails outright — verified in the test run, Pillow raises OSError: cannot write mode RGBA as JPEG. The format documentation is specific about JPEG quality: the scale runs 0 (worst) to 95 (best), the default is 75, and values above 95 should be avoided because 100 disables parts of the compression and produces large files with hardly any gain in quality. The optimize flag makes the encoder take an extra pass to select optimal settings, and progressive stores the file as a progressive JPEG. Note that save() picks the format from the filename extension unless you pass it explicitly, as the examples here do.

Step 6 — Run it over a whole folder, originals untouched

Put the pieces together and point them at a folder. Keep the originals in a folder named originals and write every output to a separate folder named web — never overwrite the only copy of a photo. The full script: from pathlib import Path; from PIL import Image, ImageOps; SRC = Path("originals"); OUT = Path("web"); OUT.mkdir(exist_ok=True); then for each path in sorted(SRC.iterdir()): open it with Image.open, apply ImageOps.exif_transpose, call thumbnail((1200, 1200)), save the WebP copy, convert to RGB and save the JPEG copy, and print the file name and final size so you can see what happened.

That exact script was run before this guide was published, on Pillow 10.2.0, against a test folder holding a 2400 x 1200 RGBA PNG and an 1800 x 1800 JPEG. Both files produced a .webp and a .jpg in the web folder at the capped sizes, and the originals were unchanged. If a file in your folder is not an image, Image.open raises an error on it — wrap the loop body in a try/except OSError and print the file name, the same pattern the handbook tutorial uses in its own convert and thumbnail examples, so one bad file does not kill the batch.

What this does not do

Pillow resizes and re-encodes; it does not crop intelligently, pick the interesting part of a photo, or generate the responsive srcset markup your site still needs. It also strips nothing on its own — the EXIF data, including location tags from phone photos, is not carried into a fresh save unless you pass it back in, which is usually what you want for public web images, but check a saved file if keeping camera metadata matters to you. And quality=82 is a starting point, not a law: save one detailed photo at 70, 82 and 92, view all three at full size, and keep the lowest setting where you cannot see a difference.

Disclaimer: Derrington Collaborative AI is my personal business, operated independently of and outside my official U.S. Government position and duties. The views and opinions expressed on this site and on any and all of my social media accounts are my own. They do not reflect the official policy or position of the Department of the Army, the Department of Defense, or the U.S. Government. Mention of any company, product, or service does not constitute an official endorsement.