图像读取、显示、保存
关键语句
- 图像读取
retval = cv2.imread()
- 图像显示
cv2.imshow('Your_window_title', retval)
- 图像保存
cv2.imwrite()
代码如下:
import cv2 IMG_PATH = '.\img\\' IMG_SAVE = '.\save\\' # lena = cv2.imread('lena.png', -1) lena = IMG_PATH + 'lena.png' def show_img(file=lena): """展示图片""" lena = cv2.imread(file, -1) cv2.imshow('lena', lena) key = cv2.waitKey() if key == ord('A'): cv2.imshow("PressA", lena) elif key == ord('B'): cv2.imshow("PressB", lena) cv2.waitKey() cv2.destroyAllWindows() def save_img(file=lena): """保存图片""" lena = cv2.imread(file, -1) retval = cv2.imwrite('dd.png', lena) return retval if __name__ == '__main__': save_img ()