A worker finished a job at 18:40 and sent the photos from his phone. I opened that job on my laptop the next morning, clicked the thumbnail, and got a grey box with a broken-file icon. Not an error page. Just nothing.
The access log said 200. The row in the database had a size on it. The file was on disk at 394104 bytes, which is not a small number, and nothing in that row looked wrong. It took me an embarrassing amount of time to accept the boring explanation: the upload had worked exactly as written, and the file was in a format my own machine could not display.
HEIC. iPhone original, the path that skips Safari's transcode — share sheet, or any browser that hands the bytes straight through. My intake route checked the mimetype and moved on, and for months that was enough because everything else arrived as JPEG.
Here are the two lines I trusted:
if not file.mimetype.startswith("image/"): return 415 ext = {"image/jpeg": ".jpg", "image/png": ".png"}.get(file.mimetype, ".jpg") I would have defended both in review. image/heic starts with image/, so it sailed through the gate I was quietly proud of. And the extension map has a default, so the HEIC bytes landed on disk as photo.jpg. Every layer said yes. The only thing that said no was the operating system of the person who had to look at the picture.
That one is on me, and it is a silly mistake in hindsight: I built a whitelist and then gave it a default.
Then the second one, which took longer to see because it looked like good engineering. The resize helper refused to write a file bigger than its input:
if not must_convert and len(out) >= len(src): return src, ext Sensible rule for a 4 MB phone JPEG — re-encoding it into a slightly bigger JPEG is pure loss. Terrible rule for HEIC, because "keep the original" there means "keep the one Windows can't open". For a while I was convinced the conversion itself was broken, because the sizes on the files I cared about never moved. The conversion was fine. My guard was voting to keep the unreadable file, and the log line I kept staring at — kept original, 394104B — was telling me exactly that while I read it as "already optimized".
The fix is one extra flag, but I only found it after the third embarrassment. I wanted a round-trip test, so I decoded a HEIC and tried to write it back out:
KeyError: 'HEIF' Not a permissions thing, not a path thing. pillow-heif==1.1.1 bundles libde265, which decodes HEVC and has no encoder in it. 1.1.1 is the last release with a cp39 wheel, so on Python 3.9 I can read HEIC and I cannot write it, full stop. The library I had picked for "support HEIC" supported half of it, and I found out by guessing.
Worth saying out loud: I generated my test fixture with pillow-heif 1.9.0 on my laptop while production had 1.1.1 pinned. Two versions, two behaviours, one of which I had never actually run. If the fixture loads and the assertion passes, you do not go looking for the encoder.
What went in and what came out, measured on the production interpreter with the pinned dependencies:
| in | out |
|---|---|
| 394104 B HEIC, stored 2400×1800, EXIF orientation=6 | 161576 B JPEG, 1200×1600, upright |
route tests through Flask test_client | 11 passed in 5.11s |
| full local suite | 169 passed, 2 xfailed in 91.08s |
The orientation number matters more than it looks. Orientation=6 means the stored pixels are landscape and every viewer is supposed to rotate them on display. Decode and re-encode, and that tag is gone — if you don't apply the rotation yourself, the worker's upright photo comes back on its side, and he assumes the app is broken. The output above is 1200×1600 portrait, which is the only way I know the rotation actually landed.
The code that replaced the old guard:
try: import pillow_heif pillow_heif.register_heif_opener() except ImportError: pillow_heif = None # intake keeps working, just without HEIC def photo_ext_for(mime): return {"image/jpeg": ".jpg", "image/png": ".png", "image/heic": ".heic", "image/heif": ".heif", "image/avif": ".avif"}.get(mime, "") No default extension any more — an unknown image type gets no extension and is rejected downstream instead of being quietly labelled .jpg. And HEIC/HEIF/AVIF always convert, bigger output or not: must_convert=True. A 380 KB file that nothing on the shop floor can open is not smaller than a 400 KB file that everything can open. It is just broken in a way that costs less disk.
The endpoint lives in wiplink, the small job-tracking app I run for workshops, and the intake route is shared, so a photo attached to a job, a note, or a delivery all take the same path. Which is the other reason I care: one bad format decision at intake gets copied into every feature that touches a file.
My take, and I'll take the argument: keeping the original when the re-encode comes out bigger is a footgun, not an optimisation. Size is the wrong tiebreaker — readability over byte count — and the moment a format your users' desktops can't read walks through the gate, that guard starts preserving the exact file nobody can use. I'd argue the only judgment that matters is whether the person on the other end can open it.
I've only measured this on one phone model and one pipeline, so treat the numbers as narrow.
What does your intake do when the re-encode comes out bigger — keep the source, like mine did? Python 3.9.2, pillow 11.3.0, pillow-heif 1.1.1 on this box. If you think original fidelity is worth a broken preview on Windows, I want the case where it actually paid off — comments are open. The tool I run is in my profile.
