This is my code, which starts the webcam :
JavaScript
x
36
36
1
import pygame.camera
2
import pygame.image
3
import sys
4
5
pygame.camera.init()
6
7
cameras = pygame.camera.list_cameras()
8
9
print "Using camera %s ..." % cameras[0]
10
11
webcam = pygame.camera.Camera(cameras[0])
12
13
webcam.start()
14
15
# grab first frame
16
img = webcam.get_image()
17
18
WIDTH = img.get_width()
19
HEIGHT = img.get_height()
20
21
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
22
pygame.display.set_caption("pyGame Camera View")
23
24
while True :
25
for e in pygame.event.get() :
26
if e.type == pygame.QUIT :
27
sys.exit()
28
29
30
31
# draw frame
32
screen.blit(img, (0,0))
33
pygame.display.flip()
34
# grab next frame
35
img = webcam.get_image()
36
I want to know how to capture an image and store it to the current directory. Please suggest the changes required.
Advertisement
Answer
When you call webcam.get_image it returns an RGB Surface. So just call pygame.image.save(), the file type is determined by the extension and defaults to TGA. Options are BMP, TGA, PNG, and JPEG. In this case you could append this line to your file.
JavaScript
1
2
1
pygame.image.save(img, "image.jpg")
2
Check out http://www.pygame.org/docs/ref/image.html and http://www.pygame.org/docs/ref/camera.html