OpenCV Python – Write (Save) an Image
After reading and processing an image in OpenCV, the next important step is saving or writing the image to your system. OpenCV provides a simple function called cv2.imwrite() to save images in different formats like JPG, PNG, BMP, etc.
In this tutorial, you will learn how to write images using OpenCV Python with clear examples.
1. What is Writing an Image in OpenCV?
Writing an image means saving the processed image from memory (NumPy array) into a file on your computer.
You can save images after:
- Applying filters
- Resizing
- Drawing shapes
- Edge detection
- Any image processing operation
2. Import OpenCV
First, import the OpenCV library:
import cv2
3. Syntax of cv2.imwrite()
cv2.imwrite(filename, image)
Parameters:
- filename → Name of output image file (with extension)
- image → Image data (NumPy array)
4. Example: Save an Image
import cv2
img = cv2.imread("image.jpg")
cv2.imwrite("output.jpg", img)
✔ This will save the image as output.jpg in your working directory.
5. Save Processed Image
You can also save images after processing.
Example: Convert to Grayscale and Save
import cv2
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite("gray_output.jpg", gray)
6. Save Image After Drawing Shapes
import cv2
img = cv2.imread("image.jpg")
cv2.rectangle(img, (50,50), (200,200), (0,255,0), 2)
cv2.circle(img, (150,150), 50, (0,0,255), -1)
cv2.imwrite("shapes_output.jpg", img)
7. Supported Image Formats
OpenCV supports multiple formats:
-
.jpg→ Small size, compressed -
.png→ High quality, supports transparency -
.bmp→ Uncompressed, large size -
.tiff→ High-quality image storage
8. Check If Image Saved Successfully
import cv2
img = cv2.imread("image.jpg")
success = cv2.imwrite("output.jpg", img)
if success:
print("Image saved successfully")
else:
print("Failed to save image")
9. Important Tips
✔ Always include file extension
✔ Ensure correct folder path
✔ Use PNG for high-quality output
✔ Avoid overwriting important files
10. Common Errors
❌ Image not saving
✔ Solution:
- Check write permissions
- Verify file path
- Ensure image is not empty
❌ Blank output image
✔ Solution:
- Ensure image is properly loaded
if img is None:
print("Image not loaded")
11. Applications of Saving Images
Image writing is used in:
- Saving edited photos
- Exporting AI results
- Storing detected faces
- Saving processed medical images
- Computer vision pipelines
12. Conclusion
Writing images in OpenCV Python is simple but very important. With cv2.imwrite(), you can easily save processed images in different formats and use them in real-world applications.
Once you master this, you can move on to advanced image processing workflows.

%20an%20Image%20%E2%80%93%20Complete%20Beginner%20Tutorial.jpg)
0 Comments