我想从笔记本电脑相机中拍摄一张简单的照片。我用pygame
编写了一个简单的程序,它运行正常,但我的问题是当我尝试捕获相机的LED指示灯打开时,并且在我的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
答案 0 :(得分: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