Skip to content

Instantly share code, notes, and snippets.

@siwalikm
Created September 15, 2024 23:20
Show Gist options
  • Save siwalikm/eb88977669c311bae70e005bdfecba97 to your computer and use it in GitHub Desktop.
Save siwalikm/eb88977669c311bae70e005bdfecba97 to your computer and use it in GitHub Desktop.
This script recursively converts all images in the current/nested directory to WebP format with a specified quality level using the cwebp tool
#!/bin/zsh
# you can install cwebp in mac with `brew install webp`
# Default quality value
quality=80
# Parse command-line options
while getopts "q:" opt; do
case $opt in
q) quality=$OPTARG ;;
*) echo "Usage: $0 [-q quality]"; exit 1 ;;
esac
done
# Define the image types to search for
image_types=("*.jpeg" "*.jpg" "*.tiff" "*.tif" "*.png")
# Iterate over each image type
for type in "${image_types[@]}"; do
# Find files of the specified image type
find . -type f -iname "$type" | while read -r IMAGE; do
# Get the filename without extension
filename_without_extension=${IMAGE%.*}
# Convert the image to WebP format
cwebp "$IMAGE" -o "${filename_without_extension}.webp" -q $quality
echo "Converted $IMAGE to ${filename_without_extension}.webp with quality $quality"
# rm -rf "$IMAGE";
done
done
echo "Conversion complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment