Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert JPG images to AVIF with Python

Python's Pillow doesn't seems to support AVIF files yet, and other packages like pyheif, only support reading. Is there any Python tool to convert jpg images to the new avif format?

like image 845
Ander Avatar asked Oct 14 '21 06:10

Ander


People also ask

How to convert an image into JPG format in Python?

Let us see how to convert an image into jpg format in Python. The size of png is larger when compared to jpg format. We also know that some applications might ask for images of smaller sizes. Hence conversion from png (larger ) to jpg (smaller) is needed. For this task we will be using the Image.convert () method of the Pillow module.

What is avif and avif JPG?

JPG format based on the 24-bit color palette, the higher the level of compression applied to create the file JPG, the greater the decompression effect on image quality. AVIF is the youngest, but most efficient and advanced codec for image compression.

How to convert JPG to GIF in AutoCAD?

Step 1: Import the library. In function jpg_to_gif () we first check whether The Selecting the image is in the same format (.jpg) which to convert to .gif if not then return error. Else Convert the image the to .gif.

How to download avif images from Netflix?

Netflix has published the first AVIF image in 2018, but still only a few software support it. Select files from Computer, Google Drive, Dropbox, URL or by dragging it on the page.


Video Answer


2 Answers

You can to do this with pillow and pillow-avif-plugin:

from PIL import Image
import pillow_avif

JPGimg = Image.open(<filename> + 'jpg')
JPGimg.save(<filename> + '.AVIF','AVIF')

You need to install pillow AVIF to do that.

like image 56
Dov Wachtfogel Avatar answered Oct 22 '22 08:10

Dov Wachtfogel


The best I could come with so far was installing libavif:

brew install libavif

And encode the jpg files directly by executing the avif encoder:

import subprocess


input_file = '/some-path/file1.jpg'
output_file = '/some-path/file1.avif'
subprocess.run(f"avifenc --min 0 --max 63 -a end-usage=q -a cq-level=18 -a tune=ssim {input_file} {output_file}", shell=True)


The options --min 0 --max 63 -a end-usage=q -a cq-level=18 -a tune=ssim are some recommended settings for AVIF images.

like image 33
Ander Avatar answered Oct 22 '22 10:10

Ander