2012-05-26 58 views
-1

我是新来的,这是我的第一篇文章。我在VideoCapture中遇到问题:我想要在“for”功能中自动更改照片的名称,并将照片保存在我选择的目录中,但我无法弄清楚我必须做什么并没有在互联网上找到它。如果有人知道,请帮助我。这里是一个命令行的例子:VideoCapture文件名

from VideoCapture import Device 
cam = Device() 
cam.saveSnapshot('here i want to call a variable, but i don't know how.jpg') 

所以就是这样。我也不知道我在哪里写目录。

感谢

+0

我认为'C++'的标签被错误添加。如果我错了,请加回去。 – bernie

回答

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虽然...

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)) 
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 

就是这样,这应该做到你想要的。

相关问题