Image Packer

Name: pack-images
Version: 1.0
Released: February 12th, 2011
Licence: Free to use without restriction.

Description

This script will compress all png files in a given directory. Must have optipng, pngcrush and advpng install (sudo apt-get install optipng pngcrush). Please note, on large files this process can take some time.

Important! This will OVERWRITE the original images, so don’t use it on the original directory.

Usage

Call with a optional directory name. If none given, will use current directory. Searches all subdirectories.

Code

#!/bin/bash
 
fileNameList=$(find $1 -name "*.png")
beforeSize=0
afterSize=0
 
for inputFile in $fileNameList; do
 
	echo "  o $inputFile"	
 
	# Create temp name for output (used by pngcrush)
	outputFile="$inputFile.old"
	beforeSize=$((beforeSize+$(stat -c%s "$inputFile")))
 
	pngcrush -q -rem gAMA -rem cHRM -rem iCCP -rem sRGB $inputFile $outputFile
	cp $outputFile $inputFile
	rm $outputFile
 
	optipng --quiet -zc1-9 -zm1-9 -zs0-3 -f0-5 -force $inputFile
	advpng --quiet --shrink-insane -z $inputFile
 
	afterSize=$((afterSize+$(stat -c%s "$inputFile")))
done
 
echo "Size Before : $beforeSize"
echo "Size After  : $afterSize"