我正在尝试将相机的屏幕截图保存到文件中。我在 Windows 8 上使用 Python 2.7 32 位和 pygame 1.9 32 位。我花了一段时间谷歌搜索这个问题,但在网上找不到太多。
这是我的代码:
import pygame
import pygame.camera
import sys
pygame.camera.init()
pygame.display.set_mode((800, 600))
cameraCount = len(pygame.camera.list_cameras())
if cameraCount < 1:
print "No cameras found!"
raw_input()
sys.exit(0)
x = 0
if cameraCount > 1:
print "Multiple cameras found, choose a number 0 through ", cameraCount
x = -1
while int(x) > cameraCount or int(x) < 0:
x = raw_input()
cam = pygame.camera.Camera(x)
cam.start()
snapshot = cam.get_image()
pygame.image.save(snapshot, "testing.png")
print "done!"
raw_input()
这是我收到的错误:
Traceback (most recent call last):
File "C:\Users\Cody\Desktop\Python\cameraa.py", line 21, in <module>
cam = pygame.camera.Camera(x)
File "C:\Python27\lib\site-packages\pygame\_camera_vidcapture.py", line 52, in
__init__
self.dev.setresolution(width, height)
vidcap.Error: Cannot set capture resolution.
我找到了我的网络摄像头支持的分辨率列表,并尝试了每一个,但都没有奏效。
任何帮助表示赞赏,谢谢。