Open In App

Python OpenCV | cv2.imwrite() method

Last Updated : 23 Dec, 2025
Comments
Improve
Suggest changes
49 Likes
Like
Report

cv2.imwrite() is an OpenCV function used to save an image to storage in any supported format such as JPG, PNG, or BMP. It saves the provided image to the given filename, and the format is determined automatically by the file extension.

Note: For this article, we will use sample images "flower.jpg" and "eyes.jpg", download it and make sure to keep the image in same folder as your python script.

Example: This example loads an image and saves it with a new filename in the same directory.

Python
import cv2
img = cv2.imread("flower.jpg")
cv2.imwrite("saved_image.jpg", img)

Output

Output
Output

Explanation: cv2.imwrite("saved_image.jpg", img) writes the loaded image into a new file named saved_image.jpg.

Syntax

cv2.imwrite(filename, image)

Parameters:

  • filename: Output file name including extension ("output.png", "img.jpg").
  • image: Image array to save.

Examples

Example 1: This example reads an image and saves it as a JPG file with a new name in the same folder.

Python
import cv2
img = cv2.imread("eyes.jpg")
cv2.imwrite("pic_saved.jpg", img)

Output

Output1
A new file pic_saved.jpg is created.

Explanation: cv2.imwrite("pic_saved.jpg", img) writes image data in JPG format because .jpg is used.

Example 2: Here, the image is saved in lossless PNG format while keeping the original image unchanged.

Python
import cv2
img = cv2.imread("eyes.jpg")
cv2.imwrite("output.png", img)

Output

Output2
A new file output.png is created.

Explanation: cv2.imwrite("output.png", img) saves using PNG format since the extension is .png.


Article Tags :

Explore