ImagePDF.Tools
Productivity

How to Compress an Image to an Exact File Size, And Why It's Harder Than You Think

N
NikolaLast updated on June 20, 2026 · 9 min read

Summary

Need a photo under 200 KB or 1 MB? Learn the binary search method for hitting exact file size targets every time. Free browser tool included.

There is a specific kind of frustration reserved for tasks that should be simple but aren't: compress this photo to under 200 KB. You compress, overshoot by five kilobytes. Lower the quality, undershoot by twenty. Raise it slightly, overshoot again. You repeat this loop until either the file lands in range or you give up and submit something too large and get an error from the government portal.

The problem is that JPEG and WebP compression ratios are non-linear and content-dependent. A photo of a clear blue sky at quality 80 might be 40 KB. The same quality setting on dense forest foliage might produce 400 KB. The encoder is measuring image complexity, the amount of high-frequency detail, not respecting a size target. This is by design: the quality slider controls visual fidelity, not bytes. Understanding that distinction is the first step to hitting size targets reliably.

Before and after image compression comparison showing file size reductions across JPEG, PNG and WebP | ImagePDF.Tools
Compression results vary significantly by image content, a uniform sky compresses far more than complex foliage at the same quality setting.

Why Quality Settings Don't Map to File Size

JPEG compression works by dividing the image into 8×8 pixel blocks and applying a frequency transform. The quality setting controls how aggressively the high-frequency components (fine detail, sharp edges) are discarded. An image with lots of high-frequency content, a photo of animal fur, a busy pattern, a dense cityscape, retains more data at any given quality setting than an image dominated by smooth gradients.

This means the same quality setting produces wildly different file sizes depending on the image. Quality 75 for a portrait with a blurred background might be 120 KB. Quality 75 for a photo of a textured brick wall might be 600 KB. There is no formula that reliably predicts output size from quality alone without actually running the compression.

The Correct Approach: Binary Search Over Quality

The reliable method for hitting a size target is a binary search over quality values. You set a target, measure the output at quality 80, then step up or down based on whether you over- or undershot, repeating until you're within tolerance. Binary search converges in at most 7 iterations (log₂ 100 ≈ 6.6), far faster than manual trial-and-error.

typescript
async function compressToTargetSize(
  file: File,
  targetBytes: number,
  toleranceBytes = 5000,
  format: 'image/jpeg' | 'image/webp' = 'image/jpeg',
): Promise<Blob> {
  let lo = 1, hi = 100, bestBlob: Blob | null = null;
  const canvas = document.createElement('canvas');
  const bitmap = await createImageBitmap(file);
  canvas.width = bitmap.width;
  canvas.height = bitmap.height;
  canvas.getContext('2d')!.drawImage(bitmap, 0, 0);

  while (lo <= hi) {
    const quality = Math.round((lo + hi) / 2);
    const blob = await new Promise<Blob>((res) =>
      canvas.toBlob((b) => res(b!), format, quality / 100),
    );

    if (blob.size <= targetBytes) {
      bestBlob = blob;
      lo = quality + 1; // within budget, try higher quality
    } else {
      hi = quality - 1; // over budget, reduce quality
    }

    if (Math.abs(blob.size - targetBytes) <= toleranceBytes) break;
  }

  // fallback: return the highest quality that fit
  return bestBlob ?? new Promise<Blob>((res) =>
    canvas.toBlob((b) => res(b!), format, lo / 100),
  );
}
💡

Binary search converges in at most 7 iterations regardless of the quality range. Manual increments of 5 at a time can take 15–20 attempts and often jump over the optimal value. Binary search finds it in under 10.

Dimension Reduction: The More Powerful Lever

Quality binary search has a floor: if quality 1 still produces a file above your target, the image is too large in pixels and no amount of compression can fix it without severe degradation. Pixel dimensions are the more powerful lever, halving width and height reduces file size by roughly 75% at the same quality.

  • Calculate approximate scale factor: scale = sqrt(targetBytes / currentBytes)
  • Apply scale to dimensions: newWidth = floor(currentWidth × scale)
  • Then run the quality binary search on the downscaled image
  • Use image resize to reduce dimensions before compressing when your target is very small

The two-lever approach, resize first, then compress, handles almost any size target. Use the dimension estimator as a starting point, then run binary search on quality to fine-tune within ±5 KB of your target.

Format Matters More Than Quality for Size Targets

Switching from JPEG to WebP at the same quality produces a 25–35% smaller file with no visual difference. If you're struggling to hit a size target with JPEG, try WebP first before reducing quality further. This is especially useful for passport and visa photos where visual quality matters but file size is tightly constrained.

