The cv2.imwrite() function in the Python binding of OpenCV is used to save an image to a specified file.
image is the image to be saved. It should be a NumPy array with dimensions [height, width, channels], where height and width are the dimensions of the image, and channels is the number of color channels (e.g., 3 for a color image and 1 for a grayscale image).
By default, cv2.imwrite() will save the image in JPEG format. If you want to save the image in a different format, you can specify the format using the ext parameter, like this:
Here is the basic syntax for using cv2.imwrite():
filename is a string that specifies the name and path of the file to which the image should be saved.cv2.imwrite(filename, image)
image is the image to be saved. It should be a NumPy array with dimensions [height, width, channels], where height and width are the dimensions of the image, and channels is the number of color channels (e.g., 3 for a color image and 1 for a grayscale image).
Here is an example of how to use cv2.imwrite() to save an image:
import cv2
# Load the image
image = cv2.imread('image.jpg')
# Save the image to a file
cv2.imwrite('saved_image.jpg', image)
cv2.imwrite('saved_image.png', image, ext=[cv2.IMWRITE_PNG_COMPRESSION, 0])
This will save the image in PNG format with maximum compression. Other formats that can be specified using the ext parameter include BMP, TIFF, and WebP.You can also specify additional options for saving the image using the params parameter. For example, you can specify the quality of the image when saving it as a JPEG by setting the IMWRITE_JPEG_QUALITY flag:
cv2.imwrite('saved_image.jpg', image, params=[cv2.IMWRITE_JPEG_QUALITY, 95])
This will save the image as a JPEG with a quality of 95%.I hope this helps! Let me know if you have any questions.
Comments
Post a Comment