【问题标题】:File naming count on each save in PythonPython中每次保存的文件命名计数
【发布时间】:2018-04-23 08:20:43
【问题描述】:

我想知道如何告诉我的 py 脚本循环重命名每个保存的图像,而不是覆盖它们。如您所见,它每 30 秒保存为 image_01.png。

import time
import cv2
import sys
import sched, time

try:
    def DoWork():
            print('Capturing a Picture')
            camera_port = 0
            camera = cv2.VideoCapture(camera_port)
            time.sleep(0.1)  
            return_value, image = camera.read()
            cv2.imwrite("/Users/pnovak/Desktop/image_01.png", image)
            del(camera)   
            def countdown(t):
                while t:
                    mins, secs = divmod(t, 60)
                    timeformat = '{:02d}:{:02d}'.format(mins, secs)
                    sys.stdout.write('Next capture in '+timeformat+'\r')
                    sys.stdout.flush()
                    time.sleep(1)
                    t -= 1
                pass
            countdown(30)
            print
    while True:
            DoWork()
except KeyboardInterrupt:
    print
    print('User CTRL+C')
    sys.exit(0)

【问题讨论】:

  • 只是为它做一个计数器......
  • 我不太清楚如何,我还是python新手,几天前开始学习它

标签: python linux macos python-2.7 rename


【解决方案1】:

试试这个:

import time
import cv2
import sys
import sched, time

try:
    def DoWork(get_name):
            print('Capturing a Picture')
            camera_port = 0
            camera = cv2.VideoCapture(camera_port)
            time.sleep(0.1)  
            return_value, image = camera.read()
            file_name = "/Users/pnovak/Desktop/image_{0}.png".format(get_name) # new line
            cv2.imwrite(file_name, image)
            del(camera)   
            def countdown(t):
                while t:
                    mins, secs = divmod(t, 60)
                    timeformat = '{:02d}:{:02d}'.format(mins, secs)
                    sys.stdout.write('Next capture in '+timeformat+'\r')
                    sys.stdout.flush()
                    time.sleep(1)
                    t -= 1
                pass
            countdown(30)
            print
    counter = 0 # new line
    while True:
            DoWork(counter)
            counter = counter + 1 # new line
except KeyboardInterrupt:
    print
    print('User CTRL+C')
    sys.exit(0)

【讨论】:

  • @Petr:请问你可以打勾吗? :)
  • 我一看到这个就已经这样做了 :) 它的说法是,声望低于 15 的投票会被记录下来。有人否决了我的问题,所以我现在不能:/
  • 现在可以打勾了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多