The scan the portal kept rejecting
Last week I had to upload a scanned rental agreement to a city housing portal. Hard limit: 1 MB per file. My scan came off the office copier as a 2100 × 2970 px PNG — 3,412,886 bytes. The portal answered with a red banner: File exceeds maximum size of 1.0 MB. No resize tool on their end, no hints, just rejection.
Walking back to the copier for a lower-dpi rescan felt wasteful, and I didn't want a photo editor trial on a work machine either. Two terminal tools and one browser tab later, the same scan sat at 681 KB and the portal took it. Here's what worked, in the order I tried it.
Method 1: Python and Pillow
Environment: Python 3.11.8, Pillow 12.1.0.
The reasoning first. An A4 page stays legible at 200 dpi, which is 1654 × 2339 px. The scan is black text on white paper, so dropping color costs nothing visible and removes two thirds of the JPEG chroma work. Quality 80 with optimize and progressive handles the rest.
from PIL import Image
src = Image.open("agreement_scan.png")
print(src.mode, src.size) # RGB (2100, 2970)
img = src.convert("L") # grayscale
img.thumbnail((1654, 2339), Image.LANCZOS) # fit inside A4 @ 200 dpi
img.save("agreement_small.jpg", "JPEG",
quality=80, optimize=True, progressive=True)
thumbnail() keeps the aspect ratio and never upscales, which is exactly what you want for a fixed scan size. My output landed at 1654 × 2321 px and 681,204 bytes. Zoomed to 100% the text is still sharp; the only visible difference is slightly smoother paper texture. The Pillow JPEG docs explain the quality scale if you need to squeeze further — below about 75, text starts showing mosquito-noise around glyph edges, so I stopped there.
One tip from doing this a few times: check the result at the size people will actually read it. My first draft used quality 70 and came out at 512 KB, but the signature line got fuzzy at 100% zoom, which is the one part of a contract you cannot have fuzzy. Quality 80 cost 169 KB more and fixed it. The difference between "opens fine" and "acceptable for legal use" is usually a couple of quality points, not a different tool.
Method 2: ImageMagick, one line
On machines where I don't want to install Python packages, ImageMagick does the same job in a single command. Environment: ImageMagick 7.1.1-43 on Ubuntu 22.04.
magick agreement_scan.png \
-colorspace Gray \
-resize 1654x2339 \
-quality 82 \
agreement_small.jpg
identify agreement_small.jpg
# agreement_small.jpg JPEG 1654x2321 1654x2321 +0+0 8-bit Grayscale 681KB
Same trade-offs, nearly the same output size (681 KB here too). -colorspace Gray replaces Pillow's convert("L"), and -resize with a single dimension fits within that box while preserving aspect ratio. If you batch dozens of scans, wrap it in a shell loop; the ImageMagick usage guide covers the resize operators in detail. For a one-off document, honestly, the loop is overkill.
Method 3: the browser, no install at all
When I'm on a locked-down laptop with neither Python nor ImageMagick, I drag the file into a free online image compressor, set the quality slider to about 80, and download the result. The whole pipeline runs client-side in JavaScript: the browser reads the file, decodes it, re-encodes the preview as you move the slider, and shows the projected size before you commit. Nothing is uploaded, which matters when the document is a rental contract and the machine sits on an office network. WebP output is on offer too, and it often beats JPEG at the same quality for text-heavy pages, though some stubborn government portals still refuse anything that isn't JPG or PDF, so I check the accepted formats before bothering.
I still verify the final size with ls -l before uploading, out of habit from the mistake below.
Three pitfalls worth knowing
1. "Just lower the quality" does nothing for PNG. My first attempt was magick scan.png -quality 40 scan_small.png. Result: 3,412,886 → 3,398,771 bytes, a 0.4% saving. PNG is lossless; its -quality value only tunes compression strategy and filtering, not what's stored. The file was big because of pixel count and color depth, so the fix was resize plus grayscale plus a lossy format — not a quality knob.
2. Transparency turns black in JPEG. My second upload was a screenshot of the signed form with a transparent background, and a straight JPEG conversion gave the stamp area a black backdrop. JPEG has no alpha channel, so converters fill transparency — often with black. ImageMagick needs -background white -alpha remove -alpha off before the format change; in Pillow you'd paste onto a white RGB canvas first. Check src.mode for an A before you convert anything.
3. "977 KB" can still be over a 1 MB limit. My first compressed attempt showed 977 KB in the file manager, yet the portal rejected it again. The portal counts decimal bytes (1,000,000), while the file manager shows binary kibibytes (1,048,576 bytes per "MB"). The file was actually 1,000,423 bytes — 423 bytes over. Now I always check with stat -c %s and aim a few percent under the stated limit.
Summary
| Approach | Command / step | Result | Best for |
|---|---|---|---|
| Pillow script |
convert("L") + thumbnail() + quality=80
|
3.4 MB → 681 KB | Repeatable, part of a batch pipeline |
| ImageMagick CLI | magick -colorspace Gray -resize 1654x2339 -quality 82 |
3.4 MB → 681 KB | Quick one-liners, shell loops |
| Browser tool | drag, slider, download | same ballpark, instant | Locked-down machines, one-off uploads |
Resize to something the content supports, drop the color you don't need, switch to a lossy format — and check the size in raw bytes before you hit upload. If you'd rather skip the terminal entirely, this browser-based image compressor gets you the same place in under a minute.
More free browser-based utilities at utilvy.com.
Top comments (1)
This is the rare compression post that explains why the obvious fix fails. The PNG pitfall is the one I see people hit most — dropping quality on a lossless format and wondering why a 3.4 MB file barely moves. The real lever was never quality, it was the format and the color data: grayscale plus JPEG is what actually removed the bytes.
The 977 KB story is my favorite detail, because it catches so many people. File managers report MiB while portals often enforce a hard 1,000,000-byte limit, so a file that looks safely under can still be rejected. Aiming a few percent under the stated limit is a rule I now follow everywhere, not just for uploads.
The transparency pitfall is worth flagging too — pasting onto a white canvas before converting is one of those steps you only forget once. Do you have a rule of thumb for when you stop at quality 80 versus pushing lower, or do you judge it purely by zooming in on the text at reading size?