I'm learning OpenCV and Python. I captured some images from my webcam and saved them. But they are being saved by default into the local folder. I want to save them to another folder from direct path. How do I fix it?
-
1I would like to note that you probably need to create the folder where you want to save the images (i.e. it must exist), otherwise, it will be as nothing happened when calling `imwrite`. – nbro May 21 '20 at 17:14
8 Answers
The solution provided by ebeneditos works perfectly.
But if you have cv2.imwrite()
in several sections of a large code snippet and you want to change the path where the images get saved, you will have to change the path at every occurrence of cv2.imwrite()
individually.
As Soltius stated, here is a better way. Declare a path and pass it as a string into cv2.imwrite()
import cv2
import os
img = cv2.imread('1.jpg', 1)
path = 'D:/OpenCV/Scripts/Images'
cv2.imwrite(os.path.join(path , 'waka.jpg'), img)
cv2.waitKey(0)
Now if you want to modify the path, you just have to change the path
variable.
Edited based on solution provided by Kallz

- 20,118
- 13
- 80
- 87
You can do it with OpenCV's function imwrite
:
import cv2
cv2.imwrite('Path/Image.jpg', image_name)

- 2,542
- 1
- 15
- 35
-
1And even better practice is declaring your path as a string variable in case you want to change it and you're using a bunch of imwrite()s. – Soltius Jan 11 '17 at 09:17
-
this code is not working on windows 10. i have tested it. the folder is not being created, i mean folder named 'Path', can you tell why? – Shahryar Feb 02 '20 at 11:02
-
3@Shahryar cv2.imwrite() doen't craete the folder if the folder doesn't exists. You'll have to check if the folder exists and if it does write to it, otherwise create it using os.makedir() – Anidh Singh Nov 10 '21 at 10:15
Answer given by Jeru Luke is working only on Windows systems, if we try on another operating system (Ubuntu) then it runs without error but the image is saved on target location or path.
Not working in Ubuntu and working in Windows
import cv2
img = cv2.imread('1.jpg', 1)
path = '/tmp'
cv2.imwrite(str(path) + 'waka.jpg',img)
cv2.waitKey(0)
I run above code but the image does not save the image on target path. Then I found that the way of adding path is wrong for the general purpose we using OS module to add the path.
Example:
import os
final_path = os.path.join(path_1,path_2,path_3......)
working in Ubuntu and Windows
import cv2
import os
img = cv2.imread('1.jpg', 1)
path = 'D:/OpenCV/Scripts/Images'
cv2.imwrite(os.path.join(path , 'waka.jpg'),img)
cv2.waitKey(0)
that code works fine on both Windows and Ubuntu :)

- 55,762
- 10
- 62
- 120

- 3,244
- 1
- 20
- 38
-
2
-
Very useful for ubuntu users. But I wasted some of my time by providing the `path` as `~/.../.../src/final_dir/` when I had to simply use `final_dir/`. – Pe Dro Sep 25 '20 at 10:24
Thank you everyone. Your ways are perfect. I would like to share another way I used to fix the problem. I used the function os.chdir(path)
to change local directory to path. After which I saved image normally.

- 20,118
- 13
- 80
- 87

- 495
- 1
- 4
- 10
You can use this simple code in the loop by incrementing a count.
cv2.imwrite("C:\\path\\where you\\want to save\\frame%d.jpg" % count, image)
Images will be saved in the folder by name line frame0.jpg
, frame1.jpg
, frame2.jpg
etc..

- 888
- 1
- 8
- 20

- 31
- 2
FOR MAC USERS if you are working with open cv
import cv2
cv2.imwrite('path_to_folder/image.jpg',image)

- 359
- 3
- 10
This is a more obvious approach, with a simple f'string'
, instead of os.join
- plus some extra functionality:
import cv2
img = cv2.imread('yourfile.jpg')
path = 'C:/OpenCV/Scripts/Images'
name = '/epic_img_title.jpg'
# write the file
cv2.imwrirte(f'{path}{name}', img)
f strings are clean and easy. Plus you can change the formatted variables on the fly, so good to go!
If you are saving a lot of images and want to iterate the name of each one:
import cv2
img = cv2.imread('yourfile.jpg')
def save_my_img(frame_count, img):
path = 'C:/OpenCV/Scripts/Images'
name = f'/img_title_{frame_count}.jpg'
cv2.imwrite(os.path.join(path, name), img)
# give it a call
save_my_img(1, img)
and pull it later...
def read_saved_img(frame_count):
path = 'C:/OpenCV/Scripts/Images'
name = f'/img_title_{frame_count}.jpg'
img = cv2.imread(f'{path}{name}')
# img is now an array, so let's just return it
return img
# give it a call
cv2_image_array = read_saved_img(1)
# random variable 'cv2_image_array' to hold
# the array that was returned. Now you can
# edit it like any other image you cv2.imread()

- 127,765
- 105
- 273
- 257

- 111
- 2
-
1This might be _easy_, but there are hidden problems. For one thing, those backslashes will be interpreted as escape codes if they precede the wrong letter. Try `foo = "bar\foo"`, then look at the value of `foo`. You either need to manually escape them or use raw string literals. For another, this isn't very cross-platform. And you can end up with double backslashes, or no backslashes, if you aren't careful. There's a reason that things like `os.path.join` and `pathlib` exist: creating paths is trickier than most of us expect. Correct is usually more important than easy. – ChrisGPT was on strike Mar 14 '22 at 22:27
-
Please refrain from using profanity, even in short forms. And, again, there are good reasons for `os.path` and `pathlib` to exist. Dismissing them out of hand is naive. There can also be _security_ implications of using strings instead of built-in modules for this. Imagine you want to constrain user input to be relative to a given path so you do something like `if not user_input.startswith("/some/safe/path")...` as a security measure. Well, what if the user enters `/some/safe/path/../../../root`? The modules in the standard library can resolve this easily. – ChrisGPT was on strike Mar 15 '22 at 11:48
-
Not sure what you mean with profanity, other than that, your point makes good sense for other use cases. Good job. – JohnSmith2000 Mar 16 '22 at 01:41
It worked! Simple fix. You just have to have the empty folders already set up in your directory for it to write images to.
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '22 at 20:02