Skip to content

Instantly share code, notes, and snippets.

@pocc
Last active November 9, 2023 14:03
Show Gist options
  • Save pocc/908b3ecec33ae687372d8a25f8eca43c to your computer and use it in GitHub Desktop.
Save pocc/908b3ecec33ae687372d8a25f8eca43c to your computer and use it in GitHub Desktop.
Convert all png images to webp recursively
#!/usr/bin/env bash
# This script will get a list of all jpg and png files recursively
# and convert them all in place to webp files.
# cwebp options for max compression: `-m 6 -q 100 -z 9`
shopt -s globstar
for image in $PWD/**/*.png; do
echo "File is $image"
outfile=${image::-4}.webp
echo "outfile is $outfile"
cwebp "$image" -lossless -m 6 -q 100 -z 9 -o "$outfile"
echo "Deleting $image"
rm "$image"
done
# Recursively replace all instances of .png with .webp. This is dumb regex, so it may break things.
for file in $(find content layouts -type f); do
sed -E -i '' "s/\.png/\.webp/g" $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment