Compress PNG image file in Linux

PNG or Portable Network Graphics is a file format for image that employs lossless data compression. It is meant to replace patent encumbered GIF file format, hence the acronym itself is optionally recursive, which unofficially stands for PNG’s Not Gif.

Employing a lossless data compression, PNG’s images while being sharp can sometimes relatively be big in size. To keep the file size small while maintaining the sharpness of the image, there is a tool available that can further compress a PNG image, losslessly.

The command line based program is called Pngcrush, and is available for both Windows and Linux. The program can reduce the file size for up to 40% less from the original by trying various compression levels of PNG filter methods.

Running the program is as simple as supplying the input and output file, as in the following example;

$ pngcrush  input.png output.png

 | pngcrush 1.6.6
 |    Copyright (C) 1998-2002,2006-2008 Glenn Randers-Pehrson
 |    Copyright (C) 2005      Greg Roelofs
 | This is a free, open-source program.  Permission is irrevocably
 | granted to everyone to use this version of pngcrush without
 | payment of any fee.
 | Executable name is pngcrush
 | It was built with libpng version 1.2.27, and is
 | running with  libpng version 1.2.27 - April 29, 2008

 |    Copyright (C) 1998-2004,2006-2008 Glenn Randers-Pehrson,
 |    Copyright (C) 1996, 1997 Andreas Dilger,
 |    Copyright (C) 1995, Guy Eric Schalnat, Group 42 Inc.,
 | and zlib version 1.2.3.3, Copyright (C) 1998-2002 (or later),
 |    Jean-loup Gailly and Mark Adler.
 | It was compiled with gcc version 4.3.1 and gas version 2.18.50.20080610.

   Recompressing input.png
   Total length of data found in IDAT chunks    =    90188
unknown chunk handling done.
   IDAT length with method   1 (fm 0 zl 4 zs 0) =    94524
   IDAT length with method   2 (fm 1 zl 4 zs 0) =    94871
   IDAT length with method   3 (fm 5 zl 4 zs 1) =    93666
   IDAT length with method   9 (fm 5 zl 2 zs 2) =   181820
   IDAT length with method  10 (fm 5 zl 9 zs 1) =    88388
   Best pngcrush method = 10 (fm 5 zl 9 zs 1) for output.png
     (2.00% IDAT reduction)
     (2.14% filesize reduction)

   CPU time used = 0.510 seconds (decoding 0.060,
          encoding 0.450, other 0.000 seconds)

Pngcrush can also run in batch mode, where running the following command will compress all the PNG files in the current folder, and save it to a folder named compressed, adding the suffix -compr to the file name.

$ pngcrush -d compressed -e -compr.png *.png