I love CloudFlare. It makes websites lots faster. One of the things it does is change PNG files to WebP automatically when your browser (f.i. Google Chrome) supports it. This is awesome, as they’re smaller and it thus loads faster.
However, sometimes I need to download an image file and I end up with a .webp
file. My operating system can’t handle those. So I have two options: opening the image URL in Safari (which doesn’t support WebP yet) or to convert them to PNG.
I had this happen to me enough that I decided to find a better solution. I run a utility on my Mac called Hazel, by Noodlesoft. This utility helps me keep my Downloads and Trash folders clean, among other things, with rules I can define myself. I decided to add a new rule.
Prerequisites
To convert .webp
files to .png
files, you need Google’s WebP libraries. On your Mac, the easiest way to do that is with Brew:
brew install webp
Code language: Shell Session (shell)
Converting from WebP to PNG
The command to convert a WebP file to PNG would look like this:
/usr/local/bin/dwebp -o target.png input.webp
Code language: Shell Session (shell)
Adding the rule to Hazel
Now it’s time to open Hazel and add a rule:
Our rule is going to:
- Find files in the Downloads folder that end in
.webp
. - Run a script to convert those
.webp
files to PNG. - Move the
.webp
file to the trash. - Display a notification to show that it’s done all this.
All this is pretty easy to configure in Hazel. What you’ll need is the following embedded script:
The script is /bin/bash
as we need to strip the .webp
extension with some bash magic, and replace it with .png
:
/usr/local/bin/dwebp -o "${1%%.*}.png" $1
Code language: Shell Session (shell)
After setting up all the rules as explained above and copy pasting the script, just save the rule. You can save any WebP file to the Downloads folder (or whichever folder you decide to run this rule for) and it should be automatically converted to PNG!
I find this extremely helpful as well. Thank you so much for creating it
This is exactly what I was looking for, but I am getting an error when hazel runs – [Error] Shell script failed: Error processing shell script on file…hazelworker[14601] Shellscript exited with non-successful status code: 255
I’ve tried multiple ways to write the script for Hazel. I can convert the files in terminal, but I can’t get it to translate to Hazel to run on the found file.
I got it to work: dwebp “$1” -o “${1%%.*}.png”
Nice!
Is it at all possible to “re purpose” this to convert SVG to PNG using the same method? Or is that a more complex file conversion?
I think that should be very possible 🙂
to converto multiple files inside folder