-
-
Save mstroeck/3363227 to your computer and use it in GitHub Desktop.
Shell script to recursively optimize all image in the current directory. WARNING: THIS OVERWRITES YOUR ORIGINALS AND REMOVES METADATA!
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 | |
set -o errexit | |
PNGS=`find . -iname "*.png"` | |
JPGS=`find . -iname "*.jpg"` | |
TMP1="_TMP1.PNG" | |
TMP2="_TMP2.PNG" | |
echo "Optimizing PNG" | |
for PNG in ${PNGS} | |
do | |
BEFORE=`stat -c %s ${PNG}` | |
echo -n " ${PNG}: ${BEFORE} " | |
cp ${PNG} ${TMP1} | |
COLORS=`pngtopnm ${PNG} | ppmhist -noheader | wc -l` | |
if [ "$COLORS" -lt 2 ]; then | |
COLORS=2 | |
fi | |
if [ "$COLORS" -lt 257 ]; then | |
cat ${PNG} | pngquant ${COLORS} > ${TMP1} | |
fi | |
pngcrush -q -l 9 -brute -rem alla ${TMP1} ${TMP2} | |
rm ${TMP1} | |
optipng -quiet -o7 -out ${TMP1} ${TMP2} | |
AFTER=`stat -c %s ${TMP1}` | |
if [ "$AFTER" -lt "$BEFORE" ]; then | |
mv ${TMP1} ${PNG} | |
echo "--> ${AFTER}" | |
else | |
echo "(Already optimal)" | |
fi | |
rm -f ${TMP1} ${TMP2} | |
done | |
echo "Optimizing JPG" | |
for JPG in ${JPGS} | |
do | |
BEFORE=`stat -c %s ${JPG}` | |
echo -n " ${JPG}: ${BEFORE} " | |
jpegtran -optimize -copy none ${JPG} > ${TMP1} | |
AFTER=`stat -c %s ${TMP1}` | |
if [ "$AFTER" -lt "$BEFORE" ]; then | |
mv ${TMP1} ${JPG} | |
echo "--> ${AFTER}" | |
else | |
echo "(Already optimal)" | |
fi | |
rm -f ${TMP1} | |
done |
I made an updated version of this with significant changes (including multi-threading) here:
https://github.com/mjc/optimizeimages.sh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this is great.
Error handling would be a helpful addtion. For example, this kills the program: