gpt4 book ai didi

python - Python 中每次保存的文件命名计数

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:14 24 4
gpt4 key购买 nike

我想知道如何告诉我的 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)

最佳答案

试试这个:

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)

关于python - Python 中每次保存的文件命名计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47217105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com