Days 32-34 / 100 Days of Code: Tackling .webp Image Files on macOS
Jacob Stern
Posted on August 4, 2024
Sat, August 3, 2024
Today I faced a different challenge: Codecademy promotes .webp image format for projects, but my MacBook didn’t recognize any graphics app as .webp capable. Using the first solution I found online with Homebrew to install the webp library resulted in extremely low-quality images. Not wanting to manually convert every image, I looked for an automated solution. I found a workaround using ImageMagick, a script that I adapted, and the mac Automator app to create Quick Actions.
Create a Quick Action to Convert .jpg or .png files to .webp:
- Install ImageMagick using Homebrew: brew install imagemagick
- Open Automator and select Quick Action
- In the sidebar, select Utilities, then Run shell scrip
- In the ‘Shell’ dropdown, select /bin/zsh
- In the ‘Pass input’ dropdown, select as arguments
- Replace the text box content with the script and save as Convert to webp
- Use the adapted zsh script from David Coleman’s with modern brew file path
- David suggests a quality setting of 70 or 80, which looks good and provides nice compression
- Now you can convert images to .webp in Finder from the Quick Actions menu
for f in "$@"
do
# Extract the file extension and base name
ext="${f##*.}"
filename="${f%.*}"
# Define the output file path with the .webp extension
outfile="${filename}.webp"
# Check for specific image file extensions
if [[ "$ext" == "jpg" || "$ext" == "jpeg" || "$ext" == "bmp" || "$ext" == "gif" || "$ext" == "avif" || "$ext" == "png" || "$ext" == "heif" || "$ext" == "heic" ]]; then
# Convert the image file to WebP using ImageMagick with specified settings
/opt/homebrew/bin/magick "$f" -quality 75 -strip "${f%.*}.webp"
Optionally, create a Quick Action to Open .webp Files in GIMP:
- Open Automator and select Quick Action
- In the ‘Workflow receives current’ dropdown, select image files
- In the ‘in’ dropdown, select Finder.app
- From the sidebar, select the Finder icon 'Files & Folders', then 'Open Finder items'
- Select Other, and choose GIMP
- Save the workflow as Open with GIMP
- Now, you can select .webp files in Finder and open them in GIMP from the Quick Actions menu
I’ve started on assessments but need to wrap up for today due to home commitments. I’ll follow up with results.
Posted on August 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.