用python将jpg图像转化为png格式图像

代码中需要更改的参数:

1.dirname_read:原始文件夹路径

2.dirname_write:保存新文件夹路径

import os
from PIL import Image

dirname_read = r"D:\fashion_mnist\data_set\flower_data\test-images\4/"  
dirname_write = r"D:\fashion_mnist\data_set\flower_data\test-images\4/"
names = os.listdir(dirname_read)
count = 0
for name in names:
    img = Image.open(dirname_read + name)
    name = name.split(".")
    if name[-1] == "jpg":
        name[-1] = "png"
        name = str.join(".", name)
        to_save_path = dirname_write + name
        img = img.convert('RGB')
        img.save(to_save_path)
        count += 1
        print(name)
    else:
        continue
print('运行完成!共转换了 %d 张图片' % (count))

你可能感兴趣的:(python,图像处理,人工智能,opencv)