Skip to content

Instantly share code, notes, and snippets.

@chuckrincon
Forked from tabrindle/webp-convert-directory.sh
Last active December 6, 2023 00:10
Show Gist options
  • Save chuckrincon/130134201a9cc5defa3422411f5790fe to your computer and use it in GitHub Desktop.
Save chuckrincon/130134201a9cc5defa3422411f5790fe to your computer and use it in GitHub Desktop.
Convert all files in directory to webp, with default params, or standard cwebp params passed from command
#!/bin/zsh
# Define the image types to search for
extensions=("*.jpeg" "*.jpg" "*.tiff" "*.tif" "*.png" "*.ico")
# Iterate over each image type
for type in "${extensions[@]}"; 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 -short -progress "$IMAGE" -o "${filename_without_extension}.webp"
# echo "Converted $IMAGE to ${filename_without_extension}.webp"
rm -rf "$IMAGE";
done
done
echo "Conversion complete."
#!/bin/bash
PARAMS=('-m 6 -q 70 -mt -af -progress')
if [ $# -ne 0 ]; then
PARAMS=$@;
fi
cd $(pwd)
shopt -s nullglob nocaseglob extglob
for FILE in *.@(jpg|jpeg|tif|tiff|png); do
cwebp $PARAMS "$FILE" -o "${FILE%.*}".webp;
done
@chuckrincon
Copy link
Author

#!/bin/zsh

# 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"

    echo "Converted $IMAGE to ${filename_without_extension}.webp"

     # rm -rf "$IMAGE";
  done
done

echo "Conversion complete."
chmod +x name-of-shellfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment