cam_photo.py

Raspberry Pi 4 Model B (2019) — Board Sheet · File : cam_photo.py · Language : Python · Use case : Prendre une photo avec le module caméra officiel et Picamera2

Picamera2 is the modern Raspberry Pi OS camera API: the script sets a still configuration, starts the sensor and saves a timestamped JPEG. Enable the camera (raspi-config or config.txt camera_auto_detect) before the first run on this model.

Hardware

A Raspberry Pi 4 Model B (quad-core BCM2711 at 1.5 GHz, USB-C 5 V/3 A, dual micro-HDMI, 5 GHz Wi-Fi, 40-pin GPIO). One Pi Camera module (v2 IMX219 or HQ), CSI ribbon cable.

Wiring

CSI ribbon into the camera socket (clip up on the 40-pin side), copper contacts facing the pins; see the official docs for the CSI port location.

How it works

Picamera2 talks to libcamera: still configuration, start, asynchronous capture straight to file, then a clean pipeline stop.

Running it

Copy the script to the board (scp), then run it with python3 (or bash). Raspberry Pi OS Bookworm ships gpiozero; otherwise sudo apt install python3-gpiozero. Use a 5 V/3 A USB-C supply to avoid undervoltage throttling.

Variants

create_video_configuration for clips; capture_request_and_convert to get a NumPy array. Use the official 5 V/3 A USB-C supply: otherwise the USB 3 ports are throttled.

cam_photo.py

# cam_photo.py — prend une photo avec la camera officielle (Picamera2).
# Deps: sudo apt install python3-picamera2 ; activer camera en raspi-config
# Sous Bookworm : camera_auto_detect=1 dans /boot/firmware/config.txt.
# Le clip du port CSI se releve, on inserre la nappe, contacts cuivres vers la carte.
from picamera2 import Picamera2
from datetime import datetime

picam2 = Picamera2()
cfg = picam2.create_still_configuration()
picam2.configure(cfg)
picam2.start()
picam2.switch_mode_and_capture_file(
    cfg, datetime.now().strftime("photo_%Y%m%d_%H%M%S.jpg")
)
picam2.stop()
print("Photo enregistrée")

← Back to the board