-
-
Save mjc/434d3b5c72fe989563c1f491b973ab41 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 | |
# @TODO detect fd and fallback to find | |
pngs=$(fd -e png .) | |
jpgs=$(fd -e jpg .) | |
#pngs=$(find . -iname "*.png") | |
#jpgs=$(find . -iname "*.jpg") | |
optimize_a_png() { | |
png=$1 | |
tmp1="${png}_tmp1.png" | |
tmp2="${png}_tmp2.png" | |
before=$(stat -c %s "${png}") | |
printf " %s: %s " "${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 | |
pngquant ${colors} < "${png}" > "${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}" | |
} | |
optimize_a_jpg() { | |
jpg=$1 | |
tmp1="${jpg}_tmp1.png" | |
tmp2="${jpg}_tmp2.png" | |
before=$(stat -c %s "${jpg}") | |
printf " %s: %s " "${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}" | |
} | |
echo "Optimizing PNG" | |
for png in ${pngs} | |
do | |
optimize_a_png "$png" | |
done | |
echo "Optimizing JPG" | |
for jpg in ${jpgs} | |
do | |
optimize_a_jpg "$jpg" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment