OpenCV - 将图像保存到选择的特定文件夹 [英] OpenCV - Saving images to a particular folder of choice
问题描述
我正在学习 OpenCV 和 Python.我从我的网络摄像头捕获了一些图像并保存了它们.但它们默认保存到本地文件夹中.我想将它们从直接路径保存到另一个文件夹.我如何解决它?
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?
推荐答案
ebeneditos 提供的解决方案完美运行.
The solution provided by ebeneditos works perfectly.
但是如果你在一个大代码片段的几个部分有 cv2.imwrite()
并且你想更改保存图像的路径,你将不得不在每次出现时更改路径cv2.imwrite()
单独.
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.
正如 Soltius 所说,这是一个更好的方法.声明一个路径并将其作为字符串传递到 cv2.imwrite()
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)
现在如果你想修改路径,你只需要改变path
变量即可.
Now if you want to modify the path, you just have to change the path
variable.
根据 Kallz 提供的解决方案编辑
Edited based on solution provided by Kallz
这篇关于OpenCV - 将图像保存到选择的特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!