1、利用opencv批量处理文件夹中的图片并且灰度化
import cv2
import numpy as np
import os
if __name__ == "__main__":
path = "C:/xxxxxxxx" #处理文件夹
path1 = "C:/xxxxxxxxxxxx" #保存文件夹
filelist = os.listdir(path)
for file in filelist:
print(file)
img_path =os.path.join(path,file)
img = cv2.imread(img_path)
width,height = img.shape[:2][::-1]
img_resize = cv2.resize(img,(int(width*0.5),int(height*0.5)),interpolation=cv2.INTER_CUBIC) #转换成灰度值
print("img_reisze shape:{}".format(np.shape(img_resize)))
img_gray = cv2.cvtColor(img_resize,cv2.COLOR_RGB2GRAY)
cv2.imwrite(path1+file+".jpg",img_gray) #保存
print("img_gray shape:{}".format(np.shape(img_gray)))
cv2.waitKey()
代码仅供学习参考