4 # Copyright (C) 2007 Arvid Norlander <anmaster AT berlios DOT de>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 # This script recompressess .png files using pngcrush and
22 # optipng to get the smallest images. All recompression is
25 # This script needs at least bash3, bash2 will not work
28 # * Make it work recursivly
30 if [ -z "$1" ] || [ "$1" == "--help" ]; then
31 echo "Usage: $(basename $0) files..."
32 echo -e '\e[1mNOTE: Files must be in same directory as the script is run from!\e[0m'
37 echo ' this does NOT work'
41 # Check that the tools we use exist:
42 if ! type pngcrush > /dev/null 2>&1; then
43 echo "Can't find pngcrush!"
44 echo "This script depends on the pngcrush tool to be in PATH."
45 echo "Please install it or, if it is already installed add the"
46 echo "directory it is in to PATH and try again."
49 if ! type optipng > /dev/null 2>&1; then
50 echo "Can't find optipng!"
51 echo "This script depends on the optipng tool to be in PATH."
52 echo "Please install it or, if it is already installed add the"
53 echo "directory it is in to PATH and try again."
57 TMPPATH="$$.png-recompress"
59 echo -e "Please wait, this can take a \e[1mlong\e[0m time."
61 echo -e "\n\n\n\e[1mPass 1: pngcrush\e[0m\n\n\n"
63 if [ -d "$image" ]; then continue; fi
65 dname=`dirname -- "$image"`
66 echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
67 newsize="$(pngcrush -reduce -brute -d "$TMPPATH" "$image" | grep -E "filesize reduction")"
69 if grep -q reduction <<< "$newsize"; then
70 cp -v "${TMPPATH}/$fname" "$dname/$fname"
72 rm -v "${TMPPATH}/$fname"
78 echo -e "\n\n\n\e[1mPass 2: optipng\e[0m\n\n\n"
80 if [ -d "$image" ]; then continue; fi
82 dname=`dirname -- "$image"`
83 echo -e "\e[1m$image\e[0m : $(du -b $image | awk '{print $1}')"
84 newsize="$(optipng -i 0 -o 7 -dir "$TMPPATH" "$image" | grep -E '^Output file size')"
86 if grep -q decrease <<< "$newsize"; then
87 cp -v "${TMPPATH}/$fname" "$dname/$fname"
89 rm -v "${TMPPATH}/$fname"