Skip to content

Instantly share code, notes, and snippets.

@norpol
Forked from snim2/camerastream.py
Created September 26, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norpol/28c065157873a04f7d744af36cef6898 to your computer and use it in GitHub Desktop.
Save norpol/28c065157873a04f7d744af36cef6898 to your computer and use it in GitHub Desktop.
Display the output of a webcam using Python and Pygame
import pygame
import pygame.camera
from pygame.locals import *
DEVICE = '/dev/video0'
SIZE = (640, 480)
FILENAME = 'capture.png'
def camstream():
pygame.init()
pygame.camera.init()
display = pygame.display.set_mode(SIZE, 0)
camera = pygame.camera.Camera(DEVICE, SIZE)
camera.start()
screen = pygame.surface.Surface(SIZE, 0, display)
capture = True
while capture:
screen = camera.get_image(screen)
display.blit(screen, (0,0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == QUIT:
capture = False
elif event.type == KEYDOWN and event.key == K_s:
pygame.image.save(screen, FILENAME)
camera.stop()
pygame.quit()
return
if __name__ == '__main__':
camstream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment