假如我们需要对一个文件夹里的图片(各种类型:比如jpg、png、tif.....)修改为统一类型比如jpg,并重新命名。python实现如下:
import os
from PIL import Image
path="图片集"
count=0
for maindir, subdir,file_name_list in os.walk(path):
for file_name in file_name_list: #遍历图片集中的每张图片
image=os.path.join(maindir,file_name) #获取图片的绝对路径
file=Image.open(image)
print(file_name)
print(image)
RGB=file.convert('RGB') #格式转换
RGB.save("图片集/"+str(count)+".jpg") #保存为jpg格式,名字从0开始命名
count+=1