6

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?

4

3 回答 3

2

您可以使用枕头和枕头-avif-plugin做到这一点:

from PIL import Image
import pillow_avif

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

您需要安装枕头 AVIF 才能做到这一点。

于 2021-10-14T07:34:50.447 回答
1

到目前为止,我能想到的最好的方法是安装 libavif

brew install libavif

并通过执行 avif 编码器直接对 jpg 文件进行编码:

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)


这些选项--min 0 --max 63 -a end-usage=q -a cq-level=18 -a tune=ssim是 AVIF 图像的一些推荐设置。

于 2021-10-14T08:19:10.067 回答
0

据我所知,目前不支持“avif”。但是有 API 支持。例如,这个 API做得很好。也许可能会出现安全问题。该网站看起来并不好,但功能上做得很好。

我检查了一些 API源代码,我发现你可以在不需要这个 API 的情况下进行这种转换。API 正在使用 magick命令进行此转换。我不知道如何使用这个命令,但我想如果你稍微搜索一下,或者查看源代码,你就可以弄清楚。

于 2021-10-14T06:49:35.257 回答