Skip to content

Instantly share code, notes, and snippets.

@spaze
Created June 21, 2022 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spaze/584c5e1b2b5b8b9f44084396e4bcd68f to your computer and use it in GitHub Desktop.
Save spaze/584c5e1b2b5b8b9f44084396e4bcd68f to your computer and use it in GitHub Desktop.
Optimize images in the current dir with Zopfli (for PNGs), Guetzli (for JPEGs), WebP (for both lossy and lossless conversions). Keeps the smaller file of those.
#!/bin/bash
# Optimize images for talk slides
# Run in directory with images, it does the following:
# 1. runs zopfli on all PNGs
# 2. runs guetzli on all JPEGs
# 3. creates lossy and lossless WebP, keeps the smaller one
# 4. keeps the WebP if smaller than original
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
echo "${GREEN}Running Zopfli on all PNGs${NORMAL}"
for I in *.png; do /opt/zopfli/zopflipng --iterations=50 --splitting=3 --lossy_8bit --lossy_transparent -y $I $I; done;
echo "${GREEN}Running Guetzli on all JPEGs${NORMAL}"
for I in *.jpg; do /opt/guetzli/guetzli $I $I; done;
for I in *.{png,jpg}; do
FILE_LOSSY=${I%.*}-lossy.webp
FILE_LOSSLESS=${I%.*}-lossless.webp
echo -e "\n* ${GREEN}Converting ${I} to lossy WebP${NORMAL}"
/opt/webp/bin/cwebp -m 6 -mt -noalpha $I -o $FILE_LOSSY;
echo "* ${GREEN}Converting ${I} to lossless WebP${NORMAL}"
/opt/webp/bin/cwebp -m 6 -mt -noalpha -lossless $I -o $FILE_LOSSLESS;
SIZE_ORIG=$(wc -c <"$I")
SIZE_LOSSY=$(wc -c <"$FILE_LOSSY")
SIZE_LOSSLESS=$(wc -c <"$FILE_LOSSLESS")
if [[ "$SIZE_LOSSLESS" -le "$SIZE_LOSSY" ]]; then
echo "* ${GREEN}Lossless WebP is smaller than lossy, deleting lossy${NORMAL}"
BEST_WEBP=$FILE_LOSSLESS
rm $FILE_LOSSY
else
echo "* ${GREEN}Lossy WebP is smaller than lossless, deleting lossless${NORMAL}"
BEST_WEBP=$FILE_LOSSY
rm $FILE_LOSSLESS
fi
SIZE_WEBP=$(wc -c <"$BEST_WEBP")
if [[ "$SIZE_ORIG" -le "$SIZE_WEBP" ]]; then
echo "* ${GREEN}Original ${I} is smaller than WebP, deleting WebP${NORMAL}"
rm $BEST_WEBP
else
echo "* ${GREEN}WebP is smaller than the original, keeping both WebP and the original${NORMAL}"
mv $BEST_WEBP ${I%.*}.webp
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment