How to compress png files on Linux

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.

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.

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

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.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

Leave a comment

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

Current ye@r *