cv2.imwrite
时间: 2023-10-19 12:16:56 浏览: 28
cv2.imwrite() is a function in the OpenCV (cv2) library in Python that is used to write an image to a specified file. The function takes two parameters - the file path where the image will be saved and the image array itself. Here is an example usage:
```
import cv2
# Read an image
img = cv2.imread("image.jpg")
# Convert the image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Save the grayscale image to a file
cv2.imwrite("grayscale_image.jpg", gray_img)
```
In this example, we first read an image file using cv2.imread(), then convert it to grayscale using cv2.cvtColor(). Finally, we use cv2.imwrite() to save the grayscale image to a file named "grayscale_image.jpg".
相关问题
cv2.IMWRITE_UNCHANGED
cv2.IMWRITE_UNCHANGED是OpenCV库中的一个参数,用于在图像写入文件时保持图像的原始通道数和深度。当你使用cv2.imwrite()函数保存图像时,可以通过设置参数cv2.IMWRITE_UNCHANGED来保持图像的原始属性。
cv2.IMWRITE_UNCHANGED的具体用法如下:
```python
cv2.imwrite(filename, img, [cv2.IMWRITE_UNCHANGED])
```
其中,filename是保存图像的文件名,img是要保存的图像。cv2.IMWRITE_UNCHANGED为可选参数,如果设置了该参数,则会保存图像的所有通道和深度信息。
举个例子,如果你有一张RGB彩色图像,并且想要将其保存为PNG格式但保持原始属性,你可以这样使用cv2.IMWRITE_UNCHANGED:
```python
import cv2
img = cv2.imread('input.jpg')
cv2.imwrite('output.png', img, [cv2.IMWRITE_UNCHANGED])
```
这样,保存的output.png文件将保持原始的RGB通道和深度信息。