png图片批量转jpg

from PIL import Image
import os

def png_to_jpg(png_path, jpg_path):
    image = Image.open(png_path)
    rgb_image = image.convert('RGB')
    rgb_image.save(jpg_path, 'JPEG')

# 文件夹路径
folder_path = "path/try"

# 获取文件夹中的所有文件
file_list = os.listdir(folder_path)

# 循环处理每个文件
for png_filename in file_list:
    # 检查文件扩展名是否为 .png
    if png_filename.lower().endswith('.png'):
        jpg_filename = png_filename[:-4] + '.jpg'  # 替换扩展名为 .jpg

        png_path = os.path.join(folder_path, png_filename)
        jpg_path = os.path.join(folder_path, jpg_filename)

        png_to_jpg(png_path, jpg_path)
        print(f"Converted {png_filename} to {jpg_filename}")