5

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?

1
  • I'm having trouble making the jump from webcam to numpy array - are you trying to have an array of pixels from the image? Commented Nov 14, 2013 at 17:05

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.