0
0
Computer Visionml~5 mins

Writing/saving images in Computer Vision

Choose your learning style9 modes available
Introduction

Saving images lets you keep your results or processed pictures to look at later or share with others.

After editing or enhancing a photo, you want to save the new version.
When your AI model creates images, you save them to check or use later.
You want to save screenshots or camera captures automatically.
To keep processed images for reports or presentations.
Saving images after resizing or cropping for a website or app.
Syntax
Computer Vision
import cv2
cv2.imwrite(filename, image)

filename is the path and name where you want to save the image.

image is the image data you want to save, usually a NumPy array.

Examples
Reads an image and saves it with a new name.
Computer Vision
import cv2
image = cv2.imread('input.jpg')
cv2.imwrite('output.jpg', image)
Creates a black 100x100 image and saves it as a PNG file.
Computer Vision
import cv2
import numpy as np
image = np.zeros((100, 100, 3), dtype=np.uint8)
cv2.imwrite('black.png', image)
Sample Model

This program reads an image named 'input.jpg' and saves it as 'saved_image.jpg'. It prints messages to confirm success or failure.

Computer Vision
import cv2

# Read an image from file
image = cv2.imread('input.jpg')

# Check if image was loaded
if image is None:
    print('Failed to load image.')
else:
    # Save the image with a new name
    success = cv2.imwrite('saved_image.jpg', image)
    if success:
        print('Image saved successfully.')
    else:
        print('Failed to save image.')
OutputSuccess
Important Notes

Make sure the folder where you save the image exists, or saving will fail.

Supported formats depend on the file extension you use, like .jpg, .png, .bmp.

Saving large images may take more time and disk space.

Summary

Use cv2.imwrite() to save images to files.

Provide the filename and image data as arguments.

Check if saving was successful to avoid errors.