To control the Raspberry Pi camera from Python, you can use the picamera
library, which provides an easy-to-use interface for capturing images and videos. Here's a basic example to get you started:
-
Install the
picamera
library:sudo apt update sudo apt install python3-picamera
-
Capture an image:
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(2) # Allow the camera to adjust to lighting conditions camera.capture('/home/pi/image.jpg') camera.stop_preview()
-
Record a video:
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() camera.start_recording('/home/pi/video.h264') sleep(10) # Record for 10 seconds camera.stop_recording() camera.stop_preview()
These examples demonstrate basic usage. The picamera
library provides many additional features, such as adjusting camera settings, adding overlays, and more. You can refer to the official documentation for further details and advanced functionalities.