# 单张
import os
import cv2
imgdir = 'd:/a/'#图片路径
savepath = 'd:/a/'#保存路径
n = 'a.jpg'#图片名称
img = cv2.imread(imdir)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(savepath + n.split('.')[0]+'_1.png', img)
批量
#读取整个bacepath文件夹下的文件并且转换为8位保存到savepath
import os
import cv2
bacepath = "E:/img/"
savepath = 'E:/img/'
f_n = os.listdir(bacepath)
print(f_n)
for n in f_n:
imdir = bacepath + n
img = cv2.imread(imdir)
print(n)
cropped = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(savepath + n.split('.')[0]+'_1.png', cropped)#NOT CAHNGE THE TYPE