三通道RGB图片转单通道灰度图片:
from PIL import Image
import os
path = ' ' #图片所在路径
file_list = os.listdir(path)
for file in file_list:
I = Image.open(path+"/"+file)
L = I.convert('L')
L.save(path+"/"+file)
tiff转png格式,代码中路径更改为自己图像存放路径即可:
from PIL import Image
import os
imagesDirectory= " " # tiff图片所在文件夹路径
distDirectory = os.path.dirname(imagesDirectory)
distDirectory = os.path.join(distDirectory, " ") # 要存放png格式的文件夹路径
for imageName in os.listdir(imagesDirectory):
imagePath = os.path.join(imagesDirectory, imageName)
image = Image.open(imagePath) # 打开tiff图像
distImagePath = os.path.join(distDirectory, imageName[:-4]+'.png') # 更改图像后缀为.png,与原图像同名
image.save(distImagePath) # 保存png图像