ApproachQualityFormatOutput sizeVisual quality
Reduce quality only55JPEG198 KBVisible blocking in background
Switch to WebP, then compress72WebP197 KBClean, nearly identical to original
Resize to 1200px wide first80JPEG180 KBSharp, no artifacts
Resize + WebP80WebP130 KBBest quality at this file size
Two strategies for hitting a 200 KB target, same 2MP portrait photograph

Common Exact-Size Requirements by Platform

Use caseSize limitMin sizeRecommended approach
UK passport photo10 MB50 KBJPEG Q85 at 600×600px, well within both limits
US visa / DS-160240 KB1 KBJPEG or WebP Q70–80 at 600×600px
India passport / OCI1 MB10 KBJPEG Q85 at 1200×1200px
India Aadhaar docs200 KB-JPEG Q70 at 1200×1200px; try WebP if still over
WordPress default limit8 MB-JPEG Q80 at 2000px wide, well within limit
Webflow asset limit25 MB-WebP Q80, virtually any image fits
Shopify product image20 MB-JPEG Q85 at 2048×2048px is the sweet spot
File size requirements for common government and platform uploads

The Manual Approach: Using a Quality Slider Effectively

If you're not running code but need to hit a size target manually, a live size estimate on the quality slider is the fastest path. The correct workflow:

  1. 1.Open the image compressor and upload your file
  2. 2.Start at quality 80 and check the estimated output size shown below the slider
  3. 3.If over target, drag left (lower quality); if under, drag right (higher quality)
  4. 4.Use WebP output if you need more room, same quality setting, ~30% smaller file
  5. 5.Save when the estimated size is at or just under your target
ℹ️

The live size estimate is calculated by compressing a thumbnail of your image and extrapolating to full resolution. It's accurate to within 5–10% for most images. For exact verification, compress and check the output file size before submitting to a government portal.

Troubleshooting: Still Over the Limit After Compression

  • Still over at quality 1 JPEG: The image is too large in pixels. Use resize to reduce dimensions first.
  • Live estimate says under but saved file is over: Some metadata (EXIF, color profiles) adds bytes. Strip metadata and recheck.
  • Converting PNG to JPEG is unexpectedly large: The PNG might have transparency. A PNG with an alpha channel converted to JPEG gets a white background, check if the original image has transparent areas.
  • Platform rejects despite correct size: Some portals check pixel dimensions, not just file size. Read the full requirements for minimum and maximum pixel dimensions.

Bottom Line

Hitting an exact file size requires two tools: dimension control and quality control. The quality slider alone cannot reliably hit a tight target because compression ratios depend on image content, not just the quality number. Resize to the right pixel dimensions first, then use binary search on quality (or a live-estimating slider) to fine-tune. Switching to WebP buys you an extra 25–35% headroom before you need to reduce quality further. The whole process takes under a minute once you know the levers.

Frequently asked questions

How do I compress an image to exactly 200 KB?
There is no single quality setting that guarantees exactly 200 KB, compression ratios depend on image content. The reliable method: use a tool with a live size estimate, start at quality 80, and drag the quality slider left until the estimate is at or just under 200 KB. Switching to WebP output at the same quality setting produces about 25–35% smaller files, giving you more headroom.
Why does the same quality setting give different file sizes on different images?
JPEG and WebP encoders measure image complexity, specifically how much high-frequency detail is present. A smooth blue sky at quality 80 might produce 40 KB; a detailed texture at quality 80 might produce 400 KB. The quality slider controls how aggressively detail is discarded, not the final file size.
How small can I compress an image without visible quality loss?
It depends on the content and display size. For photographic content displayed at screen resolution, quality 75–80 is typically indistinguishable from the original. Below quality 60, blocking and banding artifacts become visible in smooth areas like sky or skin. For thumbnails displayed at small sizes, quality 60 is often fine.
What is the file size limit for passport photos?
It varies by country. UK passport: 50 KB minimum, 10 MB maximum. US visa (DS-160): 1 KB minimum, 240 KB maximum. Indian passport: 10 KB minimum, 1 MB maximum. Always check the specific portal's requirements, pixel dimensions are often specified too, not just file size.
Does WebP produce smaller files than JPEG at the same quality?
Yes, consistently. At equivalent visual quality, WebP lossy is typically 25–35% smaller than JPEG. This is not quality-for-quality (the codecs use different scales) but perceptual quality for equivalent perceptual quality. WebP quality 75 is roughly equivalent to JPEG quality 85 visually, at about 40% smaller file size.

Sources & references

This article was researched and written by Nikola, drawing on the following primary sources and documentation:

Ready to try it?

All tools run entirely in your browser, no uploads, no account required.

Compress Image
You're offline, cached tools still work