~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to examples/camera.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# 1. Basic image capturing and displaying using the camera module
 
4
 
 
5
import pygame
 
6
import pygame.camera
 
7
from pygame.locals import *
 
8
 
 
9
 
 
10
class VideoCapturePlayer(object):
 
11
 
 
12
   size = ( 640, 480 )
 
13
   def __init__(self, **argd):
 
14
       self.__dict__.update(**argd)
 
15
       super(VideoCapturePlayer, self).__init__(**argd)
 
16
 
 
17
       # create a display surface. standard pygame stuff
 
18
       self.display = pygame.display.set_mode( self.size, 0 )
 
19
 
 
20
       # gets a list of available cameras.
 
21
       self.clist = pygame.camera.list_cameras()
 
22
       if not self.clist:
 
23
           raise ValueError("Sorry, no cameras detected.")
 
24
 
 
25
       # creates the camera of the specified size and in RGB colorspace
 
26
       self.camera = pygame.camera.Camera(self.clist[0], self.size, "RGB")
 
27
 
 
28
       # starts the camera
 
29
       self.camera.start()
 
30
 
 
31
       self.clock = pygame.time.Clock()
 
32
 
 
33
       # create a surface to capture to.  for performance purposes, you want the
 
34
       # bit depth to be the same as that of the display surface.
 
35
       self.snapshot = pygame.surface.Surface(self.size, 0, self.display)
 
36
 
 
37
   def get_and_flip(self):
 
38
       # if you don't want to tie the framerate to the camera, you can check and
 
39
       # see if the camera has an image ready.  note that while this works
 
40
       # on most cameras, some will never return true.
 
41
       if 0 and self.camera.query_image():
 
42
           # capture an image
 
43
 
 
44
           self.snapshot = self.camera.get_image(self.snapshot)
 
45
       self.snapshot = self.camera.get_image(self.snapshot)
 
46
       #self.snapshot = self.camera.get_image()
 
47
 
 
48
       # blit it to the display surface.  simple!
 
49
       self.display.blit(self.snapshot, (0,0))
 
50
       pygame.display.flip()
 
51
 
 
52
   def main(self):
 
53
       going = True
 
54
       while going:
 
55
           events = pygame.event.get()
 
56
           for e in events:
 
57
               if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
 
58
                   going = False
 
59
 
 
60
           self.get_and_flip()
 
61
           self.clock.tick()
 
62
           print (self.clock.get_fps())
 
63
 
 
64
def main():
 
65
    pygame.init()
 
66
    pygame.camera.init()
 
67
    VideoCapturePlayer().main()
 
68
    pygame.quit()
 
69
 
 
70
if __name__ == '__main__':
 
71
    main()