Compression

How Image Compression Works: Lossy Versus Lossless

By MosaicBits Editorial Team · · 4 min read

A raw photo from a modern camera can take many megabytes. Put dozens of them on a web page and it crawls. Compression is what makes images practical to store and send. It works by finding more compact ways to describe the same picture, and sometimes by giving up a little detail on purpose.

There are two broad families. Lossless methods keep every pixel exactly. Lossy methods throw away information the eye is unlikely to miss. Understanding the difference helps you pick the right format and settings.

Why Images Compress at All

Uncompressed images store a number for every color channel of every pixel, even when many pixels are identical or nearly so. Real images are full of redundancy.

  • Repetition: a blue sky contains thousands of near-identical pixels in a row.
  • Predictability: a pixel's value is often close to its neighbors.
  • Perception limits: human eyes are less sensitive to fine color detail than to brightness detail, and less sensitive to subtle high-frequency texture than to broad shapes.

Compression algorithms exploit these properties.

Lossless Compression

Lossless compression reduces file size without changing any pixel. Decompress the file and you get back exactly what you started with, bit for bit. It works much like a zip archive.

Common techniques include:

  • Run-length encoding, which replaces a long run of identical values with a count and a value.
  • Dictionary methods, which replace repeated patterns with short references.
  • Prediction and filtering, which store the difference between a pixel and a prediction from its neighbors. Differences are mostly small numbers, which compress well.
  • Entropy coding, which gives common values shorter codes than rare ones.

PNG uses prediction filters followed by a general compression method. GIF uses a dictionary approach but is limited to a small palette of colors. Formats such as TIFF can use lossless compression as well, and WebP and others offer lossless modes.

Lossless works very well on graphics with flat areas, such as logos, diagrams, screenshots and text. It helps less on photographs, where noise and fine texture make pixels unpredictable.

Lossy Compression

Lossy compression achieves much smaller files by permanently discarding some information. The goal is to throw away data that people are least likely to notice.

The JPEG method is the classic example. In outline, it works like this:

  1. Convert color into a brightness component and color components.
  2. Reduce color resolution, since the eye notices color detail less than brightness detail. This step is called chroma subsampling.
  3. Divide the image into small blocks and transform each block into frequency components, separating smooth variations from fine detail.
  4. Quantize the components, which rounds them more coarsely, especially the fine-detail ones. This is where information is actually lost.
  5. Encode the result with lossless techniques.

A quality setting controls how strongly the quantization step rounds. Low compression keeps nearly all detail. High compression produces small files with visible flaws.

Other lossy formats, including WebP and AVIF, use more advanced prediction and transforms, and often get smaller files at similar visual quality.

What Lossy Artifacts Look Like

Heavy lossy compression leaves recognizable marks.

  • Blockiness: visible squares, especially in smooth gradients.
  • Ringing: fuzzy halos around sharp edges and text.
  • Banding: smooth gradients turning into steps of color.
  • Color smearing: bleeding of color across edges.

Once information is discarded, it cannot be restored. Saving a lossy file again re-compresses it and adds more damage, a problem called generation loss. Keep your original and export copies from it.

Choosing Between Them

Use lossless when:

  • exact pixels matter, as with screenshots, interface graphics or technical images
  • the image has flat colors, sharp lines or text
  • you will edit the file further and want to avoid cumulative loss
  • you need transparency with clean edges

Use lossy when:

  • the image is a photograph or has continuous tones and gradients
  • file size and loading speed matter more than perfect fidelity
  • the image is a final delivery copy, not a working file

Many formats offer both modes, so check the options in your export tool.

Tips for Good Results

  1. Start from the best-quality source you have.
  2. Resize to the dimensions you need before compressing. Compressing a large image that will be displayed small wastes bytes.
  3. Experiment with quality settings and view the result at actual size. Often a moderate setting looks identical to the eye while saving a lot of space.
  4. Remove unnecessary metadata, such as embedded thumbnails and camera data, when privacy or size matters.
  5. Keep an uncompressed or lossless master for future edits.
  6. Test with a few representative images, since some compress much better than others.

Image Formats at a Glance

  • JPEG: lossy, ideal for photos, no transparency.
  • PNG: lossless, supports transparency, good for graphics and screenshots.
  • GIF: lossless but limited to a small color palette, supports simple animation.
  • WebP: supports lossy and lossless modes, transparency and animation.
  • AVIF: modern lossy and lossless format with strong compression, with support that varies on older software.
  • SVG: not pixel-based, stores shapes as text, ideal for scalable graphics.

Frequently Asked Questions

Is lossless always better quality than lossy?

Lossless keeps every pixel exactly, so it preserves the original perfectly. But the file is usually larger. A lossy image at a high quality setting can look nearly identical to most viewers while being much smaller, so the better choice depends on the use.

Does saving a JPEG repeatedly reduce its quality?

Yes. Each save re-applies lossy compression to an already compressed image, and small errors accumulate. Edit from the original file, and export a JPEG only as the final step.

Can I turn a lossy image back into a lossless one?

You can save it in a lossless format, which stops further loss, but you cannot recover the detail already discarded. The new file may even be larger than the original JPEG without any gain in quality.

Related reading