All case studies

Shrinking every photo automatically

For every photo on Planit Traveler, I used to shrink and compress it by hand in Squoosh before uploading. Forget once, and a full camera-sized photo went live. Now the server does it on every upload, and each screen gets a copy sized for it.

Project
Planit Traveler, blog photos and image delivery
My part
Set the size and quality targets, directed the work with Claude Code, checked the results, released it
Built with
Django, Pillow, WebP, React
Status
In production since 28 October 2026

The problem

Photos from a good camera are huge: thousands of pixels across and often over 10 MB. A blog post only needs a fraction of that, and a phone needs even less.

So my routine for every photo was: open it in Squoosh, shrink it, compress it, download it, then upload it. The server kept whatever it was given. It cleaned the file up, but it never made it smaller.

That worked as long as I never forgot. And it meant every phone downloaded the same large copy as a desktop screen.

Two changes, the same day

Every screen gets its own size. Each public photo now has several smaller WebP copies, and the page tells the browser which ones exist. A phone picks a small one, a wide screen a larger one.

New uploads are compressed automatically. A photo in a post is shrunk to fit within 1,600 by 2,400 pixels, and a post’s main photo within 2,400 by 2,400. Then it’s saved as WebP, aiming for about 200 KB and 450 KB respectively. My routine became: crop, upload, publish.

The lines that decide

Before: the photo was turned the right way up and saved again, at full size.

def _normalized_image_bytes(image, actual_format):
    image = ImageOps.exif_transpose(image)
    output = io.BytesIO()
    save_format = FORMAT_INFO[actual_format]['save_format']

    if actual_format == 'JPEG':
        image.save(output, format=save_format,
                   quality=92, optimize=True)
    ...
    return output.getvalue(), image.width, image.height
Saving an uploaded photo before, trimmed. Python.

After: shrink to fit, then try three quality levels and keep the first one under the target.

'blog_inline_image': ImageUploadPolicy(
    10 * 1024 * 1024, 8000, 8000, 36_000_000, (1600, 2400),
),
...
if not artwork:
    image.thumbnail(policy.photo_bounds, Image.Resampling.LANCZOS)
...
for quality in (85, 82, 78):
    output = io.BytesIO()
    image.save(output, 'WEBP', quality=quality, lossless=artwork)
    data = output.getvalue()
    if artwork or len(data) <= policy.photo_target_bytes:
        break
Saving an uploaded photo now, trimmed. Python.

Quality never drops below 78. A detailed photo, like a busy street, may stay above the target rather than come out blurry. The target is a goal, not a hard cap.

Decisions and tradeoffs

A quality floor, not a size cap
Forcing every photo under 200 KB would have spoiled the most detailed ones. A floor protects how they look.
Artwork stays exact
Logos, screenshots and anything with transparency are kept lossless. Compression is only for photographs.
Don’t redo what’s already done
A WebP that’s already the right size is cleaned and kept as it is, not compressed a second time. The same photo uploaded twice is stored once.
Originals aren’t kept
Only the optimized copy is stored. It saves space, and means a full-size original can’t be recovered from the site.
Old photos left alone
This changed new uploads only. Photos already on the site kept their files and links.

How it was tested

The size-per-screen change was measured on a local production build, using test data made from real photos on the site, before and after, across the same 14 page views.

As reported in the two pull requests, 28 October 2026
SuiteCoversResult
Photo transfer, 14 page viewsSame pages and data, before and after58.1 MB before, 11.2 MB after (81% less)
ScreenshotsSeven pages at five widths, light and dark70 of 70 identical to the previous build
Automatic compression, backendSizes, quality, artwork, reuse, uploads321 passed
Automatic compression, browserFrontend and browser checks, plus the production build11 of 11 passed

The 81% is from a local build, not measured on the live site. Photos already on the site only get their smaller copies after a separate one-off step, which wasn’t part of this change.

Outcome and limits

Both changes went live on 28 October. New photos are small before a reader ever sees them, and I no longer touch a photo before uploading it.

It also caused a bug. The safety check that guards memory measured each upload at full size, before this code could shrink it, so a week later my own large photos were turned away. That’s the photo-upload story.

Next: Sessions that end when they should