1 import cv2 as cv 2 3 4 def test_read(): 5 img=cv.imread('face.jpg') 6 cv.imshow('read_img',img) 7 # 给读入取名为'read_img' 8 cv.waitKey(1000) 9 # 等待键盘输入(单位毫秒)0为无限等待 10 cv.destroyAllWindows() 11 # 释放内存 OpenCV 底层为C++ 12 13 14 def color2grey(): 15 src=cv.imread('face.jpg') 16 cv.imshow('input image',src) 17 gray_img=cv.cvtColor(src,cv.COLOR_BGR2GRAY) 18 cv.imshow('gray_image',gray_img) 19 # saving the gray image at the same dir 20 cv.imwrite('gray_face.jpg',gray_img) 21 cv.waitKey(0) 22 cv.destroyAllWindows() 23 24 25 test_read() 26 color2grey()