Skip to content

Instantly share code, notes, and snippets.

@pdp7
Last active October 2, 2022 01:12
Show Gist options
  • Select an option

  • Save pdp7/1a695f7ed51e47548832c789809b326f to your computer and use it in GitHub Desktop.

Select an option

Save pdp7/1a695f7ed51e47548832c789809b326f to your computer and use it in GitHub Desktop.
How to use pygame to grab frames from you webcam and display the video stream to a window.
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 # draw frame
30 screen.blit(img, (0,0))
31 pygame.display.flip()
32 # grab next frame
33 img = webcam.get_image()
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
overlay = pygame.image.load("overlay.png")
#overlay = pygame.image.load('overlay.bmp')
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
print WIDTH
print HEIGHT
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
screen.blit(overlay,(258,178))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment