How to Compress an Image to an Exact File Size
Why most compressors overshoot or undershoot your target, and the binary search method that lands close to the limit while keeping the most quality.
"Make this image smaller" and "make this image fit under 20 KB" are different problems. Most tools solve the first one and leave you to fight with the second.
What most tools actually do
A typical compressor gives you a quality slider, or picks a fixed quality like 80%, and encodes once. The trouble is that JPEG quality is not a file size — the same quality setting produces wildly different results depending on the image. A flat product shot on white might come out at 40 KB; a detailed landscape at the identical setting might be 400 KB.
So you end up doing the search by hand: try 80, too big, try 50, too small, try 65… Each pass re-compresses an already-compressed file, and quality degrades with every attempt.
The binary search approach
The reliable method is to search for the quality level automatically, always encoding from the original rather than from the previous attempt.
- Set a range: quality from 0.08 (lowest) to 0.96 (highest).
- Encode at the midpoint. Measure the actual byte count.
- If it fits under the target, remember it and raise the floor. If it is over, lower the ceiling.
- Repeat about eight times.
Eight iterations narrows the range by a factor of 256, which is more than enough precision. You end up with the highest quality that still fits — not merely a quality that fits.
The difference in practice is real. Asked for 20 KB, a fixed-quality tool commonly returns 8 KB — technically a pass, but you threw away more than half the quality budget you were entitled to. The search lands near 19 KB.
When quality alone is not enough
Sometimes even the lowest quality setting is still over the limit. A 4000 × 3000 pixel phone photo cannot reach 20 KB at any quality, because there are simply too many pixels to describe.
At that point the only remaining lever is dimensions. Reducing to 800 × 600 first, then searching quality again, produces a dramatically better-looking 20 KB file than crushing the full-resolution image — fewer pixels, each described more accurately.
Our tools do this automatically: if the quality search fails, they step the dimensions down and search again, up to four times.
The exception: exact dimensions
When a form demands exact pixel dimensions — IBPS wants 200 × 230, US visas want 600 × 600 — shrinking is not allowed. The tool must hold the dimensions and report honestly if the target cannot be reached, rather than silently returning a file that will be rejected for the wrong size.
Formats matter more than people expect
- JPEG — photographs. Far more efficient than PNG on anything with gradients or texture. Has no transparency.
- PNG — flat graphics, screenshots, logos, anything needing transparency. Lossless, so it ignores quality settings entirely; the only way to shrink a PNG is to reduce its dimensions or its colour count.
- WebP — roughly 25–35% smaller than JPEG at equivalent quality, and widely supported. Excellent for the web, but many upload forms still reject it.
A common trap: saving a photograph as PNG. A PNG photo can be five times the size of the JPEG equivalent. If a photo will not get under a limit, check the format before blaming the compressor.
One thing to avoid entirely
Do not compress a file that has already been compressed. JPEG is lossy — each save permanently discards information, and the damage accumulates. Three rounds of squeezing produces visible blocking and colour smearing long before a single well-targeted pass would.
Always go back to the original.
Try it
Every tool on this site uses the method described above, running entirely in your browser: