最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)AVIF圖片與其他圖片格式間的批量轉(zhuǎn)換

 更新時間:2025年04月09日 09:55:29   作者:頗有幾分姿色  
這篇文章主要為大家詳細(xì)介紹了如何使用 Pillow 庫實(shí)現(xiàn)AVIF與其他格式的相互轉(zhuǎn)換,即將AVIF轉(zhuǎn)換為常見的格式,比如 JPG 或 PNG,需要的小伙伴可以參考下

圖片格式 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.將單個 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 圖片為其他格式

遍歷一個目錄下的所有 `.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} 錯誤: {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} 錯誤: {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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

霍林郭勒市| 五指山市| 阳山县| 涞源县| 安平县| 兴义市| 桐庐县| 烟台市| 日照市| 玉溪市| 修文县| 河津市| 赤峰市| 南投县| 永吉县| 马边| 元朗区| 镇江市| 正镶白旗| 鄂伦春自治旗| 三河市| 疏勒县| 宜兰市| 武鸣县| 西畴县| 莲花县| 朝阳区| 广汉市| 长治市| 自贡市| 奉贤区| 中江县| 乌拉特中旗| 仙居县| 福贡县| 绥化市| 安仁县| 曲麻莱县| 定日县| 旬阳县| 连州市|