imagemagickavif

Convert png images to lossy avif


I try to compress my ~1MB pngs to get a smaller image.

When I compress my images to jpeg with:

for i in card*.png ; do convert -resize 445x625 -background white -flatten "$i" ../medium/"${i%.*}.jpg" ; done

they end up about 100kb

So I tried

for i in card*.png ; do echo $i; convert -resize 445x625 "$i" ../medium/"${i%.*}.avif" ; done 

which results in avif images ~400kb, I guess because they are losslessly compressed.

How do I create lossy compressed avif images? And what would be a useful quality level to get images with the text still clearly readeable?

(I use ImageMagick 6.9.10-23 on Ubuntu)


Solution

  • libheif

    You can install

    apt install libheif-examples
    

    and then use heif-enc to create heif files with:

    for i in card*.png ; do echo $i; heif-enc "$i" -o "${i%.*}.avif" ; done
    

    If you need avif format, you need to compile the latest version of libheif, which has the -A option to create avif files. (There is a section in the README there how to build it on your system. But at least on Ubuntu this is not leading to a running heif-enc)


    Better use avifenc:

    # Install Homebrew
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    sudo apt install zsh # needed for this pspecific package install
    brew install joedrago/repo/avifenc
    

    Now you have the tool to create avif files with the syntax:

    avifenc [options] input.[jpg|jpeg|png|y4m] output.avif
    

    (use avifenc --speed 4 --min 20 --max 22 -j 8 to get a higher compression and use all 8 cores)