from PIL import Image
import os
INPUT_PATH = r'F:\AIStudio\Test'
OUPUT_PATH = r'F:\AIStudio\Test_Gray'
files_list = os.listdir(INPUT_PATH) # 读取列表信息,可打印查看
for file in files_list:
# Version1
I = Image.open(INPUT_PATH + "/" + file)
L = I.convert('L') # Image包内将“L“代表灰度
L.save(OUPUT_PATH + "/" + file)
# Version2
# os.path.spiltext 分割文件名和扩展名
# filename = os.path.splitext(file)[0]
# filexten = os.path.splitext(file)[1]
# if filexten == '.png' or '.jpg':
# I = Image.open(INPUT_PATH + "/" + file)
# L = I.convert('L')
# L.save(OUPUT_PATH + "/" + file)