All formats

PNG (Portable Network Graphics)

Full namePortable Network Graphics
Extensions.png, .apng (animated)
MIME typeimage/png, image/apng
Typeraster image, lossless compression
Starts with89 50 4E 47 0D 0A 1A 0A (\x89PNG\r\n\x1a\n)
SpecificationPNG Third Edition (W3C Recommendation)
Also standardized asISO/IEC 15948
Maintained byW3C (PNG Working Group)
Licenseopen, royalty-free

PNG stores raster images without losing quality. It supports grayscale, RGB and palette images, 1 to 16 bits per channel and a full alpha channel for transparency. It’s the standard format for screenshots, UI graphics, icons, diagrams and any image with sharp edges or text. For photographs JPEG, WebP or AVIF give much smaller files.

Animated PNG (APNG) adds frames to a regular PNG file. Programs that don’t support APNG show the first frame. APNG is part of the PNG Third Edition specification and works in all current web browsers.

When to use PNG#

File structure#

A PNG file is an 8-byte signature followed by a sequence of chunks.

89 50 4E 47 0D 0A 1A 0A   signature
00 00 00 0D               chunk length: 13
49 48 44 52               chunk type: IHDR
00 00 01 00 00 00 00 C8   width 256, height 200
08 06 00 00 00            bit depth 8, color type 6 (RGBA), compression, filter, interlace
xx xx xx xx               CRC-32
...                       more chunks: IDAT (image data), IEND (end)

The signature bytes detect common transfer errors: the high-bit byte 0x89 catches 7-bit transfer, \r\n and \n catch line-ending conversion, 0x1A stops output of the file with type on DOS.

Chunks#

Each chunk has 4 parts. All integers in PNG are big-endian.

Field Size Description
Length 4 bytes size of the data field (not counting type and CRC)
Type 4 bytes ASCII letters, e.g. IHDR
Data Length bytes chunk contents
CRC 4 bytes CRC-32 of type and data

The case of the letters in the type carries meaning: an uppercase first letter means the chunk is critical (a decoder must understand it), lowercase means ancillary (can be skipped). A lowercase fourth letter means the chunk is safe to copy by editors that don’t understand it.

Critical chunks:

Chunk Purpose
IHDR header: width, height, bit depth, color type, compression method, filter method, interlace method. Must be first
PLTE palette of up to 256 RGB entries. Required for color type 3
IDAT compressed image data. Can be split across many consecutive IDAT chunks
IEND marks the end of the file. Empty

Common ancillary chunks:

Chunk Purpose
tRNS transparency: alpha for palette entries, or a single transparent color
gAMA, cHRM, sRGB, iCCP color space: gamma, chromaticities, sRGB intent, embedded ICC profile
cICP, mDCV, cLLI HDR and wide color gamut (Third Edition)
tEXt, zTXt, iTXt text: Latin-1, compressed and UTF-8 key/value pairs (e.g. Title, Author, Software)
eXIf Exif metadata
pHYs physical pixel size (DPI)
tIME last modification time
bKGD, sBIT, hIST, sPLT background color, significant bits, palette histogram, suggested palette
acTL, fcTL, fdAT APNG animation control, frame control and frame data

Color types and bit depths#

Color type Name Channels Allowed bit depths
0 grayscale 1 1, 2, 4, 8, 16
2 truecolor (RGB) 3 8, 16
3 indexed (palette) 1 1, 2, 4, 8
4 grayscale + alpha 2 8, 16
6 truecolor + alpha (RGBA) 4 8, 16

Width and height are 4-byte values from 1 to 231-1.

Compression#

Image data is compressed in 2 steps:

  1. Filtering: each scanline (row) gets a filter type byte, one of None, Sub, Up, Average or Paeth. A filter replaces each byte with the difference from a prediction based on the pixel to the left, above, or both. This makes the data more compressible. Encoders usually pick the filter per row.
  2. DEFLATE: the filtered rows are compressed with zlib (DEFLATE, the same as in ZIP and gzip) and the zlib stream is split into IDAT chunks.

Optional Adam7 interlacing stores the image in 7 passes so a browser can show a coarse preview before the whole file is downloaded. Interlaced files are usually larger.

Making PNG files smaller#

Specifications and documentation#

Software that opens PNG files#

SoftwareWindowsmacOSLinuxWebCanNotes
SumatraPDF✓viewfree, open source; also opens PNG inside archives and comic books
Web browsers✓✓✓✓viewChrome, Edge, Firefox, Safari; APNG animation too. Also on Android and iOS
Microsoft Photos✓view, editbuilt into Windows
Paint✓view, edit, createbuilt into Windows
macOS Preview✓view, edit, convertbuilt into macOS
GIMP✓✓✓view, edit, create, convertfree, open source
Krita✓✓✓view, edit, createfree, open source
Paint.NET✓view, edit, create, convertfree
Adobe Photoshop✓✓view, edit, create, convertcommercial
IrfanView✓view, convertfree for non-commercial use
XnView MP✓✓✓view, convertfree for private use
ImageMagick✓✓✓convert, createcommand line, open source
Photopea✓view, edit, create, convertfree, runs in the browser
Squoosh✓convertoptimize and convert in the browser
oxipng✓✓✓convertlossless PNG optimizer, command line
pngquant✓✓✓convertlossy palette compression, command line

Libraries#

Open-source libraries that read and write PNG. See all PNG libraries for a comparison.

C/C++libpng, libspng, LodePNG, stb_image
C#ImageSharp, Magick.NET, SkiaSharp
JavaImageIO, PNGJ
Goimage/png, imaging
PythonOpenCV, Pillow, pypng
RubyChunkyPNG, MiniMagick, ruby-vips
Rustimage, png
PHPGD, Imagick, Intervention Image