Loading...
X

AVIF image format: converting between format, AVIF lossless and lossy, how to view AVIF file properties in Linux

Contents

  1. AVIF image format: How to create AVIF lossless and lossy and convert to JPG
  2. Why AVIF is better
  3. Examples of AVIF files
  4. Utility for converting to AVIF format and from AVIF format to other formats
  5. How to convert an image to AVIF
  6. How to convert an image from AVIF format to JPG and any other format
  7. How to convert to AVIF without loss of quality – AVIF lossless
  8. How to check if AVIF is lossless or lossy
  9. How to view the properties of an AVIF file
  10. Easy way to convert AVIF without installing programs

1. AVIF image format: How to create AVIF lossless and lossy and convert to JPG

This article is about the AVIF image format: we will look at how to convert a photo from JPG (or any other format) to AVIF; how to vice versa convert AVIF format to JPG and other popular image formats; how to create AVIF compressed lossless and lossy; and how to view AVIF file properties.

2. Why AVIF is better

You may have noticed that some websites have switched to the AVIF format as their primary format for photos and images. What are the benefits of the AVIF format?

The AVIF format is the newest and most technically advanced photo format. AVIF files take up less space than JPG and even HEIC while maintaining the same quality.

AVIF will be the format that replaces HEIC (after it replaces JPG).

AVIF supports both high dynamic range (HDR) and standard dynamic range (SDR) content, including the widely used sRGB and BT.2020 color spaces. It supports 8-, 10-, and 12-bit color depths, film grain preservation, transparency (like PNG images), and animation (like GIF).

The new image format features higher image quality than JPEG, with a smaller file size, fewer compression artifacts, and less image blocking.

HEIC is widely supported by both consumer applications on devices (photo viewers on computers and phones) and online (all web browsers support AVIF, WordPress (with a plugin) and many websites support AVIF)

Also see: How to open AVIF files on Windows

3. Examples of AVIF files

Below are several AVIF files and their JPG counterparts – you can compare the quality and size of the files. To download the files in full size – click on them.

1) JPG file (size 4.3 megabytes):

AVIF file (size 836 kilobytes):

2) JPG file (size 3.9 megabytes):

AVIF file (size 875 kilobytes):

3) JPG file (size 3.7 megabytes):

AVIF file (size 563 kilobytes):

4) JPG file (size 4.6 megabytes):

AVIF file (size 1 megabyte):

As you can see, the size of the AVIF files is several times smaller than the size of the JPG files, while the quality of the photos is very high.

4. Utility for converting to AVIF format and from AVIF format to other formats

In this manual we will use ImageMagick, if you do not have this package installed yet, see the section “How to install ImageMagick on Linux”.

See also: ImageMagick guide: installing, using, and troubleshooting

5. How to convert an image to AVIF

To save any photo to AVIF, use the following command:

magick ORIGINAL_FILE NEW_FILE.avif

For example, I want to convert the file 20240122_124715.jpg to AVIF, then the command will be:

magick 20240122_124715.jpg 20240122_124715.avif

Note: the new file name can be anything, just make sure you specify the file extension correctly.

Note 2: You can convert to AVIF not only from JPG, but from any other image formats.

6. How to convert an image from AVIF format to JPG and any other format

To convert photos in AVIF format, use the following command:

magick ORIGINAL_FILE.avif NEW_FILE

Please note that the format of NEW_FILE will match the specified extension. For example, to convert to JPG, the command should be as follows:

magick ORIGINAL_FILE.avif NEW_FILE.jpg

For example:

magick 20240111_124943.avif beach.jpg

And to convert to WebP format, the command should be as follows:

magick ORIGINAL_FILE.avif NEW_FILE.webp

For example:

magick 20240111_124943.avif beach.webp

See also: What is a WebP file and how is it better than other formats

7. How to convert to AVIF without loss of quality – AVIF lossless

