Skip to content

Instantly share code, notes, and snippets.

@tabrindle
Last active April 3, 2024 16:04
Show Gist options
  • Save tabrindle/ed9f77b4e96f4c98b49b to your computer and use it in GitHub Desktop.
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
#!/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
@Gyarahaddo
Copy link

As a guy who lazy to write script for recursive work, you helped me a lot. Thanks!

@tranlehaiquan
Copy link

Thank you 👍

@rodrigo-gpereira
Copy link

Great Job. Thanks

@jaygarcia
Copy link

Thanks for sharing this. I love reading modern BASH code. Helps me realize how outdated some of my methods are :).

@stefanedberg
Copy link

Thanks! 💯

@superthin
Copy link

Good jobs. Thank you so much!

@dhurba87
Copy link

Great job

@Daijobou
Copy link

Nice script! In my case I have a folder with many subfolders. Therefore, this variant unfortunately does not help me. ;)

@ihormak
Copy link

ihormak commented Jun 24, 2021

thanks, nice job

@abcfy2
Copy link

abcfy2 commented Sep 19, 2021

find -type f \( -iname '*.jpg' -o -iname '*.png' \) -print0 | xargs -0 -I{} -r -n1 -P10 cwebp -z 9 -m 6 -mt -pass 10 -alpha_filter best -short -progress "{}" -o "{}.webp"

Optional: rename .jpg.webp and .png.webp to .webp

find -iname '*.jpg.webp' -print0 | xargs -0 -I{} rename '.jpg.webp' '.webp'
find -iname '*.png.webp' -print0 | xargs -0 -I{} rename '.png.webp' '.webp'

@paulcushing
Copy link

Nice! This made my life easier. Much appreciated!

One revision I had to make was this: (because one of my directory names had a space in it)
Line #9 cd "$(pwd)"

Thanks!

@ohot2015
Copy link

ohot2015 commented Mar 1, 2022

sh convert.sh
convert.sh: 3: Syntax error: "(" unexpected

@tmjns
Copy link

tmjns commented Mar 2, 2022

❤️

@Nosferatu31
Copy link

🤙

@LuizAntonioJR-RJ
Copy link

LuizAntonioJR-RJ commented Oct 5, 2022

find -iname '*.jpg.webp' -print0 | xargs -0 -I{} rename '.jpg.webp' '.webp'

run "bash NAMEOFfile" and it runs well!

@enricogallesio
Copy link

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?

@smakintel
Copy link

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

@kenu
Copy link

kenu commented Feb 14, 2023

Thank you a lot!
👍

@lepamoore
Copy link

Thanks mate ✌️

@aw-west
Copy link

aw-west commented May 9, 2023

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

@licryle
Copy link

licryle commented Oct 3, 2023

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!

@berkbb
Copy link

berkbb commented Nov 21, 2023

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