Bash script for batch running OptiPNG

Optimize all your images with a single command:

$ find . -name '*.png' | xargs optipng -nc -nb -o7 -full

What is OptiPNG?

OptiPNG is a PNG optimizer tool. If you want your website to be faster, you should consider optimizing images.

When I ran the Google PageSpeed tool towards my website, I got a score of 81/100 for Desktop, 61/100 for Mobile. Pretty low, I thought. So I decided to go through the “Consider Fixing” issues, and the first one was - guess what? - optimize images.

Properly formatting and compressing images can save many bytes of data.

OPTIMIZE ALL THE IMAGES!The thing I did immediately next was reading the article that Google suggested: it’s a nice reading on Google Developers about optimizing images. The first recommendation is: Use an image compressor. OptiPNG is one of them.

On the OptiPNG site I realized it works only for one image at a time, so I had the need to write a script that runs the optimizer for all images at the same time.

Luckily enough, after a bit of search, I found a blog post titled Optimize all PNG images recursively, which finally solved my problem. All the credits belong to that blog.

After running the script I got a score of 86/100 for Desktop (before was 81), and 68/100 for Mobile (before was 61), all without loss of quality. Definitely worth a try!