How to Save an Image in Python using OpenCV



Python


In this article, we show how to save an image in Python using the OpenCV module.

So with OpenCV, you can do modifications to an image, such as filter out colors, resize the image, convert the image to grayscale, etc. After performing these modifications, you may then want to save this image to a certain directory.

With the OpenCV module, we can open up any image, make any amount of modifications to it, and it gives us the ability to save the image to a directory, such as on your computer or any certain filesystem, such as in a directory of your website.

We can save an image with OpenCV using the imwrite() function. This imwrite() function takes in 2 parameters, the first parameter is the full path to the file, which includes the name of the file. The second parameter is the image itself that you want to save.

Below is the code that takes an image named "Tree.jpg" and converts the image into grayscale and then saves the image as "Gray-scale-tree.jpg" into the current working directory.

This is a practical example of how you will open an image, modify it, and then save it. Of course, no modification has to be made. You can use the save function simply to save an existing image in another directory of your file system without any modifications. Saving a modified image, though, probably is the most common situation .



So let's now go over this code.

So the first thing we have do is to import the OpenCV module, which we do with the line, import cv2

The next thing we must do is import numpy, which by convention is normally shorthanded by np. We use numpy to make a copy of the original image.

Next, we must load the image in. If the image is in the current working directory that you are using, then you simply just have to specify the file name. If it isn't, then you have to specify the full path on your computer to the image. In the example, the image is in the same folder as the current working directory, so we simply specify the file name, 'Rose.jpg'.

So we now have the image loaded into the variable, image.

Next, we create a copy of the image using numpy. The reason we want to create a copy of the image is because we want the original image left alone and undisturbed. Therefore, we have the original image and then we have a separate image the original image but with grayscale conversion.

We then create another variable, grayimage. This will represent the original image with grayscale conversion.

Now that we have the image in grayscale, we now want to save the image and we do this using the imwrite() function.

The imwrite() function takes 2 parameters.

The first parameter is the full path to the image. You make up what you want to call the image filename. Make sure that when you do this, you include the file extension of the image, such as jpg, png, bmp, or else there will be an error. OpenCV supports a wide variety of image file types.

The second parameter is the image that you want to save, in this case, grayimage.

This image saves to the current working directory. If you want to save the image anywhere else than the current working directory, then you have to specify the full path where you want to save it.

And this is how to save an image in Python with the OpenCV module.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...