Created
March 5, 2024 00:17
-
-
Save M-Drummond/153490d829b5b553d3dbb38950cf5ccf to your computer and use it in GitHub Desktop.
Bulk Create WebP Images via the CLI
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 | |
# place this file in your images dir, run `bash images.sh` | |
# this present working dir | |
directory="." | |
# Loop through each file | |
for file in "$directory"/*; do | |
if [ -f "$file" ]; then | |
filename=$(basename -- "$file") | |
extension="${filename##*.}" | |
filename_noext="${filename%.*}" | |
# Check if the file is an image | |
if [[ $extension == "jpg" || $extension == "jpeg" || $extension == "png" || $extension == "gif" ]]; then | |
# Convert the image to WebP format | |
cwebp "$file" -o "${directory}/${filename_noext}.webp" | |
echo "Converted ${filename} to WebP" | |
else | |
echo "Skipping ${filename}, not an image file" | |
fi | |
fi | |
done | |
echo "Conversion complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment