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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With