# -*- coding:utf-8 -*- import cv2 import os input_dir = '/home/wmy/darknet/VOCdevkit3/VOC2007/JPEGImages' out_dir = '/home/wmy/darknet/VOCdevkit3/VOC2007/JPEGImages' a = os.listdir(input_dir) for i in a: img = cv2.imread(input_dir+'/'+i) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) cv2.imencode('.jpg', gray)[1].tofile(out_dir+'/'+i)
在python3中,cv2.write()函數已不適用,所以可用cv2.imencode()函數替代,tofile爲指定路徑。python
轉化前:函數
轉化後:spa
注意!該腳本會修改原圖像,所以如須要原圖的話,最好留一份備份。code