Open In App

Python OpenCV | cv2.imwrite() method

Last Updated : 26 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

OpenCV-Python

is a library of Python bindings designed to solve computer vision problems.

cv2.imwrite()

method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.

Syntax: cv2.imwrite(filename, image) Parameters:filename: A string representing the file name. The filename must include image format like .jpg, .png, etc. image: It is the image that is to be saved. Return Value: It returns true if image is saved successfully.

Example #1:

# Python program to explain cv2.imwrite() method

# importing cv2 
import cv2

# importing os module  
import os

# Image path
image_path = r'C:\Users\Rajnish\Desktop\GeeksforGeeks\geeks.png'

# Image directory
directory = r'C:\Users\Rajnish\Desktop\GeeksforGeeks'

# Using cv2.imread() method
# to read the image
img = cv2.imread(image_path)

# Change the current directory 
# to specified directory 
os.chdir(directory)

# List files and directories  
# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'  
print("Before saving image:")  
print(os.listdir(directory))  

# Filename
filename = 'savedImage.jpg'

# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, img)

# List files and directories  
# in 'C:/Users / Rajnish / Desktop / GeeksforGeeks'  
print("After saving image:")  
print(os.listdir(directory))

print('Successfully saved')

Output:

Before saving image:
['geeks.png']
After saving image:
['geeks.png', 'savedImage.jpg']
Successfully saved

Python OpenCV | cv2.imwrite() method – FAQs

What file formats are supported by cv2.imwrite()?

cv2.imwrite() supports several image formats, including:

  • JPEG (.jpg, .jpeg)
  • PNG (.png)
  • TIFF (.tiff, .tif)
  • BMP (.bmp)
  • PPM (.ppm)
  • PGM (.pgm)

The available formats depend on the codecs installed in your OpenCV build.

How to set image quality with cv2.imwrite() in Python?

You can control the quality of saved images, particularly for JPEG files, using the params argument:

import cv2

image = cv2.imread('input_image.jpg')
# Save with quality parameter (0-100)
cv2.imwrite('output_image.jpg', image, [cv2.IMWRITE_JPEG_QUALITY, 90])

What are the best practices for saving images with OpenCV in Python?

1. Check File Format Support: Ensure the file format is supported by your OpenCV installation.

2. Specify Image Quality: Use parameters for formats like JPEG to control quality.

3. Verify Image Type: Ensure the image data type (e.g., uint8) matches the format specifications.

4. Handle Errors: Check the return value of cv2.imwrite() to confirm if the image was saved successfully.

success = cv2.imwrite('output_image.jpg', image)
if not success:
print("Error saving image")

Can cv2.imwrite() save images in non-standard color formats?

cv2.imwrite() primarily supports standard color formats (BGR for OpenCV). However, you can save images in other color spaces or formats by converting them before saving. For example, to save an image in grayscale

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray_image.jpg', gray_image)

For non-standard formats or advanced features, consider using additional libraries or custom processing.


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg