Pygame Webcam Support

Update: Grab the up to date code in Pygame >1.9.1 from the Pygame repository.

There was a three day weekend here, so I got some real coding done on my GSoC project.

You can pull the code I’m working on from my repository at;a=summary

It is currently barely alpha quality, but it supports v4l2 cameras that use MMAP and YUYV or RGB24 pixelformats. That means it’ll work on the OLPC XO, though you’ll need to install gcc, SDL-devel, and probably some other stuff to compile it.

You can open a camera with:
cam = camera.Camera(device, (width, height))
Device in most cases is going to be "/dev/video0". (width, height) will probably be (640, 480).
You then need to start the camera with cam.start().
cam.get_image() will return an RGB Surface.
cam.close() closes the camera if necessary.

Related Posts

6 Replies to “Pygame Webcam Support”

  1. Hi, really glad someone has picked this up for GSOC. I’m working on a game design system built on pygame ( – so I’ve been hoping someone would start this. (we’re going to port all exported games to the olpc, and the entire system eventually, but I’m in Europe so couldn’t get my hands on one during the first phase of G1G1)

    As far as my use cases are concerned, the most important thing is to have a simple, device independent, webcam api to use from standard pygame (with the same api in the OLPC pygame derivative).

    The first use case for me would just be to let people take snapshots of themselves to add to high score tables, or to let them create sprites direct from the camera.

    Beyond that, motion detection would be cool (complicated use case would be a tennis game where you can hit the ball with your hands)

    I’d not want to rely on opencv, though – especially as part of pygame – it’s a fairly large overhead (dispite the benefits).

    I’d love to have your camera interface added to the main pygame trunk as soon as it’s stable (don’t know who manages the pygame trunk, though). Good luck over the summer.

  2. I was planning on using OpenCV’s Windows capture code, since their goals are fairly similar to mine. Thanks for the link though; I’ll take a look at the DirectShow stuff.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.