python2安装PIL库,python3安装Pillow库,无需改动代码
import os
from PIL import Image
dirname_read="G:/sar2optical/testA/" # 注意后面的斜杠
dirname_write="G:/sar2optical/output/"
names=os.listdir(dirname_read)
count=0
for name in names:
img=Image.open(dirname_read+name)
name=name.split(".")
if name[-1] == "png":
name[-1] = "jpg"
name = str.join(".", name)
to_save_path = dirname_write + name
img = img.convert('RGB')#RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度。而JPG不支持透明度,所以要么丢弃Alpha,要么保存为.png文件
img.save(to_save_path)
count+=1
print(name)
else:
continue
print('运行完成!共转换了 %d 张图片' % (count))
文章评论