JA EN

Image Compression Explained - How JPEG, PNG, and WebP Work

· 9 min read

What Is Image Compression - Two Approaches to Reducing File Size

Image compression is the technology of reducing image file sizes. Since it directly impacts web page loading speed, it's essential knowledge for frontend developers and designers. According to HTTP Archive research, images account for approximately 50% of total web page transfer size, making image optimization one of the most effective means of performance improvement.

Compression methods fall into two broad categories:

The choice between these methods depends on the image's purpose and required quality. For web use, the balance between display quality and file size is the most critical decision factor. Lossy compression leveraging human visual characteristics can reduce photo sizes to levels where quality degradation is barely perceptible, making it highly effective from a web performance perspective.

How JPEG Works - Frequency Transform via DCT

JPEG is a lossy compression format built around the Discrete Cosine Transform (DCT). Standardized in 1992, it has reigned as the de facto standard for photo formats for over 30 years. The compression process proceeds through these steps:

JPEG's weakness is visible block noise (mosquito noise) on images with sharp edges like text or line art. Processing in 8x8 blocks creates discontinuities at block boundaries. It also doesn't support transparency (alpha channel), making it unsuitable for images requiring transparent backgrounds.

How PNG Works - Filtering and Deflate

PNG is a lossless compression format that perfectly preserves the original image data after decompression. Created in 1996 to circumvent GIF patent issues, it has become the standard lossless image format on the web. PNG compression consists of two stages.

Filtering: For each pixel row, the optimal filter is selected from 5 types (None, Sub, Up, Average, Paeth). Filters calculate differences from adjacent pixels, increasing data redundancy. For example, in areas of nearly uniform color like blue sky, applying the Sub filter makes differences approach zero, dramatically improving compression efficiency. The Paeth filter calculates predicted values from three pixels (left, above, upper-left) and is the most sophisticated filter, effective for images with gradients.

Deflate compression: After filtering, Deflate compression combining LZ77 algorithm and Huffman coding is applied. LZ77 detects repeated byte sequences and replaces them with distance-length references, while Huffman coding uses variable-length codes based on frequency. The sliding window size (maximum 32KB) affects compression ratio - larger windows can reference more distant patterns but increase processing time.

PNG supports alpha channels (transparency) and achieves high compression ratios for images with few colors or sharp edges. However, for photographs with complex color variations, file sizes can be 3-5x larger than JPEG. The distinction between PNG-8 (256 colors, palette mode) and PNG-24/32 (full color) is important - PNG-8 is ideal for icons and logos. PNG-8 can include alpha channels, but transparency is expressed only within the 256-color limitation.

WebP Technology - Next-Generation Format Based on VP8

WebP is a format developed by Google in 2010 that supports both lossy and lossless compression modes. The lossy mode applies VP8 video codec technology to still images, achieving 25-34% smaller file sizes compared to JPEG. Designed primarily to improve web performance, it offers an excellent balance of encode/decode speed and compression efficiency.

Lossy WebP features: Like JPEG, it uses block-based predictive coding, but with 4x4 pixel intra-prediction that reduces block boundary artifacts. Four prediction modes are available - horizontal, vertical, DC (average), and TrueMotion - with the optimal mode selected per block. It also employs Boolean Arithmetic Coding, achieving higher compression efficiency than Huffman coding. Alpha channel support allows transparent photos in a single file.

Lossless WebP features: Uses a proprietary algorithm combining spatial prediction, color cache, LZ77 back-references, and Huffman coding to produce files approximately 26% smaller than PNG. The color cache remembers recently used colors and efficiently encodes their reoccurrence. Multiple preprocessing steps exploit image-specific redundancy, including palette transformation and delta storage in the green channel.

As of 2024, all major browsers (Chrome, Firefox, Safari, Edge) support WebP, and practical compatibility issues are largely resolved. However, support in image editing software varies, and depending on your workflow, you may need to use JPEG/PNG alongside WebP. Encoding speed is comparable to JPEG and significantly faster than AVIF.

Compression Quality vs File Size - Finding Optimal Quality Settings

In lossy compression, the quality parameter determines the tradeoff between file size and image quality. However, the relationship between quality value and file size is non-linear and varies significantly based on image content. Here's practical knowledge for finding optimal quality settings.

JPEG quality settings: Reducing quality from 100 to 95 cuts file size by 30-50% with virtually imperceptible visual difference. The 95-to-85 range yields another 30-40% reduction at a level where degradation is unnoticeable without careful comparison. From 85 to 75, significant size reduction is achieved but banding (stepping) becomes visible in gradient areas. Below 75, quality degradation becomes pronounced and is not recommended for web use.

WebP quality settings: WebP's quality parameter uses the same 0-100 range as JPEG but achieves higher compression efficiency at equivalent values. WebP quality 75 delivers visual quality equivalent to JPEG quality 85 at a smaller file size. WebP also has a unique method parameter (0-6) where higher values spend more encoding time to improve compression ratio.

Objective quality assessment with SSIM: SSIM (Structural Similarity Index) is widely used as a quality metric that closely matches human perception. SSIM above 0.95 is considered imperceptible to most viewers. In automated pipelines, setting a target SSIM and auto-adjusting quality parameters is effective. Use cjpeg's -quality option or cwebp's -q option to specify quality, and measure SSIM with the dssim command to search for optimal values.

Practical Format Selection - Choosing the Right Format by Use Case

Here's a summary of format selection criteria. In practice, you need to consider not just image type but also delivery environment and workflow.

Ultimately, the most reliable approach is testing multiple formats on your actual images and visually confirming the quality-to-size balance. For automation, build logic that routes images to formats based on characteristics like color count, edge density, and transparency requirements. Libraries like Sharp or ImageMagick make automated multi-format generation straightforward during build time.

Related Articles

WebP Advantages and Browser Support - Next-Gen Image Format

Learn about WebP format benefits, drawbacks, and browser compatibility. Everything you need to decide whether to migrate from JPEG and PNG.

Image Format Comparison - JPEG/PNG/WebP/AVIF/GIF/BMP Features and Use Cases

Compare technical characteristics of 6 major image formats. Organized comparison of compression methods, color depth, transparency, animation, and browser support with optimal format selection by use case.

Lossless vs Lossy Compression - How to Choose the Right Image Compression

Compare lossless and lossy compression mechanisms, characteristics, and use cases to choose the optimal compression method for your images.

History of Image Formats - 40 Years of Evolution from BMP to AVIF

Explore the chronological history of major image formats including BMP, GIF, JPEG, PNG, WebP, and AVIF. Learn the context and technical innovations behind each format's creation.

Deep Dive into Image Compression Algorithms - DCT, Wavelet Transform, and Predictive Coding

In-depth explanation of core image compression technologies. Understand the mathematical principles behind JPEG's DCT, JPEG 2000's wavelet transform, H.265/AV1 predictive coding, and entropy coding.

Image Format Conversion Best Practices - Maintaining Quality During Conversion

Strategies for preserving image quality during format conversion. Learn to avoid recompression degradation, maintain color spaces, and manage metadata properly.

Related Terms