--- /dev/null
+import os
+
+if os.name == 'posix':
+
+ import Image
+ import cv
+
+ class Camera:
+ def __init__(self):
+ self._cam = cv.CreateCameraCapture(0)
+
+ def get_image(self):
+ im = cv.QueryFrame(self._cam)
+ # Add the line below if you need it (Ubuntu 8.04+)
+ im = cv.GetMat(im)
+ #convert Ipl image to PIL image
+ return Image.fromstring("RGB", cv.GetSize(im), im.tostring())
+
+
+elif os.name in ('ce', 'nt', 'dos'):
+
+ from VideoCapture import Device
+
+ # TODO exception handling
+ class Camera:
+ def __init__(self):
+ self._cam = Device()
+ self._cam.setResolution(640, 480)
+
+ def shot(self):
+ self._cam.saveSnapshot('image.jpg', quality=100)
+
+ def __del__(self):
+ del self._cam
--- /dev/null
+#!/usr/bin/env python
+
+import sys
+
+import pygame
+from pygame.locals import QUIT, KEYDOWN
+import Image
+
+from camera import Camera
+
+
+pygame.init()
+window = pygame.display.set_mode((640,480))
+pygame.display.set_caption("WebCam Demo")
+screen = pygame.display.get_surface()
+
+cam = Camera()
+
+while True:
+ events = pygame.event.get()
+ for event in events:
+ if event.type == QUIT:
+ sys.exit(0)
+ elif event.type == KEYDOWN:
+ im = cam.get_image()
+ pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
+ screen.blit(pg_img, (0,0))
+ pygame.display.flip()