How to use PyGame In Python
PyGame.camera() camera initializer supports only Linux operating system and Currently, It is not compatible with Windows. To install PyGame in Linux, Enter the below command on the Linux terminal.
Approach:
1. Import pygame.camera module
2. Initialize the camera using the camera.init() method.
Python3
pygame.camera.init() |
3. Detect all available cameras using the list_cameras() method.
Python3
camlist = pygame.camera.list_cameras() |
4. check if the camera is detected or not
Syntax:
if camlist: # Initialize and start camera cam = pygame.camera.Camera(camlist[0], (640, 480)) cam.start() # capturing the single image image = cam.get_image() # saving the image pygame.image.save(image, "filename.jpg") else: if camera is not detected the moving to this part
Example:
Python3
# Python program to capture a single image # using pygame library # importing the pygame library import pygame import pygame.camera # initializing the camera pygame.camera.init() # make the list of all available cameras camlist = pygame.camera.list_cameras() # if camera is detected or not if camlist: # initializing the cam variable with default camera cam = pygame.camera.Camera(camlist[ 0 ], ( 640 , 480 )) # opening the camera cam.start() # capturing the single image image = cam.get_image() # saving the image pygame.image.save(image, "filename.jpg" ) # if camera is not detected the moving to else part else : print ( "No camera on current device" ) |
Output:
How to capture a image from webcam in Python?
In this article, we will discuss how to capture an image from the webcam using Python.
Contact Us