What Makes PNG Images So Efficient? A Deep Dive into Types, Structure, and Compression
This article explains the PNG image format, covering its lossless compression, different PNG types (8, 24, 32), internal data structure, how prediction and Deflate compression work, and tips for achieving better compression by using simple, low‑color images.
What is PNG
PNG (Portable Network Graphics) is a widely used lossless image format. It offers advantages such as lossless compression, smaller file size, and support for transparency. Lossless compression: PNG uses a LZ77‑derived algorithm to compress data without losing information. Small size: Special encoding reduces redundant data, making PNG files smaller than other formats under the same quality constraints. Transparency: PNG supports 256 levels of alpha, allowing smooth blending with any background, a feature not available in GIF or JPEG.
PNG Types
PNG images come in three main types: PNG‑8, PNG‑24, and PNG‑32. PNG‑8: Uses 8 bits (256 colors) and is suitable for images with few colors. PNG‑24: Uses 24 bits (8 bits per R, G, B) to represent up to 16,777,216 colors, but results in larger files. PNG‑32: Extends PNG‑24 with an additional 8‑bit alpha channel (R, G, B, A), supporting both full color and 256 levels of transparency.
PNG Data Structure
The PNG file consists of a header followed by a series of data chunks, similar to an HTTP response.
Opening a PNG in a hex editor shows the following key parts:
8950 4e47 0d0a 1a0a : PNG signature 0000 000d : IHDR chunk length (13) 4948 4452 : Chunk type "IHDR" 0000 02bc : Image width 0000 03a5 : Image heightEach hexadecimal block represents a specific meaning in the file.
Which PNG Images Compress Better
Images with fewer colors and less color variation compress more efficiently. A simple two‑color image can be represented by an array like [0,1], dramatically reducing size.
PNG Compression
PNG compression occurs in two stages: prediction and compression.
Prediction
PNG applies delta encoding to each pixel channel. Common filters include None, Sub (X‑A), Up (X‑B), Average (X‑(A+B)/2), and Paeth.
For example, a gradient image with values [1,2,3,4,5,6,7,8] filtered with X‑A yields [1,1,1,1,1,1,1], a sequence that compresses well.
Compression
The filtered data is then compressed using the Deflate algorithm, which combines Huffman coding and LZ77. Unnecessary metadata (e.g., Photoshop creation info) can be removed by exporting for web, further reducing file size.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
