How "Compress to Exactly 100KB" Actually Works (Real Numbers)
"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"…
"Compress to Exactly 100KB" may seem easy, but building it proves challenging. I recently built an in-browser tool for GetResizer, and the surprising part was how few encodes it takes to hit a very small file size. Here are some real measurements to illustrate the process:
Every "compress image" UI with a quality slider fails at hitting exact KB targets. Dragging the slider from quality 70 to 50 yields file sizes of 131KB and 113KB, respectively. The relationship between JPEG quality and file size is monotonic but highly nonlinear and image-dependent. Instead of guessing, we need a systematic approach.
The algorithm involves binary searching over quality settings, which is effective because file size increases monotonically with quality. Here's the Python code for the algorithm:
```python
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)
if len(data) <= target_bytes:
best = (mid, data)
lo = mid + 1
else:
hi = mid - 1
return best
```
A real 2048×2048 photo (1.48 MB source) completed the search in 52–68 ms. The quality range is 1–95, requiring 6–7 encodes to find the ideal target.
When aiming for a 50 KB target, quality 8 at full resolution hits the target but produces a poor-quality image. However, if we downscale to 1200px and set quality to 28, we fit the 50 KB budget while maintaining better visual quality. A 1000px photo at q45 quality looks better than a 2048px photo at q8, even though both use the same number of bytes.
Production tools must consider two axes: quality first at full size, and if the best quality is below a perceptual floor (around q40–50), downscale and search again. This approach takes less than a second for 12 MP inputs.
In production, metadata like EXIF and orientation can add several KB, and some portals reject rotated photos. Strip or normalize metadata, but keep orientation applied to pixels. Aim slightly under the target, such as 95% of the cap, to avoid rejection.
PNG and WebP do not lend themselves to exact-KB search as JPEG does since they control dimensions, palette, and other factors. Progressive JPEGs with optimize=True offer a small quality boost before sacrificing image quality. The entire search is performed locally on the CPU, so an in-browser tool can handle it without uploading data.
If you don't want to build your own compressor, consider GetResizer, iLoveIMG, Adobe Express, TinyPNG, or desktop tools like IrfanView, which show the resulting size as you adjust parameters.
Rewritten by urgent.news; may contain errors — check the original. How we use AI.
Source: dev.to