【问题标题】:VideoCapture filename视频捕捉文件名
【发布时间】:2012-05-26 00:58:47
【问题描述】:

我是新来的,这是我的第一篇文章。我在 VideoCapture 中遇到问题:我希望在“for”函数中自动更改我拍摄的图片名称并将图片保存在我选择的目录中,但我不知道我必须做什么并没有在互联网上找到它。如果有人知道,请帮助我。以下是命令行示例:

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

原来如此。我也不知道该在哪里写目录。

谢谢

【问题讨论】:

  • 我认为c++标签添加错误。如果我记错了,请重新添加。

标签: python python-2.7 python-imaging-library video-capture


【解决方案1】:

在 C++ 中,您通常会将字符串与 ostringstream 组合在一起,如下所示:

std::ostringstream filename;

for (int i=0; i<10; i++) {
    filename << "variable: " << i << ".jpg";
    cam.saveSnapshot(filename.str().c_str());
}

目前尚不完全清楚您使用 C++(如果有的话)做什么以及使用 Python 做什么......

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      好的,这很容易。

      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
      

      就是这样,它应该完全符合你的要求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多