Opencv Python 简明教程
OpenCV Python - Write an image
CV2 程序包有 imwrite() 函数,可将图像对象保存到指定的文件中。
CV2 package has imwrite() function that saves an image object to a specified file.
使用 imwrite() 函数保存图像的命令如下 −
The command to save an image with the help of imwrite() function is as follows −
cv2.imwrite(filename, img)
图像格式由 OpenCV 从文件扩展名自动确定。OpenCV 支持 .bmp, *.dib , *.jpeg, *.jpg, *.png, .webp、 .sr, .tiff、*.tif 等图像文件类型。
The image format is automatically decided by OpenCV from the file extension. OpenCV supports .bmp, *.dib , *.jpeg, *.jpg, *.png,.webp, .sr,.tiff, \*.tif etc. image file types.
Example
以下程序加载 OpenCV 徽标图像并在按下“s”键时保存其灰度版本 −
Following program loads OpenCV logo image and saves its greyscale version when ‘s’ key is pressed −
import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('OpenCV_Logo.png',0)
cv2.imshow('image',img)
key=cv2.waitKey(0)
if key==ord('s'):
cv2.imwrite("opencv_logo_GS.png", img)
cv2.destroyAllWindows()