Pygame Tutorial
Selected Reading
- Pygame - Discussion
- Pygame - Useful Resources
- Pygame - Quick Guide
- Pygame - Errors and Exception
- Pygame - PyOpenGL
- Pygame - The Sprite Module
- Pygame - Access CDROM
- Pygame - Load cursor
- Pygame - Using Camera module
- Pygame - Playing Movie
- Pygame - Playing music
- Pygame - Mixer channels
- Pygame - Sound objects
- Pygame - Transforming Images
- Pygame - Use Text as Buttons
- Pygame - Moving Rectangular objects
- Pygame - Moving with mouse
- Pygame - Moving with Numeric pad keys
- Pygame - Moving an image
- Pygame - Displaying Text in Window
- Pygame - Loading image
- Pygame - Drawing shapes
- Pygame - Mouse events
- Pygame - Keyboard events
- Pygame - Event objects
- Pygame - Color object
- Pygame - Locals module
- Pygame - Display modes
- Pygame - Hello World
- Pygame - Overview
- Pygame - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Pygame - Using Camera module
Pygame - Using Camera Module
Earper versions of Pygame upto 1.9.6 contain pygame.camera module. This module contains functionapty to capture camera feed on the game window and grab an image from it. The camera devices available to the system are enumerated in a pst returned by pst_cameras() method.
pygame.camera.pst_cameras()
To initiapze a camera object, use camera id, resolution and format arguments.
pygame.camera.Camera(device, (width, height), format)
The default format is RGB. Width and height parameters are by default 640x480.
The camera module has following methods defined in Camera class.
pygame.camera.Camera.start() | opens, initiapzes, and starts capturing |
pygame.camera.Camera.stop() | stops, uninitiapzes, and closes the camera |
pygame.camera.Camera.get_controls() | gets current values of user controls |
pygame.camera.Camera.set_controls() | changes camera settings if supported by the camera |
pygame.camera.Camera.get_size() | returns the dimensions of the images being recorded |
pygame.camera.Camera.query_image() | checks if a frame is ready |
pygame.camera.Camera.get_image() | captures an image as a Surface |
pygame.camera.Camera.get_raw() | returns an unmodified image as a string |
Example
Following programs captures pve feed from computer’s default web camera.
import pygame import pygame.camera pygame.init() gameDisplay = pygame.display.set_mode((640,480)) pygame.camera.init() print (pygame.camera.pst_cameras()) cam = pygame.camera.Camera(0) cam.start() while True: img = cam.get_image() gameDisplay.bpt(img,(0,0)) pygame.display.update() for event in pygame.event.get() : if event.type == pygame.QUIT : cam.stop() pygame.quit() exit()
Please note that on Windows OS, you may have to install Videocapture module.
pip3 install VideoCapture