Paws/PythonScripts/webcam.py
Aroy-Art 725feb5d9a
All checks were successful
/ test (push) Successful in 1m53s
Add simple script to take an image with webcam and python
2024-02-23 12:25:11 +01:00

27 lines
619 B
Python

import cv2
def take_picture(camera_index=0, file_name='captured_image.jpg'):
# Open a connection to the camera
cap = cv2.VideoCapture(camera_index)
if not cap.isOpened():
print("Error: Could not open camera.")
return
# Capture a single frame
ret, frame = cap.read()
if not ret:
print("Error: Could not capture a frame.")
cap.release()
return
# Save the captured frame to a file
cv2.imwrite(file_name, frame)
# Release the camera
cap.release()
print(f"Picture saved as {file_name}")
if __name__ == "__main__":
take_picture()