Skip to content

Instantly share code, notes, and snippets.

@M-Drummond
Created March 5, 2024 00:17
Show Gist options
  • Save M-Drummond/153490d829b5b553d3dbb38950cf5ccf to your computer and use it in GitHub Desktop.
Save M-Drummond/153490d829b5b553d3dbb38950cf5ccf to your computer and use it in GitHub Desktop.
Bulk Create WebP Images via the CLI
#!/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