Last active
June 28, 2024 23:45
-
-
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
This file contains hidden or 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
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 |
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
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.
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.