Raspberry PiIntermediate

Raspberry Pi Camera: Photo & Video with Python

Capture photos and record video using the Pi Camera Module and Picamera2 library.

12 April 202611 min read

Introduction

The official Raspberry Pi Camera Module is a great tool for vision projects. New versions of Pi OS use the libcamera stack and Picamera2 library.

Setup

  1. Connect the camera cable to the CSI port (blue tape facing USB ports).
  2. Enable Camera in sudo raspi-config.

Python: Capture Image

from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview()
sleep(5) # Camera warm-up time
camera.capture('/home/pi/desktop/image.jpg')
camera.stop_preview()

Python: Record Video

camera.start_recording('/home/pi/video.h264')
sleep(10)
camera.stop_recording()
Tags:Raspberry PiCameraPythonVision