How to convert JPG images to AVIF with Python

Viewed 2529
3 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.

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.

As far as I know, there is no support for 'avif' right now. But there are API supports. For example, this API is doing well. Maybe security issues could occur. The website doesn't look good, but functionally doing well.

I've checked up API source code a little, and I found out that you could make this conversion without needing this API. API is using magick command to make this conversion. I don't know how to use this command, but I think you could figure it out if you search a little, or check out the source code.

Related