From 725feb5d9aac9e85519d6dd1cd216f5a5f852a9f Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Fri, 23 Feb 2024 12:25:11 +0100 Subject: [PATCH] Add simple script to take an image with webcam and python --- PythonScripts/webcam.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 PythonScripts/webcam.py diff --git a/PythonScripts/webcam.py b/PythonScripts/webcam.py new file mode 100644 index 0000000..d53f704 --- /dev/null +++ b/PythonScripts/webcam.py @@ -0,0 +1,27 @@ +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()