Linux / Unix Command To Optimize and Compress PNG Files In Bulk

Categories:  Linux
Labels:  internet, bash script, optimization

I have lots of images in png format (over 250k+ png). Properly formatting and compressing png images can save many bytes of data. How do I compress and optimize png images on my amazon cloud account so that I can save bandwidth on cloudfront cdn account?

How do I use an image compressor to create lossless compression on PNG files, with no effect on image quality in bulk using Red Hat Enterprise Linux bash shell?

The following image format is recommended and used by big web sites such as Google / Yahoo / Amazon / Facebook etc:

  1. PNG file format is recommended for web.
  2. GIF file format is recommended for small size images.
  3. JPG file format is recommended for high resolution photographic-style images.
  4. Do not use BMPs or TIFFs.

Say hello to optipng

You need to use a tool called optipng. It is a PNG optimizer that recompresses image files to a smaller size, without losing any information. This program also converts external formats (BMP, GIF, PNM and TIFF) to optimized PNG, and performs PNG integrity checks and corrections. This tool can be installed on any server powered by Unix or Linux operating systems.


Any commands with a # at the start means “as root”.

Type the following yum command to install optipng:

# yum install optipng

On debian and derivatives type in terminal:

# apt-get install optipng

How do I use optipng command?

The syntax is:

optipng file
optipng [options] file
optipng [options] input.png

To display png image size, type, and compression info, enter:

$ pnginfo -t test.png 

Sample outputs:

test.png (tiffinfo compatible labels)...
  Image Width: 1164 Image Length: 911
  Bits/Sample: 8
  Samples/Pixel: 3
  Pixel Depth: 24
  Colour Type (Photometric Interpretation): RGB
  Image filter: Single row per byte filter
  Interlacing: No interlacing
  Compression Scheme: Deflate method 8, 32k window
  Resolution: 0, 0 (unit unknown)
  FillOrder: msb-to-lsb
  Byte Order: Network (Big Endian)
  Number of text strings: 1 of 9
    Software (xTXt deflate compressed): Shutter
Related Posts: