How to Convert JPG to AVIF Format in ImageMagick on Mac

ImageMagick is a very useful suite of tools for working with image files. While its command-line interface can be a bit intimidating at first, there are some real benefits to using it as part of a photography workflow.

Here’s how to use it to convert images to AVIF for use on websites.

Screenshot of macOS Automator Folder Action for automatically converting image files to AVIF using ImageMagick
Text & Photos By David Coleman
Last Revised & Updated:

I MAY get commissions for purchases made through links in this post.

The AVIF format is one of a few emerging image file formats designed specifically for images on the web. They’re designed to be very small files that retain good image quality. And while their compatibility isn’t yet especially wide, it is growing quickly, and I’m finding myself using it more and more on my websites.

But a lot of image editing apps are yet to catch up and add AVIF compatibility. (Some have, like BatchPhoto, which I’ve covered before.)

If you need to convert image files to AVIF for your website, either individually or in bulk, here’s how to put the ImageMagick toolkit to work.

Why Use ImageMagick?

ImageMagick is a very powerful toolkit for working with images. That includes converting from one image format to another. It’s cross-platform, and it’s free.

It’s a command-line tool, so it doesn’t come with a user-friendly graphical user interface. 1 And while there plenty of other apps that can convert from one format to another that do have a GUI, there are some situations where ImageMagick really shines.

For one, I’ve found that many GUI image editing apps can choke when you try to feed them very large numbers of files. They typically try to pre-scan and load thumbnail previews of images, and that can lead to slowness at best and crashes at worst.

For another, you can combine ImageMagick with other workflow-friendly tools like macOS’s folder actions so that images are automatically converted when you drop files into them.

You also have incredibly fine-grained control over the encoding settings that ImageMagick uses to convert files and can load up the conversion process with other tasks to create one-step workflows.

Checking for Latest AVIF Support

I’m focusing here on using it on Mac, because that’s what I use. But the core functionality should be similar on other platforms.

First, to make this work, you’re going to need ImageMagick with AVIF support installed.

I assume here that you’ve got both Homebrew and the latest version of ImageMagick installed on your Mac. If you haven’t, I have a straightforward step-by-step guide to installing it and updating it.

How to Check that AVIF Support is Installed

Even if you have installed ImageMagick previously, it’s possible you have a pre-AVIF version. Or an older version of the AVIF library; it’s a new and evolving spec, after all. So to check whether you have the AVIF library installed and linked to ImageMagick, run this command:

magick identify -list format | grep AVIF

You should get a response like this:

How to Update ImageMagick

If the version of ImageMagick you have previously installed doesn’t show AVIF support, you can reinstall ImageMagick with the latest version. To make sure you’re getting the latest version, update Homebrew first.

So run this:

brew update

And then this:

brew upgrade imagemagick

Then re-run the AVIF check again.

Converting JPG to AVIF with ImageMagick

The most common usage for AVIF files is for sharing on the web. That’s what the format is specifically designed for. So the most common task if likely converting from JPG to AVIF. That’s what I’m going to focus on here, starting with the simplest version and adding complexity.

But if you want to be going the other way, or using different formats (eg. PNG to AVIF), you can use these as a template.

Simple JPG to AVIF Conversion of a Single File

This is the most basic version. It simply takes an individual JPG, creates an AVIF version, and saves the AVIF version alongside the original. The different file extensions is ImageMagick’s simple and elegant way of knowing it’s an image format conversion.

magick input.jpg output.avif

Replace: Change the input.jpg to the actual name of the existing file. Change output.avif to whatever you want the new file called.

Basic Batch Conversion of JPG to AVIF

This takes a batch of JPGs in the same folder and creates AVIF versions alongside them in the same folder using the original filenames.

for file in *.jpg; do magick "$file" "${file%.jpg}.avif"; done

Put Output Files in Their Own Subfolder

This script first ensures that the “AVIF” directory exists (creating it if necessary), and then it processes each JPG file, converting it to AVIF and saving it in the “AVIF” subfolder.

mkdir -p AVIF
for file in *.jpg; do magick "$file" "AVIF/${file%.jpg}.avif"; done

Controlling Compression

If you don’t explicitly specify compression settings when converting images to AVIF with ImageMagick, it will use its default settings. These defaults are set to provide a balance between compression and image quality, and may vary based on the version of ImageMagick and the AVIF library you have installed.

