三通道RGB圖片轉(zhuǎn)單通道灰度圖片:
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轉(zhuǎn)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) # 打開(kāi)tiff圖像
distImagePath = os.path.join(distDirectory, imageName[:-4]+'.png') # 更改圖像后綴為.png丑蛤,與原圖像同名
image.save(distImagePath) # 保存png圖像