How to use OptiPNG recursively

Few hours ago I wrote about image optimization with Jpegoptim. Now I'll write few words about Optipng, a PNG and GIF optimizer. 

OptiPNG is a PNG optimizer that re-compresses 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. If you wish to learn how PNG optimization works, or to know about other similar tools, see the PNG-Tech article A guide to PNG optimization.

The only drawback of this piece of software is that it saves optimized images only in PNG format, using the same filename. This means you have to replace all references to gif files with png ones (mostly in CSS stylesheets and template files). Another disadvantaje is that sometimes, small gif files are actually smaller in size than the optimized png's, so it's up to you which files you're going to replace.

To parse a whole structure of directories in Linux, you can use the following commands:

Find png files and optimize them:

find -type f -name "*.png" -exec optipng {} \;

Find gif files, optimize and convert them to png:

find -type f -name "*.gif" -exec optipng {} \;

You can use -o(1-7) parameter before brackets, but there's almost no difference between the default value (-o2) and -o7 (slow and hardware intensive).

If you know better tips on how to use OptiPNG please leave a comment below.

Rate this article: 

Average: 4.1 (6 votes)



You can use such parameters:
optipng -nc -nb -o7
The arguments -nc -nb tell optipng not to alter any colour information (or we risk changing the appearance of the optimized image) and -o7 tells it to use the best compression.

If you want compress faster with (little more filesize) than use lower -o value, for example -o5

Thanks for these great tips! Will definitely use them!



Samples of work done for me or my customers, for free or not, like websites, repairs, photography etc.
View all my recent work



I'm giving away some knowledge in my areas of expertise. Did you like something here? Share it please!
View all tutorials



Useful Linux commands or other code snippets I often use.
View my useful shortcuts



Few bookmarks I find useful. Do you want website here? Give me a good reason and I can help you.
View my favourite web resources

About me

Hi there! My name is Marius Cârneală.

About me?

Loving husband, dad of one two three, drone pilot, web designer and administrator, SEO specialist, IT support guy, electronic hobbyist and content writer. This list is open.