通道转换
方法一
import os
import cv2
path = r' ..... ' # 图像所在路径
filelist = os.listdir(path)
print(len(filelist))
for file in filelist:
orgin_dir = os.path.join(path, file) # 原路径
print(orgin_dir) # 检查路径
img = cv2.imread(orgin_dir, cv2.IMREAD_GRAYSCALE) # 读取灰度图
print(img.shape) # 打印维度
cv2.imwrite(orgin_dir, img) # 覆盖原图
方法二
import cv2
import glob
filename = glob.glob(r'C:\Users\*.png')
for i in filename:
img = cv2.imread('{}'.format(i), cv2.IMREAD_GRAYSCALE)
print(i)
cv2.imwrite(r'{}'.format(i), img)
转换对比