Command Line (cli) Optimize/Compress PNG Image (zopflipng, pngcrush) - Ubuntu

June 14, 2019

Benchmark / Comparison

Package Compression Reduction
pngquant Lossy 58%
Zopfli Lossless 16%
Pngcrush Lossless 5%

NOTE: The optimization is run with default configuration on 4 png images of 1024x1024 with foreground text and background image.

NOTE: Kraken Lossy could compress better than pngquant default configuration, achieving 63% compression.

Available options

Zopfli

Install

git clone https://github.com/google/zopfli.git
cd zopfli
make zopflipng

Test

./zopflipng

Allow access command from anywhere (e.g. /usr/bin or ~/bin)

sudo ln -s /<INSTALL_DIR>/zopfli/zopflipng /usr/bin/zopflipng
zopflipng

Usage

zopflipng source.png output.png

NOTE: The process is quite slow, 10s for a 140Kb file.

NOTE: output.png is only generated if smaller.

Overwrite source file

zopflipng source.png output.png && [ -f output.png ] && mv output.png source.png

Proces all PNG files and append prefix to output files (output is zopfli_source.png)

zopflipng --prefix *.png

Replace original source files.

rename -f 's/(zopfli_)(.*)\.png/$2\.png/' *

Batch process all PNG files in directory.

find . -name '*.png' -print0 | xargs -0 -n1 -I{} sh -c 'zopflipng {} output.png; [ -f output.png ] && mv output.png {};'

NOTE: You can use zopfli with AdvanceCOMP / advdef.

pngquant

sudo apt-get install pngquant

or build from source

git clone --recursive https://github.com/kornelski/pngquant.git
cd pngquant
make
sudo ln -s /<INSTALL_DIR>/pngquant/pngquant /usr/bin/pngquant

Usage

pngquant source.png -o output.png

Overwrite source file

pngquant -f --ext .png source.png

Batch process all PNG files in directory.

pngquant -f --ext .png *.png

Pngcrush

Install

sudo apt-get install pngcrush

Usage

pngcrush source.png output.png

Overwrite source file

pngcrush -ow source.png

Process all PNG files and output to a directory (e.g. tmp, sadly cannot output and overwrite current directory)

pngcrush -d tmp *.png

Move files from tmp to replace original source files.

mv tmp/* .
rmdir tmp

Batch process all PNG files in directory.

find . -name '*.png' -print0 | xargs -0 -n1 pngcrush -ow

NOTE: http://manpages.ubuntu.com/manpages/xenial/man1/pngcrush.1.html

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.