Hello everybody,
Michael here, and in today’s post, we’ll continue our introduction to computer vision, but this time we’ll explore how to write images to a certain place on your computer using OpenCV.
Let’s begin!
Let’s write an image!
Before we begin, here’s the image we will be working with:

This is an image of Simba/Orange Boy and his sister Marbles (on Christmas Day 2017 excited to get their presents), both of whom got an acknowledgement in The Glorious Five-Year Plan Part Two.
Now, here’s the code to read in the image to the IDE:
cats=cv2.imread(r'C:\Users\mof39\Downloads\IMG_4778 (1).jpg', cv2.IMREAD_COLOR)
cats=cv2.cvtColor(cats, cv2.COLOR_BGR2RGB)
Once this image is read onto the IDE, here’s the code we’d use to not only write this image but also save it to a certain directory on your computer:
import os
imagePath = r'C:\Users\mof39\Downloads\IMG_4778 (1).jpg'
imageDestination = r'C:\Users\mof39\OneDrive\Documents'
cats = cv2.imread(imagePath)
os.chdir(imageDestination)
savedImage = 'simbaandmarbles.jpg'
cv2.imwrite(savedImage, cats)
What does all of this code mean? Let me explain.
You’ll first need to import the os module (or pip install it if you haven’t already done so)-this will help you write and save the image to a specific directory.
The two variables that follow-imagePath and imageDestination-represent the current location of the image on my computer and the location on my computer where I wish to write and save the image, respectively. In this case, my image is currently located in my Downloads folder and I wish to send it to my Documents folder.
The cats variable is the result of reading in the image of the cats to the IDE. The os.chdir() function takes in one parameter-the string containing the image destination path. This function will allow you to set the destination of the image to ensure that your image is written and saved to the location you set in the imageDestination variable.
The savedImage variable allows you to set both the image name and the image extension to the image you wish to save and write-in this case, my image will be named simbaandmarbles and it will have a jpg extension.
Last but not least, use the cv2.imwrite() function to write and save the image to your desired directory (represented by the imageDestination variable). You’ll notice that this function takes two parameters-savedImage and cats in this example-but why might that be? Take a look at the code above and you’ll see why!
See, savedImage is the name we’d like to use for the saved image-this is a necessary paramater because we want OpenCV to save the image using the name/extension we specified. cats saves the image itself to the desired location (or imageDestination).
- You should certainly change the values of
imagePath,imageDestinationandsavedImageto reflect accurate image locations/destinations/names/extensions on your computer!
But wait! How do we know if our code worked? Take a look at the output below:
True
Since the output of this code returned True, the image was succesfully written and saved to the desired destination on our computer! Want another way to verify if our code worked? Take a look at my Documents folder (which was my imageDestination):
As you can see, my image was succesfully written to my Documents folder with the name/extension I specified (simbaandmarbles/JPG).
Now we know the image was succesfully written and saved to the Documents folder, but how do we know if the rendering worked? In other words, did OpenCV zoom in or crop too much of the image (or change the colorscale during the writing/saving process)? Click on the image to find out:
As you can see, not only did OpenCV correctly write and save the image to the correct location, but it also wrote and saved the image without changing the zoom-in/zoom-out view or the image’s colorscale!
And that, dear readers, is how you can write and save an image anywhere on your computer using eight simple lines of code!
Thanks for reading.
Michael