LinuxCommandLibrary

zopflipng

Optimize PNG images for size

TLDR

Optimize a PNG

$ zopflipng [input.png] [output.png]
copy

Optimize several PNGs and save with given prefix
$ zopflipng --prefix=[prefix] [image1.png image2.png image3.png ...]
copy

SYNOPSIS

zopflipng [OPTIONS]... INFILE [OUTFILE]

PARAMETERS

-y
    Overwrite the output file if it already exists without prompting. If OUTFILE is not specified, INFILE will be overwritten by default.

-m
    Do multiple iterations, which leads to better compression but is much slower. This flag sets a default number of iterations (e.g., 15) which can be overridden by -i.

-i
    Specify the number of iterations to try. A higher number results in smaller files but significantly increases processing time. Default is usually 15.

-p
    Apply specific PNG filter strategies (0-4) to test for optimal compression. Default is to try all filters.

--lossy_transparent
    Apply a lossy compression to transparent pixels. This can further reduce file size but introduces lossy changes to the alpha channel.

--strip
    Strip all unnecessary chunks (metadata like tEXt, zTXt, iTXt, gAMA, cHRM, sRGB, iCCP, tIME, etc.) from the PNG file, further reducing size.

--keepchunks=
    Specify a comma-separated list of chunk names to keep, even if --strip is used.

--keeptime
    Keep the original modification time of the input file in the output file.

--zlib
    Use standard zlib compression instead of Zopfli. This results in faster compression but larger files. Useful for comparison or specific needs.

DESCRIPTION

zopflipng is a command-line utility designed to losslessly recompress PNG image files to achieve the smallest possible file size. It leverages the Zopfli deflate algorithm, a more exhaustive and slower compression method compared to standard zlib, but one that consistently produces smaller compressed data.

Unlike some other PNG optimizers that might reduce color depth or remove metadata in a lossy way, zopflipng primarily focuses on optimizing the PNG's IDAT (image data) chunk through a more thorough byte-level search for optimal deflate streams. This process typically results in file size reductions of 5-15% over PNGs compressed with standard methods, making it highly valuable for web assets where every kilobyte counts for faster load times.

While highly effective at reducing file size without visual quality loss, the exhaustive nature of the Zopfli algorithm means that zopflipng is significantly slower than other compression tools. It offers options to control the optimization intensity, strip metadata, and even apply a controlled lossy compression for transparent pixels if explicitly desired.

CAVEATS

Performance Overhead: zopflipng is computationally intensive and very slow due to the exhaustive nature of the Zopfli algorithm. It is best suited for pre-processing assets rather than real-time compression.
Memory Usage: For very large images, memory consumption can be significant.
Marginal Gains: On already highly optimized PNGs or very simple images, the file size reduction might be negligible despite the long processing time.
Lossy Option: Be cautious with the --lossy_transparent option, as it is the only feature that introduces lossy changes to the image data (specifically the alpha channel).

COMPRESSION PRINCIPLE

The Zopfli algorithm, unlike standard zlib, performs an exhaustive search for the optimal deflate compression, trying out many more possibilities to find a shorter byte sequence. This 'brute-force' approach guarantees a smaller output size, often pushing the limits of what deflate can achieve, but at a computational cost that makes it orders of magnitude slower than conventional methods. It's ideal for 'compress once, serve many' scenarios.

TYPICAL USE CASES

zopflipng is commonly used in web development workflows to optimize image assets for websites and applications, ensuring faster page load times and reduced bandwidth consumption. It's also useful for reducing the size of game assets, mobile application resources, or any scenario where preserving image quality while minimizing file size is critical.

HISTORY

The Zopfli compression algorithm was developed by Lode Vandevenne and Jyrki Alakuijala at Google and open-sourced in 2013. Its primary goal was to create a compressor that achieves the highest possible compression ratio for deflate-compatible data, even at the cost of vastly increased computational time. zopflipng is the dedicated command-line tool that applies this powerful algorithm specifically to PNG image files, making it a valuable utility for web developers and designers focused on optimizing image delivery for performance.

SEE ALSO

optipng(1), pngcrush(1), convert(1)

Copied to clipboard