Fish function for WebP
A Fish function to convert images to WebP
When creating this blog I like to convert my images to WebP, as they save a lot of space at little to no quality loss. For example, the cover image of this post is 87 KB in PNG format, but only 27 KB in WebP format. That’s a 69% reduction. Nice.
I use the
cwebp command line tool to convert my images, but
there’s something I really don’t like about it. It always requires you to explicitly specify the output file name.
This makes it so you can’t just do something like
cwebp *.png, as it will just dump the output to
stdout. Not very
As I’m using the
fish shell I decided to write a function to make this a little easier:
function convert-to-webp --description='Convert given files to webp format' for filename in $argv set -l output_filename (path change-extension .webp $filename) cwebp -m 6 -q 70 -mt -af -progress $filename -o $output_filename end end
I’m especially fond of
path change-extension .webp $filename. It’s a very readable way to change the extension of a
filename. The surrounding parentheses (used
for command substitution in
fish) are not
required, but do improve readability.
I’ll leave implementing this in
zsh as an exercise to the reader.
Note: It turns out this is not needed for this project, as my current Hugo setup automatically converts images to WebP.
The file name doesn’t matter here, as the function name determines how you call it, but it’s good practice to give them the same name. ↩︎