Python實現(xiàn)AVIF圖片與其他圖片格式間的批量轉換
圖片格式 AVIF轉換為常見的格式,比如 JPG 或 PNG。本文介紹如何使用 Pillow 庫實現(xiàn)AVIF與其他格式的相互轉換。
環(huán)境配置
使用 Python 環(huán)境管理工具 `conda` 和常用庫 `Pillow` 來處理圖片格式轉換。環(huán)境的詳細信息:
- 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.將單個 AVIF 圖片轉換為 JPG 和 PNG
單張圖片轉換
將 `.avif` 圖片轉換為 `.jpg` 和 `.png` 格式。
import pillow_avif from PIL import Image # 將 AVIF 轉換為 JPG def convert_avif_to_jpg(input_path, output_path): with Image.open(input_path) as img: # 將圖像轉換為 RGB 模式確保兼容性 img = img.convert('RGB') img.save(output_path, 'JPEG') # 將 AVIF 轉換為 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')
運行效果:
2.批量轉換目錄下所有 AVIF 圖片為其他格式
遍歷一個目錄下的所有 `.avif` 圖片,批量轉換為常見格式如 `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,需要先轉換為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)用通用轉換函數(shù),指定輸出格式 convert_avif(input_path, output_path, output_format) print(f'已成功轉換: {filename} -> {output_format.upper()}') except Exception as e: print(f'轉換失敗: {filename} 錯誤: {str(e)}') # 使用示例:轉換為PNG格式 batch_convert_avif('E:/software/test', 'E:/software/test', output_format='png')
運行效果:
3.將其他格式圖片批量轉換為 AVIF
將 `JPG` 或 `PNG` 等圖片格式批量轉換為 `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模式,需要轉換 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) # 修改輸出文件擴展名為 .avif output_filename = os.path.splitext(filename)[0] + '.avif' output_path = os.path.join(output_directory, output_filename) try: # 將圖片轉換為AVIF convert_to_avif(input_path, output_path) print(f'已成功轉換: {filename} -> {output_filename}') except Exception as e: # 捕獲并處理異常 print(f'轉換失敗: {filename} 錯誤: {str(e)}') # 使用示例:將JPG、PNG批量轉換為AVIF batch_convert_to_avif('E:/software/test/avif', 'E:/software/test/avif')
運行效果:
到此這篇關于Python實現(xiàn)AVIF圖片與其他圖片格式間的批量轉換的文章就介紹到這了,更多相關Python AVIF圖片格式轉換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python內(nèi)置函數(shù)breakpoint()與bytearray()示例詳解
本文給大家分享的是python內(nèi)置函數(shù)breakpoint()與bytearray()的相關資料,并給大家附上了詳細代碼,有需要的小伙伴可以參考下2017-04-04Python?mistune庫靈活的Markdown解析器使用實例探索
本文將深入介紹Python?Mistune,包括其基本概念、安裝方法、示例代碼以及一些高級用法,以幫助大家充分利用這一工具來處理Markdown文本2024-01-01