JPG vs AVIF: A Comparison

An unscientific analysis of these two image formats based on file size and image quality.

4 minute read

JPG and AVIF are the two main contenders in the battle, where JPG/JPEG are common image format we use daily in mobiles, laptops, cameras. It is mostly supported by all devices and platforms. And on the other hand AVIF the next generation image format. It is not supported by many social media platforms and browsers like Microsoft Edge and Opera Mini.

Methodology

I have selected four different test images to run through both the JPG and the AVIF encoders:

  • Flower - A normal image we take everyday using our smartphones.

    flower

    Taken from Redmi 9 Power

  • Lemur - A normal image we take everyday using our smartphones.

    lemur

    Taken from free-images.com. Untouched 13Mb image

  • Text - A screenshot image of terminal.

    text

    A text image with small range of colors

  • Cartoon - A cartoon image.

    cartoon

    A cartoon I designed by using Sketchbook

All images used in this post (source .jpg, encoded .avif)

To conduct the image encoding tests, the following system specifications were used:

  • Operating System: Linux (Arch Linux)
  • Kernel Version: 6.4.2
  • Processor: Ryzen 5 5500U (4 Ghz)
  • RAM: 8GB DDR4

Lossy compression

Lossy compression is a method of data compression that selectively discards or reduces some of the information from the original data in order to reduce file size. It is widely used in image compression algorithms to achieve smaller file sizes while maintaining acceptable image quality.

To encode the AVIF images I used avifenc provided by libavif

avifenc input.jpg output.avif --min 0 --max 63 -j 12

The --min and --max set the minimum and maximum quantizer for color. We use the full range here. The -j set the number of parallel threads. Here I am using 12 threads.

Flower

For this flower image I have cropped around the one of the flower. The crop is based on 200% zoomed image. Here are the results

File formatFile sizeCompression ratioTime taken to encode
JPG440Kb--
AVIF156Kb- 64.54%0.465s
jpgavif
OriginalAVIF
Image (200% zoom crop)

We compressed the original 440Kb image to 156Kb image which is 64% lesser than original. And the AVIF version is more smooth in edges than JPG. Most of the smart phones take these images and the difference is not noticeable.

Lemur

For this lemur image I have cropped around the eye. The crop is based on 200% zoomed image. Here are the results

File formatFile sizeCompression ratioTime taken to encode
JPG13Mb--
AVIF1.1Mb- 91.53%3.547s
jpgavif
OriginalAVIF
Image (200% zoom crop)

Comparing to other three images this lemur image is huge in size with resolution around 5K (4928x3264). We compressed the original 13Mb image into 1.1Mb image which is whopping 91.53% less than original image and There are some color artifacts in the AVIF format.

Text

For this image I have cropped around the text. The crop is based on 200% zoomed image. Here are the results

File formatFile sizeCompression ratioTime taken to encode
JPG392Kb--
AVIF40Kb- 89.79%3.54s
jpgavif
OriginalAVIF
Image (200% zoom crop)

This image is very simple with limited colors. This will give you an overview of text based images. We converted the original image 392Kb to 40Kb image. Which is again a huge compression of 89.79%. The edges of the text are slightly blurry compared to original image.

Cartoon

For this cartoon image I have cropped center of the image. The crop is based on 200% zoomed image. Here are the results

File formatFile sizeCompression ratioTime taken to encode
JPG864Kb--
AVIF136Kb- 84.25%0.887s
jpgavif
(Original)AVIF
Image (200% zoom crop)

The cartoon image is contain less colors and overfill colors in the edges. We converted the original 136Kb to 86Kb which is 84.25% lesser than original image. There are some blue color overfill in the edges of the original image which is not visible in AVIF format.

How to use AVIF in websites

You can use AVIF in websites using <picture> element. The <picture> element contains one <img> tag and one or more <source> tags. Put the normal JPG/PNG images in the <img> tag and add AVIF images to <source> tag. The browser first looks for the <source> tag, if the image format is supported by the browser it loads the image. If not supported by the browser then the <img> tag image loads.

You can verify which format of the file loaded by

  1. Right clicking the page and select the inspect option.
  2. Switch to the network tab and reload the page.
  3. You can find the image name with extension .AVIF
google chrome

Conclusion

According to the results presented above, my option is that AVIF is better compared to JPG in lossy compression. For haven’t tested for lossless compression because my original JPGs are compressed images. The main reason for this comparison is for website use.

© 2024 Jothi Prasath