2017-08-27 37 views
0

我想從筆記本相機捕捉簡單的圖像。我用pygame編寫了一個簡單的程序,它工作正常,但是我的問題是,當我試圖捕獲照相機的led燈打開的圖像時,直到我的Python代碼達到結束時才關閉。這很煩人,因爲用戶從不知道什麼時候拍照。 這裏是我的示例代碼:使用python捕捉圖像時的相機光線

import pygame 
import pygame.camera 
import time 
import sys 
pygame.init() 
pygame.camera.init() 

camlist = pygame.camera.list_cameras() 
cam = pygame.camera.Camera(camlist[0],(640,480)) 
cam.start() 
time.sleep(0.1) 
img = cam.get_image() 
pygame.image.save(img,"out.jpg") 
cam.stop() 
print("camera stopped!") 
time.sleep(10) #the light is still on 
+1

不能保證這會工作,但嘗試在您停止相機後,您可以使用「del cam」。當物體存在時,它可能會保持燈光亮着。 – kindall

回答

0

由於@kindall答案,我不得不刪除對象,這裏是代碼的對象:

class My_Camera: 
    def __init__(self,camshot_file): 
     self.other=others() 
     self.camshot_file=self.other.resource_path(camshot_file) 
     pygame.init() 
     pygame.camera.init() 
     self.camlist = pygame.camera.list_cameras() 

    def grab(self): 
     try: 
      self.cam = pygame.camera.Camera(self.camlist[0],(640,480)) 
      self.cam.start() 
      sleep(0.1) 
      img = self.cam.get_image() 
      pygame.image.save(img,self.camshot_file) 
      self.cam.stop() 
      return True 
     except: 
      return False 
    def stop(self): 
     del self.cam 
相關問題