How to Batch Convert Images to WebP on Mac
As WebP grows in popularity as a compressed image format for the web, many web workers need to convert batches of images to WebP format from their Mac. Bulk converting images like JPG and PNG to WebP is not currently a native feature, but with a little tinkering on the Mac, you can gain functionality to easily batch convert any images to WebP on the Mac.
This article is expanding on this post about converting images to webp using the Mac command line. We’ll be using the same webp toolkit from Google that is installed through HomeBrew, so you’ll want to install HomeBrew first, then install the webp tools from Google via Homebrew so that you have cwebp available as a command line utility on your Mac. But you won’t need to use the command line beyond the initial installation, because we’ll be using Automator to create a Finder Quick Action that performs the image compression and conversions to webp.
How to Batch Convert Images to WebP on Mac with a Quick Action
We’re going to create a Quick Action with Automator that uses the command line cwebp tool to convert image files to webp format. The result is you will have an contextual menu option to ‘Convert to WebP’ in the Finder, that you can use with any image file or collection of image files on the Mac.
- Install HomeBrew and cwebp if you have not done so already (directions to install Homebrew are here, and to install cwebp are here)
- Open Automator on the Mac
- Create a new “Quick Action”
- At the top of the Workflow, where it says “Workflow receives current” choose “image files”, and under “in” select “Finder”
- Next, look at the left side panel of Automator and choose “Utilities” under Library, then drag and drop “Run Shell Script” to the right workflow panel
- For ‘Run Shell Script’, look for ‘Pass input’ and select “as arguments”
- Now enter the following code snippet exactly*:
- Save your Quick Action from File > Save, name it something like “Convert to WebP” (whatever you name the Quick Action is what will appear in the Finder contextual menu)
- Go to the Finder on the Mac, and locate any image file or collection of image files, right-click, and select “Convert to WebP”
for FILE in "$@"
do
/opt/homebrew/bin/cwebp -q 85 "$FILE" -o "${FILE%.*}.webp"
done
Your Quick Action workflow should now look like this:
The converted webp files will appear in the same directory with the same file names except carrying the webp extension.
With your newly created Convert to WebP Quick Action workflow, you can convert a single image file to WebP, or if you select multiple image files you can batch convert them all to WebP files.
* Some notes about this little script that you may want to change for your particular needs:
- The path to cwebp is set for cwebp installed through Homebrew, if you installed cwebp manually then it’s probably in /usr/bin instead and you’d want to adjust the path accordingly
- The -q quality flag is currently set to 85 but you can set it to anything between 1-100 (higher will produce higher quality images, but less compression)
Again, you can use this for a single webp file conversion, or a batch conversion of many image files, to webp format.
With how popular webp is becoming, and given that MacOS currently offers native capabilities to convert WebP to JPG and open WebP image files and save the as JPEG through Preview (and batch converting webp to jpeg through that!), it seems reasonable to expect that a future version of MacOS will support the opposite direction as well; saving to WebP, and direct conversion to WebP. If and when that does occur, this particular Quick Action will no longer be needed, nor will the manual installation of cwebp from HomeBrew, but for the time being this is necessary.
For the unfamiliar, WebP is an image file format created by Google that is intended to be efficient and broadly capable, supporting still images and animations, and both lossy and lossless. The cwebp tool we use here for our purposes of batch converting images to webp is the official WebP encoder from Google, which you can read more about here if interested. The shell script we use to perform the backend conversion and compression is a modified version of what was created by Jay Holtslander from here. And finally, Homebrew is a very popular command line package manager for Mac that easily allows Mac users to install thousands of command line tools and utilities, including cwebp which we use here, without having to compile them yourself.
If you have another preferred method of batch converting image files to webp on the Mac, or an alternate approach, then share it with us in the comments below!
Thanks for this. I was just wondering if there is any way to convert this into a Folder Action instead so that any time an image is added to a certain folder then this action runs and converts the image just added to a webp format?
Many thanks,
Mark
XnConvert is free in the App Store or on the website and can convert to and from WebP
https://www.xnview.com/en/xnconvert/
https://apps.apple.com/app/xnconvert/id436203431