Home / Software y Cloud / JPEG does not compress pixels: it discards what your eye cannot perceive

JPEG does not compress pixels: it discards what your eye cannot perceive

Ilustracion tecnica de la compresion JPEG en bloques de 8x8

A 24 MB photo that weighs under 3

Take a photo with your phone and look at its raw data: uncompressed, a 4000×3000 image would take up about 24 MB. Twenty shots and you have half a gigabyte. Yet your gallery weighs a fraction of that: the JPEG format makes it happen by discarding information in a controlled way. In practice, most JPEG photos survive at around 10 % of their original size with no visible difference to the eye.

The trick is not deleting pixels at random. It is applying a model of how human vision works and throwing away precisely what you will not notice. That happens in four phases: color space, blocks, transform, and quantization.

Step 1: separate brightness from color

The camera captures each pixel in RGB (red, green, blue), but JPEG first converts it to the YCbCr color space: Y is the luma (brightness), while Cb and Cr are the two chroma (color) components. The reason is physiological: the human retina resolves far more detail in brightness than in color. You read an object’s edges from luminance contrast, not from hue.

Exploiting that asymmetry, JPEG reduces the resolution of the color components. In the most common scheme, 4:2:0, chroma is stored at half the horizontal and vertical resolution. That alone already removes half of the image’s color information, and you never perceive it.

Step 2: split the image into 8×8 blocks

The image is divided into 8×8 pixel blocks; each block is 64 numeric values. Small blocks let the algorithm focus on uniform areas —a sky, a wall— where there is very little information to encode. They also explain the format’s most famous artifact: with very aggressive compression you see visible squares, because every block was treated on its own.

Step 3: DCT, or expressing an image as frequencies

Here is the heart of the method. Each 8×8 block goes through the discrete cosine transform (DCT), a mathematical operation that expresses it as a sum of 64 frequency patterns, just as a sound wave can be decomposed into sines and cosines. The first coefficient, the DC, is simply the block’s average brightness. The other 63, the AC coefficients, represent the variations: sharp edges, fine textures, or smooth transitions.

A block of uniform blue sky has almost only a DC coefficient; an eye, a feather, or curly hair needs many high-frequency AC coefficients. The DCT by itself loses no information: it is a reversible transform. The loss happens in the next step.

Step 4: quantization, the threshold of what you cannot see

Each coefficient is divided by a number from a quantization table and the result is rounded to the nearest integer. That is where real information is discarded: small values, common at high frequencies, collapse to zero. The table —and the famous quality factor from 1 to 100— decides how hard each frequency is crushed. A high factor keeps almost everything; a low one turns into zeros the frequencies the eye barely detects.

Rounding introduces a small error, but that is the format’s bet: the error lands exactly in the range human vision does not perceive.

Step 5: zigzag and Huffman, the lossless part

After quantization, a typical block holds many consecutive zeros. The coefficients are read in zigzag order, from low to high frequency, so the zeros group into long runs. Then run-length encoding (RLE) and a Huffman code are applied: frequent symbols —zero above all— get short codes, and rare ones get long codes. This phase is 100 % reversible: nothing is lost here, you only compact what was already quantized.

It is the same principle as a ZIP file, but tuned to the statistics of a transformed image.

And where do the artifacts come from?

When the quality factor drops too far, quantization crushes mid frequencies as well. The result is two classic defects: blocking, the visible 8×8 grid, and ringing, a ghost halo around sharp edges, because the coefficients that created the exact transition are gone.

To be precise, JPEG is not only lossy: there is a lossless mode (JPEG-LS) and the progressive mode, which first sends a blurry version of the image and then refines it — the one you see loading on slow websites. And for a photo, PNG is almost never a good alternative: its compression is purely statistical, and the smooth gradients of a photograph make the file grow.

The heir of the format that photographed the world

Baseline JPEG was standardized in 1992, and since then almost everything we photograph passes through it. Its modern successors, AVIF and JPEG XL, keep the same idea —transform, quantize, encode— but with larger blocks (up to 256×256), more flexible transforms, and prediction between blocks. That is why they compress up to 50 % better at identical quality.

Next time your phone says «2.4 MB», remember: it did not erase the photo at random. It discarded, using a mathematical model of your eye, what you were never going to notice.