Python實(shí)現(xiàn)AVIF圖片與其他圖片格式間的批量轉(zhuǎn)換
圖片格式 AVIF轉(zhuǎn)換為常見的格式,比如 JPG 或 PNG。本文介紹如何使用 Pillow 庫實(shí)現(xiàn)AVIF與其他格式的相互轉(zhuǎn)換。
環(huán)境配置
使用 Python 環(huán)境管理工具 `conda` 和常用庫 `Pillow` 來處理圖片格式轉(zhuǎn)換。環(huán)境的詳細(xì)信息:
- Conda: 24.7.1
- Python: 3.8.19
- Pillow: 10.4.0
- pillow-avif-plugin: 1.4.6
安裝步驟:
conda create -n yourenv python=3.8.19 conda activate yourenv pip install pillow pillow-avif-plugin
Pillow 支持的常見圖片格式:
- JPEG (jpg, jpeg)
- PNG
- GIF
- BMP
- TIFF
- WEBP
- 其他格式(部分格式需要插件支持,如 AVIF)
可以在 Pillow 的官方文檔中查看支持的文件格式
也可以通過代碼查看:
from PIL import Image # 打印 Pillow 支持的文件格式 print(Image.registered_extensions())
1.將單個(gè) AVIF 圖片轉(zhuǎn)換為 JPG 和 PNG
單張圖片轉(zhuǎn)換
將 `.avif` 圖片轉(zhuǎn)換為 `.jpg` 和 `.png` 格式。
import pillow_avif
from PIL import Image
# 將 AVIF 轉(zhuǎn)換為 JPG
def convert_avif_to_jpg(input_path, output_path):
with Image.open(input_path) as img:
# 將圖像轉(zhuǎn)換為 RGB 模式確保兼容性
img = img.convert('RGB')
img.save(output_path, 'JPEG')
# 將 AVIF 轉(zhuǎn)換為 PNG
def convert_avif_to_png(input_path, output_path):
with Image.open(input_path) as img:
img.save(output_path, 'PNG')
# 使用示例
convert_avif_to_jpg('demo.avif', 'output.jpg')
convert_avif_to_png('demo.avif', 'output.png')
運(yùn)行效果:

2.批量轉(zhuǎn)換目錄下所有 AVIF 圖片為其他格式
遍歷一個(gè)目錄下的所有 `.avif` 圖片,批量轉(zhuǎn)換為常見格式如 `JPG` 或 `PNG`。
import os
from PIL import Image
import pillow_avif
def convert_avif(input_path, output_path, output_format='jpg'):
# 打開AVIF圖像
with Image.open(input_path) as img:
# 如果輸出格式是JPG,需要先轉(zhuǎn)換為RGB模式
if output_format.lower() in ['jpg', 'jpeg']:
img = img.convert('RGB')
# 處理其他格式直接保存
img.save(output_path, output_format.upper())
def batch_convert_avif(input_directory, output_directory, output_format='jpg'):
# 確保輸出目錄存在
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# 遍歷輸入目錄中的所有文件
for filename in os.listdir(input_directory):
if filename.lower().endswith('.avif'):
input_path = os.path.join(input_directory, filename)
output_filename = os.path.splitext(filename)[0] + f'.{output_format}'
output_path = os.path.join(output_directory, output_filename)
try:
# 調(diào)用通用轉(zhuǎn)換函數(shù),指定輸出格式
convert_avif(input_path, output_path, output_format)
print(f'已成功轉(zhuǎn)換: {filename} -> {output_format.upper()}')
except Exception as e:
print(f'轉(zhuǎn)換失敗: {filename} 錯(cuò)誤: {str(e)}')
# 使用示例:轉(zhuǎn)換為PNG格式
batch_convert_avif('E:/software/test', 'E:/software/test', output_format='png')
運(yùn)行效果:

