4

I want to access my Webcam via python. Unfortunately openCV is not working because of the webcam. Pygame.camera works like a charm with this code:

from pygame import camera,display

camera.init()
webcam = camera.Camera(camera.list_cameras()[0])
webcam.start()

img = webcam.get_image()

screen = display.set_mode((img.get_width(), img.get_height()))
display.set_caption("cam")

while True:
    screen.blit(img, (0,0))
    display.flip()   
    img = webcam.get_image()

My question is now, how can I get a numpy array from the webcam?

4

1 回答 1

5

get_image返回一个Surface。根据http://www.pygame.org/docs/ref/surfarray.html,您可以使用pygame.surfarray.array2d(或模块中的其他函数之一surfarray)将 Surface 转换为 numpy 数组。例如

    img = webcam.get_image()
    data = pygame.surfarray.array2d(img)
于 2013-11-14T17:15:43.247 回答