The AVIF format supports both lossy and lossless compression. Usually there is no point in converting photos from JPG (which only uses lossy compression) to AVIF lossless – AVIF lossy is enough for this since the quality of the photos is almost unchanged, and the image files themselves take up much less space.

In AVIF lossless, it makes more sense to convert photos from formats that also use lossless compression, such as PNG and TIFF. However, you still have two options, you can choose AVIF lossless to preserve all the data from the original image, or you can choose AVIF lossy to save disk space.

Apparently, the magick utility creates AVIF files only using lossy compression – even if you specify an image quality of 100. Therefore, to convert to AVIF lossless, we will use the avifdec utility, which is part of the libavif library.

To install libavif in Debian, Ubuntu, Linux Mint, Kali Linux and their derivatives, run the command:

sudo apt install libavif-bin

To install libavif in Arch Linux, Manjaro, BlackArch and their derivatives, run the command:

sudo pacman -S libavif

To convert images to AVIF lossless, we will use the avifenc utility.

Note: unlike the magick utility, which can convert any known image formats to AVIF, the avifenc utility can only work with JPEG, PNG and YUV4MPEG2 (Y4M) formats. Therefore, if you want to convert another format, such as TIFF, to AVIF lossless, you will probably have to convert it to PNG first.

To save an image to AVIF lossless, you need to use the avifenc utility with the -l option. For example, to convert test.png to AVIF lossless format and save the result to a file named lossless_test.avif, you would run the following command:

avifenc -l test.png lossless_test.avif

Example output (note the words “lossless” in the output):

Successfully loaded: test.png
AVIF to be written: (Lossless)
 * Resolution     : 1388x854
 * Bit Depth      : 8
 * Format         : YUV444
 * Alpha          : Not premultiplied
 * Range          : Full
 * Color Primaries: 1
 * Transfer Char. : 13
 * Matrix Coeffs. : 0
 * ICC Profile    : Absent
 * XMP Metadata   : Absent
 * Exif Metadata  : Absent
 * Transformations: None
 * Progressive    : Unavailable
Encoding with codec 'aom' speed [6], color quality [100 (Lossless)], alpha quality [100 (Lossless)], tileRowsLog2 [0], tileColsLog2 [0], 12 worker thread(s), please wait...
Encoded successfully.
 * Color total size: 2119968 bytes
 * Alpha total size: 0 bytes
Wrote AVIF: lossless_test.avif

You can also separately configure the level of lossy or lossless compression for color and alpha channels. For this, avifenc has the following options:

--min Q Set the minimum quantizer for color. Possible values ​​are in the range 0-63, where 0 is lossless.
--max Q Set the maximum quantizer for color. Possible values ​​are in the range 0-63, where 0 is lossless.
--minalpha Q Set the minimum quantizer for alpha. Possible values ​​are in the range 0-63, where 0 is lossless.
--maxalpha Q Set the maximum quantizer for alpha. Possible values ​​are in the range 0-63, where 0 is lossless.

Note that setting all options to 0 will not produce AVIF lossless (although colors and alpha will be losslessly compressed). That is, the following and the previous command (using the -l option) are NOT identical:

avifenc --min 0 --max 0 --minalpha 0 --maxalpha 0 20240122_124715.jpg lossless_20240122_124715.avif

Let's compare PNG and AVIF formats visually. You can compare the image quality using the following photos.

PNG source file (file size 2.8 megabytes):

AVIF lossless (size 2 megabytes)

AVIF lossy (353 kilobytes):

8. How to check if AVIF is lossless or lossy

With the AVIF lossless format, everything is quite complicated. First of all, we have already learned that magick converts files to AVIF lossy even when quality is set to 100 (recall that in ImageMagick, setting quality to 100 USUALLY means lossless conversion, but there are exceptions – one of them is the AVIF file).

In addition, the AVIF format has several settings that relate to lossy or lossless compression. That is, there is no way to look at the value of any one parameter and unambiguously answer whether a given AVIF file is lossless or lossy.

