OptiPNG: Optimize the Quality-Size of your PNG Pictures

OptiPNG is a small tool that allows you to convert on the fly (and in a batch) all your PNG files to a smaller file size. Amazingly, it doesn't loose any information.

Getting the OptiPNG tool

The ImageMagick suite should be inside your extra packages. It might not be installed by default depending on your distribution. If it is not the case, simply install it with the following command.

On a Debian base distribution, do the following.
# apt-get install optipng
On ArchLinux based distibution, do the following.
# pacman -S optipng
On RedHat based distribution, do the following.
# yum install optipng

How to use

Here are the most usefull flags I could found in the OptiPNG manuel. You may want to consult the help (optipng -h). Help will display the documentation in a compact view which is readable thant the man documentation.

-backup (or) -keep  - keep a backup of the original file
-dir                - specify the output directory
-preserve           - preserve file attributes (timestamp, file owner, etc.)
-out                - output file name
-o                  - how hard you want the tool to find the best compression (0-7) default : 2

Backing up the original file

First lets look at how to keep a backup of our file. The following code will copy the original file to example.png.bak and overight the example.png with the new compress image.

$ optipng -backup example.png

By default optipng will use the -o 2 option, it compressed the file by 12.37% which is not bad. Let see if we can increase it by using the -o 7 option. (remember our file is now called example.png.bak)

$ optipng -backup -o7 example.png.bak

Alright it seems the image could have been compressed a little more. This time we got 12.47%

How long should I wait?

But do I really need to use the -o 7 option? More you try to compress your image, more time it will take to OptiPNG to find the perfect compression attributes. The real question how many time can I sacrifice to compress all my files. I ran the previous example a second time, but this time I used the time function in Linux and here are the results

Note, I used the -preserve and the -out flags so you can see what happens. During the execution, I learned that you don't need to specify the -backup if you use the -out flag

$ optipng -preserve -o2 -out time-o2.png example.png

2.816 seconds for -o2, 12.37% of compression


$ optipng -preserve -o7 -out time-o7.png example.png

47.683 seconds for -o7, 12.47% of compression



In summary, it took 45 seconds to increase the compression by 1.535 KB on a 1.5 MB file. Now it is up to you to see if you really need that compression. I would say, if you are hosting your images on a website with a high volume traffic, it with be better to go the extra mile. Although, if you just want to send a picture to your mother, the extra compression won't really make a difference inside an email.

For those who are curious, the screenshots on this page were compress with the -o 7 flag with a compression rate around 75%. The images are only a few kilobytes so it only took around 2 seconds per images.

Sources

  1. Tecmint documentation
  2. OptiPNG project page

Links

Radio Channels

Electronic