读取图像
import cv2
image_path = 'D:\Word\end.png'
image = cv2.imread(image_path)
cv2.imread() 读取图像文件,返回一个 NumPy 数组。
注意imread('D:\Word\end.png')
的 “\” 和最后的后缀。
如果图像路径错误或文件不存在,返回 None。
判断是否加载成功
if image is None:
print('无法加载图片')
显示图像:
cv2.imshow("Display Window", image)
等待按键输入
cv2.waitKey(0)
cv2.waitKey(0) 等待按键事件,在有按键输入前一直显示,如果要保持一秒后自动关闭显示就把0改成1000,单位应该是毫秒
保存图像
决定是否保存
if key == ord('s'):
outPath = 'D:\Word\end2.png'
//这一步保存
cv2.imwrite(outPath, image)
print(f'图片已保存为{outPath}')
else:
print('图片未成功保存')
键入“s”才会保存
关闭所有窗口
cv2.destroyAllWindows()