Created
October 21, 2011 19:23
-
-
Save tessro/1304694 to your computer and use it in GitHub Desktop.
A badass command line PNG compressor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
## Packages: | |
# brew install pngcrush | |
# brew install optipng | |
# brew install advancecomp | |
# brew install https://raw.github.com/adamv/homebrew-alt/23f9084410476da1fcaf946f24f4dde47fe888ea/non-free/pngout.rb | |
function usage { | |
echo "Usage: $0 infile outfile" | |
exit 1 | |
} | |
if [ ! $# -eq 2 ]; then | |
usage | |
elif [ ! -f $1 ]; then | |
usage | |
fi | |
infile=$1 | |
outfile=$2 | |
insize=`stat -f %z $infile` | |
echo "Compressing $infile -> $outfile" | |
function progress { | |
local outsize=`stat -f %z $outfile` | |
local savings=`expr $insize - $outsize` | |
local pct=`expr 100 \* $savings / $insize` | |
echo "$insize -> $outsize ($pct%)" | |
} | |
if which -s pngcrush; then | |
# pngcrush sometimes emits libpng warnings | |
pngcrush -q -rem cHRM -rem gAMA -rem iCCP -rem sRGB -l9 -brute $infile $outfile 2>/dev/null | |
ran=1 | |
else | |
# make sure $outfile exists | |
cp $infile $outfile | |
fi | |
if which -s optipng; then | |
optipng -q -o7 $outfile | |
ran=1 | |
fi | |
if which -s advpng; then | |
advpng -q -z4 $outfile | |
ran=1 | |
fi | |
if which -s pngout; then | |
pngout -q -s0 -y $outfile | |
ran=1 | |
fi | |
if [ -z $ran ]; then | |
echo "Error: no compressors installed" >&2 | |
exit 1 | |
else | |
progress | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment