Skip to main content
 首页 » 编程设计

Python pygame奇数序列

2025年05月04日15lhb25

在我当前的 Python 脚本中,我让它从 3 倒数到 1,然后拍照,您可以从下面的这段代码中解释,这样做 4 次。但是,它似乎在倒计时前偷偷插入了一张图片,而将最后一张留在了外面,所以最后倒计时似乎无关紧要。

谁能告诉我这是为什么?

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 

请您参考如下方法:

当 capture_continuous 方法在该点执行时,您的代码将在循环的最开始拍照。

然后您的代码将运行倒计时并重新开始循环,此时它会拍摄另一张照片。

你的循环真正做的只是:

  • 拍照
  • 倒计时
  • 重复

你希望它是:

  • 倒计时
  • 拍照
  • 重复

因此,您可以将循环的开始更改为:

for i in range(total_pics): 

删除代码末尾的 if 部分(因为这现在由 for 循环处理)并插入一行以在倒计时后拍照。假设这是 rasperry pi 相机,那么该行将是:

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

我对picamera模块不熟悉,可能你是这样操作的:

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