Last active
April 30, 2024 14:03
-
-
Save tabrindle/ed9f77b4e96f4c98b49b 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
This file contains 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 | |
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 |
Nice script! In my case I have a folder with many subfolders. Therefore, this variant unfortunately does not help me. ;)
Hi
I use this code block on ZSH :
#!/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."
Please note that, you must not run this script on macOS root folder. Only run in needed folder. It finds all images at sub directiories and convert to web without removing original one. Save as "*.sh" and elevate it from terminal:
chmod +x name-of-shellfile
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey folks, I wanted to generate all my sizes in one go over a directory, so I took the idea further in this cwebp_r.sh file.
My bash scripting is poor so I'm sure there are many possible suggestions!