Learning Center · Compression · Beginner

Lossless vs lossy compression: what gets thrown away

Lossless compression gives back every byte. Lossy compression discards detail on purpose. See what JPEG and MP3 drop, why re-saving hurts, and what to keep.

By listme.name Updated 7 min read 1:26 video Share

1:26 video with captions. The text below has the detail and the sources.
Read the video transcript

The trade-off

Lossless compression gives back exactly what it started with. Lossy compression throws detail away on purpose. Which one should a file use?

Fewer levels, fewer bits

Each sample can take one of two hundred fifty-six values: eight bits. Keep sixteen levels, and each sample takes four bits. Keep four levels, and each sample takes two bits. The shape survives, but the fine steps are gone.

What lossy formats drop

JPEG rounds fine detail to coarser steps, in blocks of eight by eight pixels. MP3 and AAC drop quiet sounds that a louder sound masks.

Lossless or lossy

Lossless output matches the input exactly. Lossy output is an approximation. Common lossless formats include PNG, FLAC and ZIP. Common lossy formats include JPEG, MP3 and AAC. A lossless file loses nothing when saved again. A lossy one loses more each time it is edited and saved.

Keep a lossless master

Keep a lossless master, such as a raw photo. Export lossy copies only for delivery. Keep text, code, spreadsheets and backups lossless.

What to remember

Lossy compression is a deliberate choice about detail. Read the full guide for formats and the master rule.

Lossless compression gives back exactly the bytes it started with. Lossy compression keeps an approximation instead, discarding detail that the format judges least important, so the file is smaller but can never be restored exactly. PNG, FLAC and ZIP are lossless; JPEG and MP3 are lossy. The video above shows the core idea.

What each kind keeps

The difference lies in the round trip. A lossless method compresses the data, then decompresses it, and the output matches the input bit for bit. Huffman coding and DEFLATE work this way, as how file compression works explains. A lossy method accepts that the decoded result differs from the original. Wikipedia describes lossy compression as using "inexact approximations and partial data discarding" to represent the content.

The two kinds get smaller for different reasons. A lossless file shrinks by writing the same information more briefly. A lossy file shrinks because information was removed. Lossy methods are chosen when the removed detail is unlikely to matter to the person looking or listening.

Fewer levels: quantization in one example

Quantization replaces each value with the nearest value from a finite set of allowed values. Take a sample on a scale from 0 to 1 whose exact value is 0.37. If only four levels are allowed (0, one third, two thirds and 1), the nearest is one third, about 0.33. Four levels need two bits per value, because two bits give four patterns. With 256 levels, the same sample is stored as 94 out of 255, about 0.369, which is almost exact. Two hundred fifty-six values need eight bits, sixteen values need four bits, and four values need two bits.

The gap between the true value and the stored value is the lost information. Fewer levels make the gap larger, and a smooth curve turns into visible steps. The lossy demonstration in the video uses exactly these three steps: 256 levels, then 16, then 4.

How JPEG drops fine detail

JPEG, the format most photographs use, works on blocks of 8 by 8 pixels, a detail that RFC 2435 also describes for JPEG payloads. For each block, a discrete cosine transform converts the pixel values into frequency components. A few components describe the broad, smooth shape of the block, and the rest describe finer detail. The transform by itself loses nothing. The loss comes next: quantization divides each component by a number from a table and rounds the result. Coarser divisors push more of the high-frequency components to zero, and runs of zeros pack very tightly before a lossless step writes them out.

Color gets similar treatment. The eye is less sensitive to fine color detail than to fine brightness detail, so JPEG stores color at a lower resolution than brightness. The high-frequency components contribute less to the overall picture than the low-frequency ones, so removing them changes the picture less.

The quality setting controls how coarse the divisors are, and nearly all JPEG software lets you choose it. Higher quality keeps more detail and makes a larger file. An excessively low setting discards the high-frequency components altogether, and the damage becomes visible.

How MP3 and AAC drop sound

Audio codecs use a different model of what can be lost. A psychoacoustic model describes which parts of a digital audio signal can be removed or reproduced with reduced quality without significant loss in perceived quality. Its main tool is auditory masking: the perception of one sound is affected by the presence of another. Masking that happens at the same time, in the frequency domain, is called simultaneous, frequency or spectral masking. A quiet note close in pitch to a louder note sounding at the same moment is hard to hear, so the encoder can store it coarsely or not at all.

MP3 "takes advantage of a perceptual limitation of human hearing called auditory masking", according to its Wikipedia article. AAC quantizes its frequency-domain signal "based on a psychoacoustic model". The model describes typical hearing, so it does not guarantee that no listener will notice the difference.

Why re-saving a lossy file hurts

Editing a lossy file means decoding it, changing it and encoding it again. Wikipedia calls the result generation loss: repeatedly compressing and decompressing a lossy file "will cause it to progressively lose quality". Suppose you open a JPEG, adjust its brightness, save it as JPEG, then crop it and save it again. Each save quantizes data that was already approximated, so the errors add up. The generation loss article on Wikipedia shows a JPEG rotated by 90 degrees over and over without a lossless tool, and the image visibly degrades with each rotation.

Some edits avoid re-encoding. A tool that rotates or crops a JPEG by changing the compressed data directly, without decoding it, can keep the rest of the picture unchanged. The lossy compression article notes that some editing of lossy files without loss of quality is possible in this way.

Which formats are lossless or lossy

Lossless formats in common use:

  • PNG for images. The PNG specification describes it as a format for "lossless, portable, well-compressed storage of raster images" (RFC 2083).
  • FLAC for audio. The name stands for Free Lossless Audio Codec, and the project describes it as lossless (xiph.org).
  • ZIP archives. Most ZIP files store their contents with the DEFLATE method, which is lossless (PKWARE's application note, and RFC 1952 for the gzip format that uses DEFLATE).
  • WebP in lossless mode. Google's WebP documentation describes the format as offering both lossless and lossy compression.

Lossy formats in common use:

  • JPEG for photographs, a Huffman-coded, DCT-based lossy image format according to the JPEG committee.
  • MP3 and AAC for audio.
  • H.264 (AVC) for video. Wikipedia lists DCT-based lossy coding as used by MPEG and H.264/AVC video standards.
  • WebP in lossy mode.

WebP shows that the extension alone does not settle the question. Check which mode a file was saved in.

Keep a master and export copies

The practical rule follows from generation loss. Keep a lossless master, and export lossy copies only for delivery. Wikipedia recommends producing lossy files from lossless originals and editing copies of the originals, such as raw camera images rather than JPEGs. A master can be a raw photo or a PNG, and the lossy copy is what you send, upload or publish.

Quality settings are a dial, not a switch. A higher setting keeps more detail and makes a larger file, and a lower setting makes a smaller file that shows its losses sooner. No setting suits every image or recording, so check the export at the size and device where people will see it. Photo and video editors are compared in the photo editing software ranking and the video editing software ranking.

Some data must never be lossy. Lossless compression is typically required for text and data files, such as bank records or text articles, according to Wikipedia. Text, source code, spreadsheets, programs and backups belong in this group. A changed byte in a program or a spreadsheet formula is a fault, not a small loss of quality. Store these with a lossless format such as ZIP or gzip. Choosing a compression algorithm compares the lossless options for each job.

Sources