-
-
Save janjiss/06a835d72e9b0e748b5ddc0316bcc48c to your computer and use it in GitHub Desktop.
Batch convert PNG files to WebP in 1 directory
This file contains hidden or 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/bash | |
PATH=/usr/local/bin:/usr/bin:/bin | |
# cd to the directory of the image so we can work with just filenames | |
dir="$(dirname "$1")" | |
cd "$dir" || exit 1 | |
base="$(basename "$1" .jpg)" | |
# create a WebP version of the PNG | |
cwebp -q 80 "$base".png -o "$base".webp | |
# delete the WebP file if it is equal size or larger than the original PNG | |
if [[ `stat -c '%s' "$base".webp` -ge `stat -c '%s' "$base".png` ]]; then | |
echo "Deleting WebP file that is no smaller than PNG" | |
rm -f "$base".webp | |
fi | |
# delete the WebP file if it is size 0 | |
if [[ -f "$base".webp && ! -s "$base".webp ]]; then | |
echo "Deleting empty WebP file" | |
rm -f "$base".webp | |
fi |
This file contains hidden or 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/bash | |
find . -regextype posix-egrep \ | |
-maxdepth 1 \ | |
-type f \ | |
-iregex '.*\.png$' \ | |
-exec test '{}'.webp -ot '{}' \; \ | |
-execdir "$(dirname $(readlink -f -- $0))"/convert-png-to-webp '{}' \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment