user5095342
user5095342

Reputation:

Python pygame odd sequence

On my current Python script I have it counting down from 3 to 1, then taking a picture as you can intepret from this code below, doing this 4 times. However, it seems to sneak in 1 picture before counting down, and leaving the last one out, so the last countdown doesn't seem to matter.

Can anyone tell me why this is?

def start_photobooth():
    import config
    now = time.strftime("%d-%m-%Y-%H:%M:%S")
    try:
        for i, filename in enumerate(camera.capture_continuous(config.images_path + now + '-' + '{counter:02d}.jpg')):
            print "[" + current_time() + "] [PHOTO] Producing " + filename
            surface.fill((0,0,0,0))
            surface.set_alpha(288)
            textSurfaceObj = fontObj.render('', True, red)
            surface.blit(textSurfaceObj, textRectObj)
            pygame.display.update()
            time.sleep(float(config.countdown)) 
            for y in range(3,0,-1):
                surface.fill((0,0,0,0))
                surface.set_alpha(288)
                textSurfaceObj = fontObj.render(str(y), True, red)
                surface.blit(textSurfaceObj, textRectObj)
                pygame.display.update()
                pygame.time.wait(1000)
            if i == total_pics-1:           
                break

Upvotes: 2

Views: 82

Answers (1)

elParaguayo
elParaguayo

Reputation: 1318

Your code will take a picture at the very beginning of your loop as the capture_continuous method is executed at that point.

Your code will then run its countdown and restart the loop at which point it takes another photo.

What your loop is really doing is just:

  • Take picture
  • Countdown
  • Repeat

You want it to be:

  • Countdown
  • Take picture
  • Repeat

You could therefore change the start of your loop to:

for i in range(total_pics):

remove the if section at the end of your code (as this is handled by the for loop now) and insert a line to take the photo after the countdown. Assuming this is rasperry pi camera then the line would be:

filename = camera.capture("{0}{1}-{2:02d}.jpg".format(config.images_path,now,i))

I'm not familiar with the picamera module, so it may be that you do it this way:

filename = "{0}{1}-{2:02d}.jpg".format(config.images_path,now,i)
camera.capture(filename)

Upvotes: 1

Related Questions