Python脚本—批量图片格式转换

使用Python的os、sys、PIL库完成批量图片格式转换

import os
import sys
from PIL import Image

input_folder = r'D:\Toolkit\Project\image\traininput'  # 源文件夹
output_folder = r'D:\Toolkit\Project\image\trainoutput'  # 输出文件夹

a = []
for root, dirs, files in os.walk(input_folder):
    for filename in (x for x in files if x.endswith('.jpg')):
        filepath = os.path.join(root, filename)

        object_class = filename.split('.')[0]
        a.append(object_class)
    print(a)

for i in a:
    old_path = input_folder + "\\" + str(i) + '.jpg'
    new_path = output_folder + "\\" + str(i) + '.png'
    img = Image.open(old_path)
    img.save(new_path)
# 上述示例将输入文件夹input_folder中的所有jpg格式图片转换为png格式,并存放在输出文件夹output_folder

你可能感兴趣的:(python,开发语言)