# Compress a PNG to a file size

> PNG is lossless, so the only way to shrink one and keep it PNG is fewer pixels. Set a KB limit and the screenshot or logo is scaled until it fits.

Use it online (free, runs in your browser): https://directory.milunir.com/image-tools/image-editors/image-compressor/compress-png/

PNG has no quality dial, so a screenshot or diagram that must stay PNG gets smaller only by losing pixels. This page keeps the PNG format and scales the picture down until it fits under your KB limit.

## Worked example

- Mode: To a file size
- Quality: 75
- Largest file size: 5
- Unit: KB
- Save as: PNG

Result: Compressed size: 4.3 KB Compared with the original: 39% smaller Pixel size: 229 × 41 px

## How it works

`chosen quality = the highest q in 5–95% where encoded size(q) ≤ limit; limit in bytes = KB × 1,024 or MB × 1,048,576`

- `q`: the JPEG or WebP quality passed to the browser encoder
- `limit`: the largest file size you typed, in bytes

The browser decodes the picture, turns it upright from its EXIF orientation tag and redraws it on a canvas. In To a file size mode it first saves at 5% quality. If that already fits, a halving search over six more saves narrows down the best quality under the limit. If even 5% is too large, the width and height are cut by about the square root of the size ratio and the search repeats. PNG output is lossless, so only the scaling step applies to it.

Sources:

- [HTML Standard: the canvas element, toBlob()](https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-toblob), WHATWG
