magu_
magu_

Reputation: 4856

get numpy array from pygame

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?

Upvotes: 5

Views: 2036

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114781

get_image returns a Surface. According to http://www.pygame.org/docs/ref/surfarray.html, you can use pygame.surfarray.array2d (or one of the other functions in the surfarray module) to convert the Surface to a numpy array. E.g.

    img = webcam.get_image()
    data = pygame.surfarray.array2d(img)

Upvotes: 5

Related Questions