Compress PNG Images on Linux

Posted on In Linux

PNG images already use DEFLATE data compression algorithm involving a combination of LZ77 and Huffman coding. But the PNG images can be further compressed by removing non-important metadata or using lossy compression to save storage space and/or data transfer bandwidth. In this post, we introduce 2 compression ways with tools available on Linux.

Lossless compression using optipng

The quality of the image will be kept the same after lossless compression. The OptiPNG program attempts to optimize PNG files and reduce their size to a minimum without losing semantic information.

To install optipng on Ubuntu, run $ sudo apt install optipng.

To optimize a PNG file, run

$ optipng -o7 file.png

-o selects the optimization level. The higher the level, the more compression trials. OptiPNG optimizes the given file in-place.

To make the file further smaller, we can remove the metadata from the PNG file by specifying --strip all:

$ optipng --strip all -o7 file.png

To read more usage of optipng, check optipng manual.

Lossy compression using pngquant

pngquant(homepage, source code) does lossy compression of PNG images. It reduces file sizes significantly.

To install it on Ubuntu, run $ sudo apt install pngquant.

To compress a PNG file, run

$ pngquant --speed 1 file.png -o file-out.png

To read more usage of pngquant, check pngquant manual.

Leave a Reply

Your email address will not be published. Required fields are marked *