3.將其他格式圖片批量轉(zhuǎn)換為 AVIF
將 `JPG` 或 `PNG` 等圖片格式批量轉(zhuǎn)換為 `AVIF`。
import os
from PIL import Image
import pillow_avif
def convert_to_avif(input_path, output_path):
# 打開圖片(支持JPG、PNG等格式)
with Image.open(input_path) as img:
# 如果不是RGBA或RGB模式,需要轉(zhuǎn)換
if img.mode not in ("RGBA", "RGB"):
img = img.convert("RGBA")
# 保存為AVIF格式
img.save(output_path, "AVIF")
def batch_convert_to_avif(input_directory, output_directory, supported_formats=('jpg', 'jpeg', 'png')):
# 確保輸出目錄存在
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# 遍歷輸入目錄中的所有文件
for filename in os.listdir(input_directory):
# 檢查文件是否為支持的格式
if filename.lower().endswith(supported_formats):
input_path = os.path.join(input_directory, filename)
# 修改輸出文件擴(kuò)展名為 .avif
output_filename = os.path.splitext(filename)[0] + '.avif'
output_path = os.path.join(output_directory, output_filename)
try:
# 將圖片轉(zhuǎn)換為AVIF
convert_to_avif(input_path, output_path)
print(f'已成功轉(zhuǎn)換: {filename} -> {output_filename}')
except Exception as e:
# 捕獲并處理異常
print(f'轉(zhuǎn)換失敗: {filename} 錯(cuò)誤: {str(e)}')
# 使用示例:將JPG、PNG批量轉(zhuǎn)換為AVIF
batch_convert_to_avif('E:/software/test/avif', 'E:/software/test/avif')
運(yùn)行效果:

到此這篇關(guān)于Python實(shí)現(xiàn)AVIF圖片與其他圖片格式間的批量轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Python AVIF圖片格式轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于django 1.10 CSRF驗(yàn)證失敗的解決方法
今天小編就為大家分享一篇關(guān)于django 1.10 CSRF驗(yàn)證失敗的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python使用re模塊實(shí)現(xiàn)okenizer(表達(dá)式分詞器)
這篇文章主要介紹了Python使用re模塊實(shí)現(xiàn)okenizer,我們這里講解用正則表達(dá)式構(gòu)建簡(jiǎn)單的表達(dá)式分詞器(tokenizer),它能夠?qū)⒈磉_(dá)式字符串從左到右解析為標(biāo)記(tokens)流,需要的朋友可以參考下2022-04-04
Python實(shí)現(xiàn)Microsoft Office自動(dòng)化的幾種方式及對(duì)比詳解
辦公自動(dòng)化是指利用現(xiàn)代化設(shè)備和技術(shù),代替辦公人員的部分手動(dòng)或重復(fù)性業(yè)務(wù)活動(dòng),優(yōu)質(zhì)而高效地處理辦公事務(wù),實(shí)現(xiàn)對(duì)信息的高效利用,進(jìn)而提高生產(chǎn)率,實(shí)現(xiàn)輔助決策的目的,所以本文給大家介紹了Python實(shí)現(xiàn)Microsoft Office自動(dòng)化的幾種方式,需要的朋友可以參考下2025-03-03
Python結(jié)合Pytest打造自動(dòng)化測(cè)試體系
這篇文章主要為大家詳細(xì)介紹了Python如何結(jié)合Pytest打造自動(dòng)化測(cè)試體系,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2026-03-03
Python通過模塊化開發(fā)優(yōu)化代碼的技巧分享
模塊化開發(fā)就是把代碼拆成一個(gè)個(gè)“零件”,該封裝封裝,該拆分拆分,下面小編就來和大家簡(jiǎn)單聊聊python如何用模塊化開發(fā)進(jìn)行代碼優(yōu)化吧2025-04-04
python結(jié)合selenium獲取XX省交通違章數(shù)據(jù)的實(shí)現(xiàn)思路及代碼
這篇文章主要介紹了python結(jié)合selenium獲取XX省交通違章數(shù)據(jù)的實(shí)現(xiàn)思路及代碼方法的相關(guān)資料2016-06-06

