How to compress PNG files on Linux

Last updated on November 19, 2020 by Dan Nanni

PNG (short for "Portable Network Graphics") is a raster image format designed to replace limitations of GIF image format in terms of data compression and color precision. The size of a PNG image file can vary significantly based on several factors including color depth, interlacing, precompression filter, compressor used, etc.

In this tutorial, I will describe how to compress a PNG image file losslessly on Linux. If you want to compress JPEG images, refer to this tutorial instead.

optipng is a Linux command-line utility that performs various optimizations on PNG image files, including size compression, metadata recovery and integrity checks, etc. Using optipng, you can compress PNG files losslessly.

Install optipng on Linux

To install optipng on Ubuntu or Debian:

$ sudo apt-get install optipng

To install optipng on CentOS or RHEL, first set up Repoforge repository on your system, and then run:

$ sudo yum install optipng

To install optipng on Fedora, simply run:

$ sudo yum install optipng

Compress PNG Image with optipng

Once you have installed optipng, you can compress a PNG image file as follows.

$ optipng -o 7 input.png
 optipng -o 7 input.png
 OptiPNG 0.6.4: Advanced PNG optimizer.
 Copyright (C) 2001-2010 Cosmin Truta.

 ** Processing: input.png
 1600x1200 pixels, 4x8 bits/pixel, RGB+alpha
 Reducing image to 3x8 bits/pixel, RGB
 Input IDAT size = 608523 bytes
 Input file size = 609539 bytes

 zc = 9  zm = 9  zs = 0  f = 0  IDAT size = 523671
 zc = 9  zm = 8  zs = 0  f = 0  IDAT size = 523639
 zc = 9  zm = 9  zs = 0  f = 4  IDAT size = 523579

 Selecting parameters:
 zc = 9  zm = 9  zs = 0  f = 4  IDAT size = 523579

 Output IDAT size = 523579 bytes (84944 bytes decrease)
 Output file size = 523707 bytes (85832 bytes = 14.08% decrease)

In the above, -o option specifies the optimization level (0 to 7). The higher the level is, the more trials optipng will run through, resulting in a smaller image.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean