My previous post introduces some tools to optimize JPEG images. The focus of this post is on optimizing PNG images. Two complimentary tools will be presented: optipng, and pngquant. The former, lossless, and the latter, lossy.
optipng optimizes a PNG file by compressing it losslessly.
The command to install optipng on Debian/Ubuntu is:
$ sudo apt-get install optipng
For Fedora/Centos/RedHat, execute:
$ sudo yum install optipng
To optimize a PNG file named input.png:
$ optipng -o7 -strip all -out out.png -clobber input.png
- Output PNG file.
By default, optipng compresses the PNG file in-place, hence overwriting the original file. To write the output to a different file, use the -out option to specify a new output file. If the specified output file already exists, the -clobber option allows it to be overwritten. The -clobber is useful if you are running the command more than once.
Alternatively, replace -out out.png with the -backup option. As a result, optipng first backs up the original input file before compressing the input file in-place.
- Meta data.
The -strip all option removes all meta data from the image.
- Optimization level.
The -o option specifies the optimization level, which ranges from 0 to 7. Level 7 offers the highest compression, but also takes the longest time to complete. It has been reported that there is a marginal return of improved compression as you increase the optimization level. The results obtained from my own 1-image test confirm that. The tests show that the default optimization level of 2 is pretty good, and that higher levels do not offer a big increase in compression.
Optimization level Compression time
% Reduction Original N/A 285,420 N/A 0 0.03 285,012 0.14 1 3.07 242,548 15.02 2 5.77 242,548 15.02 3 10.33 242,175 15.15 4 17.54 241,645 15.34 5 34.61 241,258 15.47 6 35.86 241,645 15.34 7 71.37 241,258 15.47
pngquant uses lossy compression techniques to reduce the size of a PNG image. It converts a 32-bit PNG image to a 8-bit paletted image. More specifically, instead of storing each pixel as a 4-channel, 32-bit RGBA value, each pixel is stored as an 8-bit reference for mapping to a color in a palette. This 8-bit color palette is embedded in the image, and is capable of defining 256 unique colors. The trick then becomes how to reduce the total number of colors in an image without sacrificing too much perceivable quality.
To install pngquant on Debian/Ubuntu:
$ sudo apt-get install pngquant
Note that the pngquant version shipped on Debian Wheezy is obsolete (1.0), and not recommended by the official pngquant web site. The examples below were run on version 2.0.0.
To install pngquant on Fedora/Centos/Redhat:
$ sudo yum install pngquant
To optimize a PNG image:
$ pngquant -o output.png --force --quality=70-80 input.png
- Specify the output image file name using the -o option. Without it, the default output name is the same as the input except that the extension is changed (for example, input-fs8.png).
- Without the --force option, pngquant will not overwrite the output file if it already exists.
- Since the introduction of the --quality=min-max option in version 1.8, the number of colors is automatically derived based on the specified min and max quality values. The min and max values range from 0 to 100, 100 being the highest quality.
pngquant uses only the least number of colors required to meet or exceed the max quality level (80 in the above example). If it cannot achieve even the min quality value (70), the output image is not saved.
Below summarizes the results of optimizing one randomly chosen PNG image. It is not intended to be scientific or conclusive. Rather, I hope to give you an idea of the scale of reduction that is possible.
| File Size |
The 2 programs - optipng and pngquant - are not mutually exclusive. You will get the most compression from running pngquant. But if you want to get the last possible 1% or so compression, you may first run pngquant, then optipng.
$ pngquant -o lossy.png --force --quality=70-80 input.png $ optipng -o7 -strip all -out output.png lossy.png