How Image Compression Works: Lossless, Lossy and the Quality Slider
A 12-megapixel photo is 36 million bytes of raw colour data, yet a phone saves it in a few megabytes. Two very different tricks close that gap, and knowing which one a format uses explains most image-quality surprises.
Every digital image is a grid of pixels, and each pixel needs a few numbers to describe its colour. Compression is the set of tricks that makes the same picture fit in far fewer bytes. To choose a format, see PNG vs JPG vs WebP.
Start with the raw size
A normal colour pixel uses 3 bytes: one each for red, green and blue. Add a fourth byte for transparency (the alpha channel) and you have RGBA.
4000 x 3000 x 3 = 36,000,000 bytes (about 34.3 MiB)
1920 x 1080 x 4 = 8,294,400 bytes (about 7.9 MiB)
The first line is a 12-megapixel photo; the second, a full-HD screenshot with transparency.
Two kinds of compression
Lossless compression writes patterns more briefly, so every pixel can be rebuilt exactly (PNG, GIF, lossless WebP). Lossy compression first throws away information that matters little to human eyes, then packs the rest (JPEG, lossy WebP, AVIF, HEIC).
How PNG compresses without losing anything
PNG uses two steps, both defined in the W3C PNG specification.
Step 1: filtering
Before compressing, PNG can replace each byte with the difference between it and a neighbour. There are five filters: None, Sub (compare with the pixel to the left), Up (the pixel above), Average (the mean of left and above) and Paeth (whichever of left, above or upper-left is the best guess). The encoder can pick a different filter for each row.
A row of a smooth gradient might read 10, 12, 14, 16, 18, 20, 22, 24. After the Sub filter it reads 10, 2, 2, 2, 2, 2, 2, 2. The picture has not changed at all, but a long run of identical numbers is far easier to compress.
Step 2: DEFLATE
The filtered bytes then go through DEFLATE, the same lossless method used in ZIP files. It combines two ideas. LZ77 spots repeated sequences and replaces later copies with a short note that says "go back this far and copy this many bytes". Huffman coding then gives the most common values the shortest codes, like a shorthand.
This is why a PNG screenshot stays razor sharp and is often surprisingly small: flat backgrounds and repeated letter shapes compress very well. In a photo, sensor noise makes neighbouring pixels slightly different everywhere, so there are few exact repeats and the PNG stays large.
How JPEG throws away what you are unlikely to miss
JPEG, defined in ITU-T recommendation T.81, is built around human vision: we notice brightness more than colour, and broad shapes more than tiny, fast-changing detail. An encoder works in five stages.
- Convert the colours. Red, green and blue are turned into one brightness value (Y) and two colour-difference values (Cb and Cr). The JFIF standard gives the formula below. A mid grey of 128, 128, 128 becomes Y 128, Cb 128, Cr 128: the two colour values sit at 128, which means "no colour".
- Reduce the colour detail. Because our eyes are less sensitive to colour, many encoders, including libjpeg by default, keep only one Cb and one Cr value for each 2 x 2 group of pixels. This is called 4:2:0 chroma subsampling. For a 4000 x 3000 photo, the samples drop from 36,000,000 to 18,000,000 before any real compression starts.
- Cut the picture into 8 x 8 blocks. The brightness of a 4000 x 3000 photo becomes 187,500 blocks, and each colour channel about 47,000 more. Each block goes through a discrete cosine transform (DCT), which rewrites its 64 pixels as 64 frequency values. The top-left value is the block's average; the others describe finer and finer patterns.
- Quantise. Each of the 64 values is divided by a step size from a quantisation table and rounded to a whole number. Fine detail gets big steps, so many of those values round to zero. This is the only stage where information is deliberately thrown away.
- Pack the result. The values are read in a zigzag from coarse to fine, runs of zeros are recorded briefly, and Huffman coding packs everything into the final file.
Cb = -0.1687 R - 0.3313 G + 0.5 B + 128
Cr = 0.5 R - 0.4187 G - 0.0813 B + 128
What the quality slider actually changes
The quality number does not set a file size. It scales the quantisation tables. The JPEG standard includes example tables, and the widely used libjpeg library treats quality 50 as "use them as they are". Below 50, the tables are scaled to 5000 / quality percent (quality 10 means 500%). From 50 up, they are scaled to 200 - 2 x quality percent (quality 75 means 50%). At 100 every step becomes 1.
The table follows one block through that formula. The first column of steps is for the block average (16 in the standard brightness table). The second is for the finest detail (99 in the same table). The last column shows what happens to a fine-detail value of 37 after quantising and decoding.
| Quality | Table scale | Step for the block average | Step for the finest detail | A detail value of 37 becomes |
|---|---|---|---|---|
| 10 | 500% | 80 | 255 (capped) | 0 |
| 50 | 100% | 16 | 99 | 0 |
| 75 | 50% | 8 | 50 | 50 |
| 90 | 20% | 3 | 20 | 40 |
| 100 | 0% | 1 | 1 | 37 |
At quality 50 and below, that bit of fine texture is deleted. At 75 it survives but is rounded heavily; at 90 it is close. Lots of zeros make a small file, which is why lower quality means smaller files and softer detail. Other apps may map quality numbers differently, so quality 80 in one program is not exactly quality 80 in another. In a web page, code asks the browser for a JPEG or WebP with a quality between 0 and 1.
Why re-saving a JPEG makes it worse
Each time you edit and re-save a JPEG, the encoder quantises the already damaged picture again. Rounding errors pile up, and blocks and halos around text grow. Cropping or resizing between saves shifts the 8 x 8 grid, which tends to make it worse, and converting to PNG afterwards just preserves the damage. Edit from the original and save as JPEG once, at the end.
WebP and AVIF in brief
Lossy WebP borrows from the VP8 video codec. It predicts each block from pixels already decoded around it, then transforms and quantises only the difference, and uses arithmetic coding, which Google says packs data 5 to 10% better than JPEG's Huffman coding. Lossless WebP uses predictors, colour transforms, a cache of recent colours and LZ77-style back references. AVIF stores AV1-compressed images inside the HEIF container, with an even larger toolbox of block sizes and prediction modes.
How a "compress to 200 KB" tool works
A tool cannot calculate the right quality for a size limit directly, because file size depends on the picture: a busy photo of leaves needs more bytes than a clear sky at the same quality. So the tool searches.
- Save the image at the highest quality allowed. If it already fits the target, stop.
- Save it at the lowest quality allowed. If even that is too big, the image needs fewer pixels.
- Otherwise, try the middle quality. If it fits, search the upper half; if not, the lower half. Repeat until the range closes. Halving a range of 94 quality steps needs at most 7 tries, so the whole search takes no more than 9 saves.
When quality alone cannot reach the target, the tool shrinks the dimensions. File size roughly follows the number of pixels, and the number of pixels follows the square of the scale. To halve the file you scale each side to about 71% (the square root of one half), not 50%.
A 4000 x 3000 photo is 400 KB at the lowest quality you will accept, and the form wants 200 KB. Scaling each side to 71% gives about 2828 x 2121 pixels, which is half the original pixel count. A tool that aims 5% lower to leave a safety margin would try about 2687 x 2015, then run the quality search again at that size.
Our image compressor works exactly this way. With a target size, it searches for the highest quality that fits. If you tick "Allow smaller dimensions", it keeps quality at 40 or above and scales the picture down, with a small margin, until the target is reachable. All of it happens in your browser.
Some websites count a kilobyte as 1,000 bytes and others as 1,024. Our compressor uses 1,024. For a "200 KB" limit, a 190 KB target (194,560 bytes) fits under either reading.
Practical tips
- Resize first. Shrink the image to the size it will be shown at with the image resizer, then compress. You can keep a higher quality that way.
- Match the method to the content. Lossless for text and screenshots; lossy for photos. A photo saved as PNG usually shrinks a lot with PNG to JPG.
- Check at full size. Look at faces, text and sharp edges. Blocks and halos show up there first.
- Keep the original. Every lossy save loses a little more. Make copies from the best version you have.
- Drop hidden extras. Camera details and location take a little space and can reveal more than you mean to. The metadata remover saves a clean copy.
Frequently asked questions
Is JPEG quality 100 lossless?
No. At quality 100 the widely used libjpeg library sets every quantisation step to 1, so very little is thrown away, but the colour conversion and rounding still change some pixel values slightly, and many encoders still reduce colour detail. The file is also very large. If you need an exact copy, use a lossless format such as PNG.
What JPEG quality should I use?
There is no single right number, and the scale differs between apps. Around 80 to 90 is a common starting point for photos you share. Lower values suit small thumbnails. Look at the result at full size, especially around faces, text and sharp edges, before you settle on a setting.
Why did my file get bigger after converting it?
Converting a JPG to PNG stores the decoded pixels exactly, including the faint noise JPEG added, and lossless compression cannot shrink noise much. Re-saving an already small JPG at a higher quality can also make it bigger without adding any real detail.
Does resizing reduce file size more than lowering quality?
Often, yes. Halving the width and height leaves a quarter of the pixels, and file size roughly follows the pixel count. If an image is much larger than it will be shown, resizing first lets you keep a higher quality setting.
Will zipping my photos make them much smaller?
Usually not. JPG, PNG and WebP files are already compressed, and ZIP uses the same kind of lossless method that PNG uses. Zipping is still handy for sending many files as one, but expect only a small saving.