我是新来的,这是我的第一篇文章。我在VideoCapture中遇到了问题:我希望在“for”功能中自动更改图片的名称,并将图片保存在我选择的目录中,但我无法弄清楚我要做什么并没有在互联网上找到它。如果有人知道,请帮助我。以下是命令行的示例:
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('here i want to call a variable, but i don't know how.jpg')
就是这样。我也不知道在哪里写这个目录。
由于
答案 0 :(得分:0)
在C ++中,您通常会将字符串与ostringstream
组合在一起,如下所示:
std::ostringstream filename;
for (int i=0; i<10; i++) {
filename << "variable: " << i << ".jpg";
cam.saveSnapshot(filename.str().c_str());
}
目前还不完全清楚你在用C ++做什么部分(如果有的话)以及用Python做什么......
答案 1 :(得分:0)
saveSnapshot()
方法作为第一个非self
参数,采用文件名。
参考:http://videocapture.sourceforge.net/html/VideoCapture.html
这意味着你可以这样做:
import os
from VideoCapture import Device
save_dir = '/path/to/my/img/dir' # or for windows: `c:/path/to/my/img/dir`
img_file_name = 'an_image.jpg'
cam = Device()
cam.saveSnapshot( os.path.join(save_dir, img_file_name) )
答案 2 :(得分:0)
好的,这很简单。
from VideoCapture import Device
from os.path import join, exists
# We need to import these two functions so that we can determine if
# a file exists.
import time
# From your question I'm implying that you want to execute the snapshot
# every few seconds or minutes so we need the time.sleep function.
cam = Device()
# First since you want to set a variable to hold the directory into which we
# will be saving the snapshots.
snapshotDirectory = "/tmp/" # This assumes your on linux, change it to any
# directory which exists.
# I'm going to use a while loop because it's easier...
# initialize a counter for image1.jpg, image2.jpg, etc...
counter = 0
# Set an amount of time to sleep!
SLEEP_INTERVAL = 60 * 5 # 5 minutes!
while True:
snapshotFile = join(snapshotDirectory, "image%i.jpg" % counter)
# This creates a string with the path "/tmp/image0.jpg"
if not exists(snapshotFile):
cam.saveSnapshot(snapshotFile)
time.sleep(SNAPSHOT_INTERVAL)
# if the snapshot file does not exist then create a new snapshot and sleep
# for a few minutes.
#finally increment the counter.
counter = counter + 1
就是这样,那应该完全符合你的要求。