English 中文(简体)
Pygame - Using Camera module
  • 时间:2024-11-03

Pygame - Using Camera Module


Previous Page Next Page  

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

Output

Videocapture Advertisements