open-cv write image
10 March 2025 at 13:30
Explore the OpenCV imwrite function used to write an image.
Source Code
import cv2
image = cv2.imread("./data/openCV_logo.jpg",cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image,(600,600))
cv2.imwrite("./data/openCV_logo_grayscale.jpg",image)
Image

Function
Explain Code
Import the OpenCV library import cv2
.
The imread
function reads an image. Since I need a grayscale image, I set the flag value as cv2.IMREAD_GRAYSCALE
.
Resize the image using the resize()
function.
imwrite
The imwrite
function is used to save an image. It takes two arguments:
- Image path β Set the image path and name.
- Image β The image as a NumPy array.