So, utilities for displaying information about AVIF files cannot unambiguously answer the question of whether a given AVIF file is compressed with losses or without losses.

However, you can tell whether an AVIF file is lossy or lossless compressed if you have the original image. To do this, use a command like this:

magick compare -verbose -metric mae FILE1 FILE2 out.png

For example, to compare files test.png and lossless_test.avif, you need to run the following command:

magick compare -verbose -metric mae test.png lossless_test.avif out.png

Example output:

test.png PNG 1388x854 1388x854+0+0 8-bit TrueColorAlpha sRGB 2.76841MiB 0.100u 0:00.063
lossless_test.avif AVIF 1388x854 1388x854+0+0 8-bit sRGB 2.02203MiB 0.320u 0:00.283
Image: test.png
  Channel distortion: MAE
    red: 0 (0)
    green: 0 (0)
    blue: 0 (0)
    alpha: 0 (0)
    all: 0 (0)
test.png=>out.png PNG 1388x854 8-bit sRGB 2.76841MiB 1.830u 0:01.434

If the file is losslessly compressed, then the Channel distortion values​​should be 0 in all lines.

Another example:

magick compare -verbose -metric mae test.png test.avif out.png

The following output is obtained:

test.png PNG 1388x854 1388x854+0+0 8-bit TrueColorAlpha sRGB 2.76841MiB 0.120u 0:00.074
test.avif AVIF 1388x854 1388x854+0+0 8-bit sRGB 361065B 0.110u 0:00.099
Image: test.png
  Channel distortion: MAE
    red: 847.746 (0.0129358)
    green: 803.248 (0.0122568)
    blue: 888 (0.01355)
    alpha: 0 (0)
    all: 634.749 (0.00968564)
test.png=>out.png PNG 1388x854 8-bit sRGB 2.76841MiB 1.930u 0:01.442

Since the Channel distortion values ​​are not equal to 0, this means that the file is compressed with lossy quality.

9. How to view the properties of an AVIF file

The libavif package contains the avifdec utility, which with the -i option displays some properties of an AVIF file, namely:

  • image size in pixels
  • color depth
  • color space
  • presence or absence of an alpha channel
  • presence or absence of various types of metadata
  • presence or absence of transformation
  • number of frames (for video files)

Example of using the avifdec utility:

avifdec -i 20240122_100051.avif

Example output:

Image decoded: 20240122_100051.avif
 * Resolution     : 4000x3000
 * Bit Depth      : 8
 * Format         : YUV420
 * Chroma Sam. Pos: 0
 * Alpha          : Absent
 * Range          : Full
 * Color Primaries: 1
 * Transfer Char. : 13
 * Matrix Coeffs. : 6
 * ICC Profile    : Absent
 * XMP Metadata   : Absent
 * Exif Metadata  : Present (57238 bytes)
 * Transformations: None
 * Progressive    : Unavailable
 * 1 timescales per second, 1.00 seconds (1 timescales), 1 frame
 * Frame:
   * Decoded frame [0] [pts 0.00 (0 timescales)] [duration 1.00 (1 timescales)] [4000x3000]

Actually, the main purpose of avifdec is to convert AVIF files to JPEG, PNG and YUV4MPEG2 (Y4M). So if you want, you can use this utility for AVIF conversion purposes (instead of magick).

10. Easy way to convert AVIF without installing programs

If you want to convert photos to AVIF format or you need to convert a picture from AVIF format or you want to view the properties of an AVIF file, but you do not want to install programs and deal with the command line, then you can use online services to perform all these functions:

Immediately after processing and sending you the results, your files are deleted from the server (both the original file and the one obtained as a result of conversion).

Using the services is extremely simple – you just need to specify the file you want to process.

Additional options are also available for those who need more – you can convert to lossless AVIF format or choose from a list of popular formats if you need to convert an AVIF file.


Leave Your Observation

Your email address will not be published. Required fields are marked *