Python|用于读取和保存图像的 OpenCV 程序
OpenCV(Open Source Computer Vision)是一个主要针对实时计算机视觉的编程函数库。这是一个跨平台库,它提供了在多种语言中使用的功能。
谈到图像处理,OpenCV 使我们能够对图像执行多种操作,但为了做到这一点,我们需要读取图像文件作为输入,然后我们可以执行所需的操作并保存它。
让我们看一个使用 OpenCV 库读取图像文件然后保存的简单操作。
使用的功能:
-> imread(Location_of_image, integer): The second parameter is an integer for changing the color of image. -1 To read image Unchanged and 0 To read image in gray scale.
-> imwrite(Name_after_save, variable_containing_read_image)
->waitKey(0): After execution will keep the window open till a key is pressed
-> destroyAllWindow(): It will close all the windows that were opened during the program run.
下面是Python的实现:
# Python Program To Read And Save image
import numpy as np
import cv2
# This will give error if you don't have a cv2 module
img = cv2.imread("G:\demo.jpg", -1)
# img is object of cv2 and stores the image read demo.jpg
cv2.imwrite("outputimage.jpg", img)
# The image is saved in folder where program is stored
cv2.waitKey(0)
cv2.destroyAllWindow()
输入 :
输出 :