2017-11-10 12 views
0

내 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) 
+0

단지 그것을 위해 카운터를 만들 ... – DRPK

+0

임 너무 확실하지 않은 방법 임은 새로운 파이썬에 여전히 며칠 전 – Petr

답변

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

@Petr를 배우기 시작 : 않았다 너 내 코드를 시험해? – DRPK

+0

내 질문을 해결해 주셔서 감사합니다 :) – Petr

+0

@ Petr : plz 날 틱 수 있습니다? :) – DRPK