RBG转灰度图

RBG转灰度图,要求储存为png格式

import cv2
import os
def main(path,topath):
    # print(path)
    folders = os.listdir(path)
    # print(folders)
    for idx,pic in enumerate(folders):
        # print(path  + '{}'.format(pic))
        input_image = cv2.imread(path+'/'+'{}'.format(pic),1)
        print(pic,input_image.shape)

        image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
        # cv2.imshow("image", image)  # 显示图片,后面会讲解
        # cv2.waitKey(0)  # 等待按键
        # print(topath,pic.replace('.jpg','.png'))
        cv2.imwrite(topath+pic.replace('.jpg','.png'), image)
if __name__ == '__main__':
    path = r'./pic/'
    topath = r'./grey/'
    main(path,topath)


你可能感兴趣的:(opencv,计算机视觉,python)