Running PNGCrush on a recursive directories

One easy way of reducing website load time is by optimizing your images, which is what PNGCrush does. PNG graphics are often more bloated than they need to be so using PNGCRUSH should be obvious.

Basic usage flags don’t allow for recursive directories so needs to be put inside a loop.

-brute

crushes the image with all ~114 possible methods and picks the smallest.

-reduce

does lossless colour type or bit depth reduction. Eg, if there are only 5 unique colours, there’s no need for a colour palette of 256.

Running script without parameters will do all files files from current working directory, or first param can be location. Example usage:

$ recursivepngcrush .
$ recursivepngcrush ~/images/folder/
#!/bin/sh
FILTER=${1:-"."};
TFILE="temp.png";
for PNG in `find $FILTER -name "*.png"`;
do
    echo "Crushing $PNG"
    pngcrush -brute -reduce "$PNG" $TFILE
    mv -f $TFILE $PNG
done;

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in bash, compression, linux Tagged with: , , ,
2 comments on “Running PNGCrush on a recursive directories
  1. Tonci says:

    Thanks! You’re missing a period in `find . $1 -name “*.png”` :)

    • Wulf Solter says:

      Ah! I’d been using it with first param being a location, but have tweaked it now so running without params does $CWD :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*