How to capture a image from webcam in Python? - GeeksforGeeks

21 Dec, 2021
In this article, we will discuss how to capture an image from the webcam using Python.
We will use OpenCV and PyGame libraries. Both libraries include various methods and functions to capture an image and video also. By using, these vast libraries we need to write only 4 to 5 lines of code to capture an image.
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
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("GeeksForGeeks", image)
    
    # save the image
    imwrite("GeeksForGeeks.png", image)

else:
    Move to this part is input image has some error

Python3
# program to capture single image from webcam in python  # importing OpenCV libraryfrom cv2 import *  # initialize the camera# If you have multiple camera connected with # current device, assign a value in cam_port # variable according to thatcam_port = 0cam = VideoCapture(cam_port)  # reading the input using the cameraresult, image = cam.read()  # If image will detected without any error, # show resultif result:      # showing result, it take frame name and image     # output    imshow("GeeksForGeeks", image)      # saving image in local storage    imwrite("GeeksForGeeks.png", image)      # If keyboard interrupt occurs, destroy image     # window    waitKey(0)    destroyWindow("GeeksForGeeks")  # If captured image is corrupted, moving to else partelse:    print("No image detected. Please! try again")
Output:
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.
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
Python3
# Python program to capture a single image# using pygame library  # importing the pygame libraryimport pygameimport pygame.camera  # initializing  the camerapygame.camera.init()  # make the list of all available camerascamlist = pygame.camera.list_cameras()  # if camera is detected or notif 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 partelse:    print("No camera on current device")
Output:
rajeev0719singh
adnanirshad158
Python-OpenCV
Python-PyGame
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()