-
Star
(119)
You must be signed in to star a gist -
Fork
(23)
You must be signed in to fork a gist
-
-
Save tabrindle/ed9f77b4e96f4c98b49b to your computer and use it in GitHub Desktop.
#!/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 |
find -iname '*.jpg.webp' -print0 | xargs -0 -I{} rename '.jpg.webp' '.webp'
run "bash NAMEOFfile" and it runs well!
Very nice, thank you!
Uhmmm any clues on how to even produce multiple sizes of each image? Let's say 3 sizes with max width each one?
execute this from current directory
#!/bin/bash
PARAMS=('-m 6 -q 70 -mt -af -progress')
for D in `find . -type d`
do
# cd $D
echo $D
cd $D
for FILE in *.{jpg,jpeg,png,svg,tif,tiff}; do
[ -e "$FILE" ] || continue
# Here "$FILE" exists
cwebp $PARAMS "$FILE" -o "${FILE%.*}".webp;
done
cd ~
done
Thank you a lot!
👍
Thanks mate ✌️
I needed a recursive version, and wanted to use gnu parallel:
find . -type f | egrep '.jpeg|.jpg|.tiff|.tif|.png' | parallel --progress 'cwebp -quiet -af {} -o {.}.webp'
requires: webp
, parallel
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!
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
Thank you all for this, I added together your solutions into a recursive script.
#!/bin/bash
PARAMS=('-m 6 -q 50 -mt -af -progress')
for D in `find . -type d`
do
# cd $D
echo "Entered directory $D"
cd $D
# 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 $PARAMS "$IMAGE" -o "${filename_without_extension}.webp"
echo "Converted $IMAGE to ${filename_without_extension}.webp"
# rm -rf "$IMAGE";
done
done
done
echo "Conversion complete."
Save it to something like convert-webp.sh
, put it in a directory with images and subfolders, then run
sh convert-webp.sh
and watch the magic happen.
Compare original and webp versions, then marvel at up to 80-90% size reductions :D
Nice!
Nice!
Thank you all for this, I added together your solutions into a recursive script.
#!/bin/bash PARAMS=('-m 6 -q 50 -mt -af -progress') for D in `find . -type d` do # cd $D echo "Entered directory $D" cd $D # 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 $PARAMS "$IMAGE" -o "${filename_without_extension}.webp" echo "Converted $IMAGE to ${filename_without_extension}.webp" # rm -rf "$IMAGE"; done done done echo "Conversion complete."Save it to something like
convert-webp.sh
, put it in a directory with images and subfolders, then runsh convert-webp.shand watch the magic happen.
Compare original and webp versions, then marvel at up to 80-90% size reductions :D
Amazing! Thank you so much!🤝
Thank you ! saved my day
🤙