"Compress this photo to exactly 100KB" sounds like a solved problem until you try to build it. I recently implemented exact-target compression (in-browser, for GetResizer — that's my site) and the interesting part is how few encodes it takes and what you have to sacrifice to hit small targets. All numbers below are real measurements, not estimates.
Why sliders don't cut it
Every "compress image" UI with a quality slider fails the exact-KB task: you drag to 70, get 131KB, drag to 50, get 113KB, drag again… The relationship between JPEG quality and file size is monotonic but wildly nonlinear and image-dependent. So instead of guessing, search.
The algorithm: binary search over quality
File size is monotonically increasing in quality. That's all you need for a binary search:
def compress_to_target(img, target_bytes): lo, hi = 1, 95 best = None while lo <= hi: mid = (lo + hi) // 2 data = encode_jpeg(img, quality=mid) # in the browser: canvas.toBlob if len(data) <= target_bytes: best = (mid, data) # fits — try to spend the budget on quality lo = mid + 1 else: hi = mid - 1 return best Quality range 1–95 is 7 halvings, so the search costs 6–7 encodes total. On a real 2048×2048 photo (1.48 MB source, Pillow 13, one CPU core) the whole search finished in 52–68 ms:
| Target | Result at native resolution | Time |
|---|---|---|
| 200 KB | quality 71 → 200 KB | 68 ms |
| 100 KB | quality 24 → 99 KB | 52 ms |
| 50 KB | quality 8 → 50 KB | 55 ms |
For reference, the quality→size curve on that same photo at native resolution: q30 → 113 KB, q40 → 131 KB, q50 → 149 KB, q60 → 167 KB, q70 → 196 KB, q80 → 241 KB. Notice how flat it is: 50 quality points only move the file by ~2.5×. That's why sliders feel terrible for exact targets.
The real decision: dimensions vs quality
Look at the 50 KB row: quality 8 at full resolution technically hits the target, and it will look like garbage. When the budget is tight, the better move is to downscale and spend the saved bytes on quality. Same photo, same 50 KB budget, best quality that fits at each size:
| Dimensions | Best quality that fits 50 KB |
|---|---|
| 2048px | q8 |
| 1600px | q14 |
| 1400px | q19 |
| 1200px | q28 |
| 1000px | q45 |
A 1000px photo at q45 is visibly better than a 2048px photo at q8 — same bytes, better pixels where they matter. A good exact-KB tool therefore searches two axes: try quality first at full size, and if the best fitting quality drops below a perceptual floor (~q40–50), downscale (×0.7 is a reasonable step) and search again. That's exactly the loop: outer loop on scale, inner binary search on quality, and it still costs under a second even for 12 MP inputs.
Details that bite in production
- EXIF and orientation. Metadata can add several KB and portals reject rotated photos even when the KB is right. Strip or normalize it, but keep orientation applied to pixels.
- Aim slightly under. Some portals count the upload envelope inconsistently. Target ~95% of the stated cap (95 KB for a 100 KB limit) and you never get bounced.
- PNG won't play along. Exact-KB search works because JPEG quality is a continuous knob. For PNG/WebP you mostly control dimensions and palette, so the search space is coarser — convert to JPEG for tight budgets.
- Progressive JPEG + optimize=True buys a few percent for free before you sacrifice any quality.
- Privacy: the whole search is local CPU work — a browser tool can do it without uploading anything. If a "compress to 100KB" site uploads your passport photo to a server to do a binary search, that's a design smell, not a requirement.
Alternatives
If you don't want to build it: GetResizer (mine, runs fully in-browser with an explicit KB target), iLoveIMG and Adobe Express (quality-based rather than target-based), TinyPNG (excellent compression, no exact-KB guarantee), and IrfanView on desktop, which shows the resulting size live while you drag.
Bottom line: exact-KB compression is a 7-iteration binary search plus one honest decision about dimensions vs quality. Measure once, ship the search.

