Skip to content

Instantly share code, notes, and snippets.

@cjimmy
Last active June 26, 2023 13:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjimmy/e9849073cf65e9cf3ce8252f4dc27494 to your computer and use it in GitHub Desktop.
Save cjimmy/e9849073cf65e9cf3ce8252f4dc27494 to your computer and use it in GitHub Desktop.
Bash script to convert to images to webp, intended for Automator, right-click Service
source ~/.bash_profile
# assumes you have cwebp installed
# https://developers.google.com/speed/webp/download
# Typing `cwebp` into terminal should result in anything but Command not found.
# Gist by Jimmy Chion
# shell script intended to be used in Mac Automator,
# so you can right-click on a file and convert the file(s) to webp
# In Automator, create a new `Service`,
# drag and drop a `Run Shell Script` and copy paste this gist
# Set top settings to `Service receives selected [image files] in [Finder.app]`
# Shell: [/bin/bash] (You can type `echo $SHELL` in Terminal to see what shell you're running)
# Pass input [as arguments]
# Save as... "Convert to Webp" (or whatever you want it to be called in your right-click menu)
for FILE in "$@"
do
echo "coverting file: $FILE"
EXT=${FILE##*.} # file extension
QUALITY=85 # quality for the image
# convert the image using cwebp and output a file with the extension replaced as .webp
cwebp -q $QUALITY "$FILE" -o "${FILE/%.$EXT/.webp}" &>/dev/null
done
@crilleengvall
Copy link

Hello Jimmy,
Thanks for sharing this gist. I added an update to the script that makes it possible to add automator actions after the "Run shell script".

source ~/.bash_profile

for FILE in "$@"
do
	EXT=${FILE##*.} # file extension
	QUALITY=100 # quality for the image
	cwebp -q $QUALITY "$FILE" -o "${FILE/%.$EXT/.webp}" &>/dev/null
done

echo "$@"

@loukasstoltz
Copy link

loukasstoltz commented Dec 13, 2020

Capture d’écran 2020-12-13 à 21 32 43

Hi thank you for your work :
I am doing it like that but, when i run the script i have no the exit file (and no message ...)
I'm on macos big and my terminal open on szh ... :-s

i have created an empty .bash_profile

thank you kindly !

ps : this command work properly on terminal :
loukas@Air ~ % cwebp -q 80 /Users/loukas/Downloads/20140629_172650.JPG -o /Users/loukas/Downloads/20140629_172650.webp

@sentiMatteo
Copy link

sentiMatteo commented Apr 30, 2023

It works! Thank you for the script.

But whit two changes.

My shell use zsh, so i changed the first line in #! /bin/zsh and Shell: /bin/zsh.
Could don't not find cwebp, so I searched for the correct path, entered it instead of cwebp.

#! /bin/zsh

for FILE in "$@"
do
	echo "coverting file: $FILE"
	EXT=${FILE##*.} # file extension
	QUALITY=100 # quality for the image
	# convert the image using cwebp and output a file with the extension replaced as .webp
	/usr/local/bin/cwebp -q $QUALITY "$FILE" -o "${FILE/%.$EXT/.webp}" &>/dev/null
done

Now it works correctly.
Whit QUALITY=100, the quality of the image in WebP is very good, and the compression is better then online compressor or app like Webp Converter.

@myworkfloww
Copy link

myworkfloww commented Jun 26, 2023

It works! Thank you for the script cjimmy and sentimatteo

But with two changes.
Could don't not find cwebp, so I searched for the correct path, entered it instead of cwebp.

for FILE in "$@"
do
echo "coverting file: $FILE"
EXT=${FILE##*.} # file extension
QUALITY=100 # quality for the image
# convert the image using cwebp and output a file with the extension replaced as .webp
/usr/local/bin/cwebp -q $QUALITY "$FILE" -o "${FILE/%.$EXT/.webp}" &>/dev/null
done

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