Python OpenCV: Saving an image to the file system

In this tutorial we will check how to save an image to the file system using OpenCV on Python.


Introduction

In this tutorial we will check how to save an image to the file system using OpenCV on Python.

As we have been covering in previous tutorials, OpenCV allows us to read and manipulate images, amongst many other features. So, it is useful to have a way of saving the changes we performed in a new image.

In this tutorial, we will read an image and convert it to gray scale, and then save the new image to our file system. For a detailed tutorial on how to convert an image to gray scale, please consult this previous post.

This tutorial was tested using Python version 2.7.8 and OpenCV version 3.2.0.


The code

The first thing we need to do is importing the cv2 module, to be able to access the OpenCV functions.

import cv2

Next, we need to read the original image we want to convert to gray scale. To do it, we need to call the imread function. This function receives as input the path of the image to read and returns the image as a numpy ndarray

For a detailed tutorial on how to read and display an image using OpenCV, please check here.

image = cv2.imread('C:/Users/N/Desktop/Test.jpg') 

Now we will convert the image to gray scale. To do it, we use the cvtColor function. This function allows to convert an image from one color space to another, as explained in more detail in this previous post.

The cvtColor receives as first input the original image and as second input a code representing the color space conversion. We are going to use the COLOR_BGR2GRAY code because, by default, the imread function we used before loads the image in BGR.

As output, the cvtColor function will return the converted image.

image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

In order to save the converted image, we will use the imwrite function, which allows to save the image to a file. As first input, this function receives the path where to save the image and as second it receives the image.

cv2.imwrite('C:/Users/N/Desktop/Test_gray.jpg', image_gray) 

The final source code can be seen below.

import cv2

image = cv2.imread('C:/Users/N/Desktop/Test.jpg')
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imwrite('C:/Users/N/Desktop/Test_gray.jpg', image_gray)

 

Testing the code

To test the code, simply run it on the Python environment of your choice, changing the file paths to the images you want to read and write in your computer. When the program execution finishes, you should have the gray scale converted image in your file system, at the location you specified in the imwrite function call, as shown in figure 1.

Python openCV save image.png

Figure 1 – Gray scale converted image using OpenCV, saved in the file system.

 

Related Posts

Advertisements
This entry was posted in Python and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s