Adjusting the Amount of Compression

To adjust the AVIF compression in ImageMagick, you can use the -quality option. The quality value can range from 0 to 100, where 0 is the lowest quality and smallest file size, and 100 is the highest quality and largest file size.

Here’s an example:

magick input.jpg -quality 75 output.avif

Here, the quality is set to 75, which offers a compromise between file size and image fidelity.

The “right” setting is going to depend a lot on personal preference. Since there’s no preview function in ImageMagick, it’s going to be a matter of trial and effort to find the balance that best matches your preferences for image quality and file size.

Besides the quality setting, there are other options you might consider when working with AVIF in ImageMagick.

Encoding Speed

AVIF encoding can be adjusted for speed. Faster encoding usually results in larger file sizes, whereas slower encoding can achieve better compression.

magick input.jpg -define avif:speed=1 output.avif

The speed can typically range from 0 (slowest) to 10 (fastest), although the exact range may vary based on the version of the AVIF library used.

Color Profile

AVIF also supports wide-gamut color spaces and high dynamic range (HDR), which you can specify if needed using the -profile switch. Here’s an example that assigns the sRGB profile:

magick input.jpg -profile sRGB.icc output.avif 

While sRGB is the best option for displaying images on the web, most modern web browsers assume sRGB even if there’s no profile. And by removing a profile altogether, you can save a little file size. And since AVIF are really intended for display on the web, that’s a gain worth doing.

You can do this by using the +profile switch in combination with a wildcard that applies to all profiles. Here’s an example:

magick input.jpg +profile "*" output.avif

This will remove any color profile that’s attached to the image. It’s what I use routinely to squeeze out maximum file size savings. It might not save you a lot on each file, but when you scale up to hundreds or thousands of images on a website, the savings (and potential speed benefits) can be substantial.

Combining ImageMagick with macOS Watched Folder Actions for Automatic Conversion of JPG to AVIF

Now we get to where I think things get really interesting from a workflow standpoint. We can combine the power of ImageMagick with macOS’s Folder Actions feature.

What this means is that we can create a watched folder. Any JPGs (or PNGs or GIFs) that are dropped or copied into it are automatically converted to AVIF. For maximum efficiency, I have several such folders, each assigned a different type of output format (eg. AVIF, WebP, Metadata Stripped, etc).

There’s more than one way to set this up. In theory, you can save an AppleScript in /Library/Scripts/Folder Action Scripts/ or ~/Library/Scripts/Folder Action Scripts/ and then assign that script to a folder using the Folder Actions Setup in Services. While that works well for me in other contexts, including SIPS, I’ve had trouble when doing it that way with ImageMagick commands. It could be any number of permissions problems or sandboxing or something else.

Instead, I’ve found it to work much more reliably by setting it up through Automator. Here’s an example of what I use, assigning the action to a specific folder I have setup called AVIF.

A few things to note:

  • it’s created as a new Folder Action
  • Use Run Shell Script, not Run AppleScript
  • Pass input should be set to as arguments
  • Shell is set to /bin/zsh
  • This will take several common file formats as input. Most of the time, I’m converting JPG or PNG or maybe HEIC. It’s rare that I’d be converting WebP to AVIF—that usually doesn’t make much sense to do—but this script will nevertheless do it if you feed it WebP images (the output file potentially might come out larger, though).
Screenshot of macOS Automator Folder Action for automatically converting image files to AVIF using ImageMagick

There’s a bunch of different ways to customize this, of course, starting with adjusting the amount of compression. I use 60 as a starting point, which I find often works well, but you’ll want to run your own tests to find the amount that works well for you.

You could also make it work on recursive folders, preserve folder structure, rename the output files, put the output files in a specific folder, check that the output AVIFs are in fact smaller than the input JPGs, and so on. You can even add a popup asking you to specify the compression value.

But if you want to use it as a starting point, here’s the code you can copy and paste:

