将一个文件夹下的一种格式完全转换为另外一种格式的图片:
1、将文件下的图片全部转换为png格式(转换之后输出的图片跟原始输入图像在同一个文件夹下)
import os
from PIL import Image
json_dir = r"E:\input"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
label_dir.append(os.path.join(json_dir,filename))
for i,filename in enumerate(label_dir):
im = Image.open(filename) # open ppm file
newname = label_names[i].split('.')[0] + '.png' # new name for png file
im.save(os.path.join(json_dir,newname))
2、将文件下的图片全都转换为jpg格式(跟1完全一样,就是代码中图片最后命名的后缀不一样)
import os
from PIL import Image
json_dir = r"E:\input"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
label_dir.append(os.path.join(json_dir,filename))
for i,filename in enumerate(label_dir):
im = Image.open(filename) # open ppm file
newname = label_names[i].split('.')[0] + '.jpg' # new name for png file
im.save(os.path.join(json_dir,newname))
用于日常记录,希望能帮助有需要的小伙伴。新年将至!!新的一年祝大家科研顺利!!!多多发论文!!!!