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.
Method 1: Using OpenCV
OpenCV library is compatible with the Linux and windows both operating system. Users need to install the OpenCV library on their local computer using the below command before they go ahead.
Install command - pip install opencv-python
Approach
1. Import OpenCV library
2. Initialize the camera using the VideoCapture() method.
Syntax:
Python3
cam = VideoCapture( 0 ) |
3. Read input using the camera using the cam.read() method.
Syntax:
Python3
result, image = cam.read() |
4. If input image detected without any error, show output
Syntax:
If result: # show the image imshow("w3wiki", image) # save the image imwrite("w3wiki.png", image) else: Move to this part is input image has some error
Example:
Python3
# program to capture single image from webcam in python # importing OpenCV library from cv2 import * # initialize the camera # If you have multiple camera connected with # current device, assign a value in cam_port # variable according to that cam_port = 0 cam = VideoCapture(cam_port) # reading the input using the camera result, image = cam.read() # If image will detected without any error, # show result if result: # showing result, it take frame name and image # output imshow( "w3wiki" , image) # saving image in local storage imwrite( "w3wiki.png" , image) # If keyboard interrupt occurs, destroy image # window waitKey( 0 ) destroyWindow( "w3wiki" ) # If captured image is corrupted, moving to else part else : print ( "No image detected. Please! try again" ) |
Output:
Method 2: Using PyGame
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:
Contact Us