Automatically convert WebP files to PNG

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

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

Adding the rule to Hazel

Now it’s time to open Hazel and add a rule:

A screenshot of the Hazel configuration screen with the configured 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:

A screenshot of the Hazel configuration screen with the script highlighted.

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

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!

By Joost de Valk

Joost is the Founder & Chief Product Officer at Yoast. He is married to Yoast's CEO Marieke, they have 4 kids together.

7 comments

  1. 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.

  2. to converto multiple files inside folder

    for i in *.webp; do dwebp "$i" -o "${i%.*}.png"; done

Leave a comment

You have to agree to the comment policy.