Performance Engineering • 7 min read • Updated November 2026
Lossless vs Lossy Compression: How Image & PDF Compression Works
How lossy and lossless encoders choose what to throw away: DCT quantization tables, DEFLATE and Huffman coding, and what each format costs a real file.
1. The Fundamental Difference: Reversible vs. Irreversible
Lossless Compression
Reduces file size by identifying and eliminating statistical redundancy without removing any original information. Decompressing a lossless file yields an exact, bit-for-bit duplicate of the original data.
Common formats: PNG, GIF, ZIP, FLAC, PDF text streams.
Lossy Compression
Achieves dramatically smaller file sizes by discarding perceptual data that the human eye or ear is least capable of detecting (psychoacoustic and psychovisual modeling).
Common formats: JPEG, WebP (lossy mode), MP3, MP4/H.264.
2. Inside Lossy Image Compression: JPEG & DCT
When you run an image through a lossy compressor like ToolVerve's client-side Image Compressor, the pipeline follows four sequential stages:
- Color Space Transformation (RGB to YCbCr): The image is separated into Luminance (Y, brightness) and Chrominance (Cb/Cr, color). Because human eyes are far more sensitive to brightness than subtle hue shifts, the color channels are downsampled (Chroma subsampling 4:2:0 or 4:2:2).
- Discrete Cosine Transform (DCT): The image is sliced into 8×8 pixel blocks. The DCT converts spatial pixel color values into frequency coefficients (low-frequency base gradients vs high-frequency sharp details).
- Quantization: High-frequency coefficients are divided by values in a quantization matrix and rounded to integers. This is where irreversible file reduction occurs: fine noise and imperceptible micro-variations are set to zero.
- Entropy Encoding (Huffman / Arithmetic): The remaining non-zero numbers are encoded into compact binary bitstreams using Huffman coding, where frequently occurring values take fewer bits.
3. How PDF Compression Works
PDF documents are compound binary containers holding vector paths, fonts, forms, and embedded raster images. PDF compression uses a hybrid strategy:
- Raster Image Downsampling: High-resolution 300+ DPI scans are resampled to 150 DPI and re-encoded using DCT (JPEG) streams.
- Flate / DEFLATE Stream Compression: Vector text, coordinates, and content dictionaries are compressed losslessly using the DEFLATE algorithm (LZ77 + Huffman coding).
- Object Pruning & Linearization: Unused cross-reference table entries, duplicate embedded font subsets, and outdated revision histories are stripped from the document tree.
4. When to Use Which Method
| Asset Type | Recommended Mode | Target Format |
|---|---|---|
| Logos, Icons, Line Art | Lossless | SVG or PNG-8 / WebP |
| Hero Banners & Photos | Lossy (75–85% quality) | WebP or AVIF |
| Signed Legal Contracts | Hybrid Lossless/Scanned | PDF (Optimized streams) |
| App UI Screenshots | Lossless or high WebP | PNG or WebP |
5. What Compression Cannot Undo
The quality slider controls how much is discarded on the way out. It cannot reach backwards. Re-saving a JPEG at a higher setting does not restore detail that an earlier save threw away; it encodes the already-degraded pixels more faithfully, so the file grows while the picture stays soft. The same reason makes repeated saves costly: each re-encode applies quantisation to coefficients that were already an approximation, so a JPEG saved five times is softer than the same file saved once at the same setting, even though nobody changed the slider.
Damage also appears unevenly, which is why one image can look fine and another look ruined at the identical quality number. Hard edges suffer first — text and logos develop ghosting around the strokes, flat skies develop banding, and skin develops blotches. A detailed landscape hides the same settings because the busyness masks the error. If the image contains type, judge it at 100% zoom on the letters, not on the thumbnail.
Size behaves the same way for the opposite reason. Lossless coders work by predicting pixels from their neighbours, so anything unpredictable costs bytes: film grain, sensor noise at high ISO, and dithered gradients are the most expensive content you can put in a file. A screenshot of a settings panel can shrink to a tenth of its raw size; a noisy night photo barely moves. Two images at the same pixel dimensions can therefore need completely different settings to reach the same target weight.
Which points at the one move that beats every slider: resize before you compress. Dropping a 4000 × 3000 photo to 2000 × 1500 removes three quarters of the pixels before any encoder is involved, and a page that displays the image at 800 px wide never needed the other half. Set the dimensions you actually use, then walk the quality control down until the first visible change appears at 100% zoom, and step back one notch. That operating point is measured from your own file, which is the only one that matters.
Questions About Reversibility, Sliders and File Types
I over-compressed a screenshot, then moved the slider back up. The text stayed soft. Why?
Because encoding only decides what to discard on the way out; it cannot reach backwards. Re-encoding a degraded JPEG at a higher setting encodes the artefacts more faithfully, so the file grows and the ghosting stays. On flat-colour artwork the loss shows up as banding in gradients and blotches on skin, which is why the guide says to judge at full zoom on the letterforms.
Is any path through this compressor lossless?
No. It accepts PNG, JPEG and WebP and re-encodes pixels in both modes, with quality bounded between 30 and 95 percent. A PNG-8 of a logo, or an untouched ZIP-equivalent re-save, is not what this page produces. Use it for photographs; keep line art in a format that does not quantise in the first place.
Target-size mode asks for 100 KB and hands me back the original file. Did it ignore me?
If the input is already under the ceiling, returning it unchanged is the correct answer, and the row tells you so instead of re-encoding needlessly. When it does work, it aims at 99 percent of the requested bytes and walks quality down first, then resolution, through eight steps toward a 200 px floor. A pathologically dense image against a very small target can still miss; the badge is the measurement.
Why did my transparent PNG come out on a white background?
That is the JPEG output flattening alpha onto white, which every browser does when the container cannot hold transparency. WebP keeps the alpha channel in target mode. Note that the format switch is only offered there: the quality-mode path does not expose an output format control at all.
Is 4096 pixels a limit I can raise?
It is the hard dimension cap in both paths — a maximum width or height of 4096 in quality mode, the same figure as maxDimension in target mode — so a 6,000 px panorama is resampled before any encoder runs and that pixel loss is not a quality setting you can tune. Batch work is also strictly sequential, so twenty photos are twenty passes, not one parallel queue.
Keep Reading
Three guides that pick up where this one leaves off:
- WebP vs PNG vs JPG: Which Image Format Should You Use in 2026? — The theory applied: which format family uses which method, and what it does to a picture.
- How to Compress Images to Exact File Sizes (50KB, 100KB, 200KB) — The hands-on version of choosing between the two families under a hard byte limit.
- How to Compress PDF Documents for Email and Web Uploads — The same split, applied to documents instead of photographs.