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

Jun 14, 2019

Benchmark / Comparison

PackageCompressionReduction
pngquantLossy58%
ZopfliLossless16%
PngcrushLossless5%

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.gitcd zopflimake 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.gitcd pngquantmakesudo 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

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.