Python批量修改PNG格式为JPG格式代码(四通道转换为三通道)

from PIL import Image
import os

# 设置要转换的图片文件夹路径
folder_path = 'C:/Users/Administrator/Desktop/CR'

# 获取文件夹中所有PNG图片名
png_names = [f for f in os.listdir(folder_path) if f.endswith('.png')]

# 遍历PNG图片名列表并转换为JPG
for png_name in png_names:
    # 构造旧文件的路径和新文件的路径
    old_image_path = os.path.join(folder_path, png_name)
    new_image_path = os.path.join(folder_path, os.path.splitext(png_name)[0] + '.jpg')
    # 打开PNG图片并转换为JPG
    with Image.open(old_image_path) as image:
        image.convert('RGB').save(new_image_path, 'JPEG')
        print(f'{png_name}已转换为JPG并保存为{new_image_path}')

你可能感兴趣的:(代码应用实例,python,图像处理)