Python OpenCV - 将图像保存到选择的特定文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41586429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV - Saving images to a particular folder of choice
提问by Hieu Tran Trung
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?
我正在学习 OpenCV 和 Python。我从我的网络摄像头捕获了一些图像并保存了它们。但它们默认保存到本地文件夹中。我想将它们从直接路径保存到另一个文件夹。我如何解决它?
回答by Jeru Luke
The solution provided by ebeneditos works perfectly.
ebeneditos 提供的解决方案完美运行。
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.
但是如果你有cv2.imwrite()
一个大代码片段的几个部分,并且你想要更改保存图像的路径,则必须在每次出现时cv2.imwrite()
单独更改路径。
As Soltius stated, here is a better way. Declare a path and pass it as a string into cv2.imwrite()
正如 Soltius 所说,这是一个更好的方法。声明一个路径并将其作为字符串传递到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.
现在,如果要修改路径,只需更改path
变量即可。
Edited based on solution provided by Kallz
根据 Kallz 提供的解决方案进行编辑
回答by ebeneditos
You can do it with OpenCV's function imwrite
:
您可以使用 OpenCV 的功能来实现imwrite
:
import cv2
cv2.imwrite('Path/Image.jpg', image_name)
回答by Hieu Tran Trung
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.
谢谢大家。你的方法很完美。我想分享我用来解决问题的另一种方法。我使用该函数os.chdir(path)
将本地目录更改为路径。之后我正常保存图像。
回答by Kallz
Answer given by Jeru Lukeis 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.
Jeru Luke给出的答案仅适用于 Windows 系统,如果我们尝试在另一个操作系统 (Ubuntu) 上运行,则它可以正常运行,但图像会保存在目标位置或路径上。
Not working in Ubuntu and working in Windows
不能在 Ubuntu 中工作并在 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 OSmodule to add the path.
我运行上面的代码,但图像没有将图像保存在目标路径上。然后我发现添加路径的方式对于我们使用OS模块添加路径的通用目的是错误的。
Example:
例子:
import os
final_path = os.path.join(path_1,path_2,path_3......)
working in Ubuntu and Windows
在 Ubuntu 和 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 :)
该代码在 Windows 和 Ubuntu 上都可以正常工作:)
回答by SUJITKUMAR SINGH
FOR MAC USERS if you are working with open cv
对于 MAC 用户,如果您正在使用 open cv
import cv2
cv2.imwrite('path_to_folder/image.jpg',image)
回答by Sharat Chandra
You can use this simple code in loop by incrementing count
您可以通过增加计数在循环中使用这个简单的代码
cv2.imwrite("C:\Sharat\Python\Images\frame%d.jpg" % count, image)
cv2.imwrite("C:\Sharat\Python\Images\frame%d.jpg" % count, image)
images will be saved in the folder by name line frame0.jpg, frame1.jpg frame2.jpg etc..
图像将按名称行frame0.jpg、frame1.jpg frame2.jpg等保存在文件夹中。