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
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
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.
| Suite | Covers | Result |
|---|---|---|
| Photo transfer, 14 page views | Same pages and data, before and after | 58.1 MB before, 11.2 MB after (81% less) |
| Screenshots | Seven pages at five widths, light and dark | 70 of 70 identical to the previous build |
| Automatic compression, backend | Sizes, quality, artwork, reuse, uploads | 321 passed |
| Automatic compression, browser | Frontend and browser checks, plus the production build | 11 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.