for f in "$@"
    # Extract the file extension and base name

    # Define the output file path with the .avif extension

    # Check for specific image file extensions
    if [[ "$ext" == "jpg" || "$ext" == "jpeg" || "$ext" == "bmp" || "$ext" == "gif" || "$ext" == "png" || "$ext" == "webp" || "$ext" == "heif" || "$ext" == "heic" ]]; then
        # Convert the image file to AVIF using ImageMagick with specified settings
        /usr/local/bin/magick convert "$f" -quality 60 -define avif:speed=1 -strip "$outfile"

        # Move the original image file to the trash using AppleScript
        osascript -e 'tell application "Finder" to delete POSIX file "'"$f"'"'

Here’s what this does:

  • Watches a designated folder for new image files.
  • Converts added image files to AVIF format if they are in any of the following formats: JPEG (.jpg, .jpeg); Bitmap (.bmp); GIF (.gif); PNG (.png); WebP (.webp); HEIF (.heif); HEIC (.heic)
  • Applies a quality setting of 60 for the AVIF compression.
  • Sets the AVIF encoding speed to 1, indicating a slower but more thorough compression process.
  • Strips out all color profiles and metadata from the original images.
  • Retains the original filenames but changes the file extension to .avif.
  • Moves the original image files to the trash, leaving only the newly created AVIF files in the folder.

I have other folders and corresponding actions set up for some other formats and tasks I need to support fairly often, such as WebP.

Other Recipes

One of the quirks of AVIF is that sometimes the AVIF version ends up larger than the original. Sometimes it’s by a lot. And that rather defeats the purpose of using AVIF.

However, it’s possible, by combining ImageMagic commands with bash script, to check the output AVIF against the input JPG and, if it’s larger, separate it or delete it. In this example, if any AVIF files end up larger than the input JPG, they’re separated into a subfolder called “Larger.” (The subfolder is only created if any match that criteria.)


# Directory where the converted AVIF files will be stored if they are smaller
mkdir -p "$avif_dir"

# Directory where the larger AVIF files will be moved

# Loop through all JPG files in the current directory
for jpg_file in *.jpg; do
  # Construct the AVIF filename

  # Convert the JPG to AVIF
  magick "$jpg_file" "$avif_file"

  # Get the file sizes of the original JPG and the new AVIF
  size_jpg=$(stat -f%z "$jpg_file")
  size_avif=$(stat -f%z "$avif_file")

  # Compare the file sizes
  if [ "$size_avif" -ge "$size_jpg" ]; then
    echo "The AVIF file is larger. Moving it to the 'Larger' directory."

    # Create the 'Larger' directory if it doesn't exist
    [ ! -d "$larger_dir" ] && mkdir "$larger_dir"

    # Move the AVIF to the 'Larger' directory
    mv "$avif_file" "${larger_dir}/"
    echo "The AVIF file is smaller. Conversion successful."

What Other Settings are Available?

I’ve focused here on some of the core encoding options like the quality setting and speed, because those are the ones that are most relevant most of the time in my own workflows. But there are other options you can set as part of the encoding. They include:

  • Lossless (-define avif:lossless=true): Enables lossless compression. When set to true, the image is compressed without any quality loss.
  • Chroma Subsampling (-define avif:chroma-subsampling=): Controls the chroma subsampling rate. Common values are 4:4:4 (no subsampling), 4:2:2, and 4:2:0. Less subsampling can result in higher quality and larger files.
  • Color Depth (-depth): Specifies the bit depth of the image. AVIF supports higher color depths than many formats, like 10-bit or 12-bit, allowing for a wider color range and finer gradations.
  • Pixel Format (-define avif:pixel-format=): Determines the pixel format to use, such as YUV420 or YUV444. Different pixel formats can affect compatibility and file size.
  • Compression (-compression): ImageMagick allows you to specify the compression type. For AVIF, you would use -compression AV1–the AV1 encoding is a defining feature of the AVIF format.
  1. There are some GUIs for ImageMagick that have been developed by third parties. Here is an example.[]
Profile photo of David Coleman | Have Camera Will Travel | Washington DC-based Professional Photographer

Text & Photos by David Coleman

I'm a professional photographer based in Washington DC. Seven continents, up mountains, underwater, and a bunch of places in between. I've been shooting for 30+ years, and my my photos and time-lapse videos have appeared in a bunch of different publications from major newspapers to magazines and books, billboards, TV shows, professional sports stadiums, museums, and even massive architectural scrims covering world-famous buildings while they're being renovated. You can see some of my travel photography here and here.

Leave a Comment