This guide is an introduction to the Picamera2 Python library for the Raspberry Pi board. It covers how to install Picamera2, take photos, and record video to an .mp4 file. This guide is compatible with the Raspberry Pi Camera V2 and V3.
Table of Contents
This guide covers the following topics:
- Picamera2 Python Library
- Preparing the Raspberry Pi Camera
- Picamera2 Take Photo Example – Python Script
- Picamera2 Record Video Example – Python Script
Prerequisites
Before proceeding, make sure you check the following prerequisites:
- You need a Raspberry Pi board and a Raspberry Pi camera
- You should have a Raspberry Pi running Raspberry Pi OS (32-bit or 64-bit).
- You should be able to establish an SSH connection with your Raspberry Pi.
Picamera2 Python Library
Picamera2 is a Python library for interacting with the Raspberry Pi’s camera. It is based on the libcamera camera stack and it is maintained by the Raspberry Pi foundation. It’s no longer recommended to use the older PiCamera library with the latest Raspberry Pi OS versions.
The Picamera2 library is supported on all Raspberry Pi models from the Pi Zero to the RPi 5.
Installing Picamera2 Library
Having an SSH connection established with your Raspberry Pi, update and upgrade your Raspberry Pi, if any updates are available. Run the following command:
sudo apt update && sudo apt upgrade -y
Run the next command to install the Picamera2 library in your Raspberry Pi.
sudo apt install -y python3-picamera2
It is strongly recommended to install and update Picamera2 using the apt command described earlier which will avoid compatibility problems. I’ve encountered many compilation issues while trying to install the Picamera2 library with the pip command on a virtual environment.
Preparing the Raspberry Pi Camera
The Raspberry Pi camera is a small and low-cost camera module compatible with the Raspberry Pi boards. Even though it can be good enough for most projects, some USB cameras will provide better image quality. For this guide, we’ll be using the Raspberry Pi Camera V2 module shown in the following picture:
This guide also works with the Raspberry Pi Camera V3 and the camera is compatible with all Raspberry Pi models.
Enable the Raspberry Pi Camera Module
If you are running the latest version of Raspberry Pi OS, the official Raspberry Pi cameras will be detected and enabled automatically.
Connect the camera
Connecting the Raspberry Pi Camera Module is very straightforward. With the Pi shutdown, connect the camera to the Pi CSI port as shown in the following figure.
Picamera2 Take Photo Example – Python Script
Taking a photo with the Raspberry Pi camera is simple thanks to the Picamera2 Python library. Create a new file called take_photo.py:
nano take_photo.py
Copy the following code to your newly created file:
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-picamera2-python/
from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
camera_config = picam2.create_preview_configuration()
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(2)
picam2.capture_file("test_photo.jpg")
Press Ctrl+X to save your file, type Y and Enter.
Take Photo Script
Let’s take a quick look at how the code works.
Start by importing the required libraries.
from picamera2 import Picamera2, Preview
import time
Create a Picamera2() object called picam2.
picam2 = Picamera2()
Then, generate a camera configuration suitable for preview and configure the camera system with that preview configuration.
camera_config = picam2.create_preview_configuration()
picam2.configure(camera_config)
Start the preview window.
picam2.start_preview(Preview.QTGL)
Finally, start the camera, wait for two seconds, and take a picture. It will be stored with the filename test_photo.jpg.
picam2.start()
time.sleep(2)
picam2.capture_file("test_photo.jpg")
Running the Script
Run your script to take a photo by running the following command on your project directory:
python take_photo.py
It takes a photo with the Raspberry Pi Camera and saves it with the test_photo.jpg name. The image file will be saved in the same folder as the Python script.
You can access your Raspberry Pi Desktop remotely and open the image file to take a look at the picture.
Picamera2 Record Video Example – Python Script
The next Python script also uses the PiCamera package to capture video to an .mp4 file. Create a new file called record_video.py:
nano capture_video.py
Copy the following code to your newly created file:
# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-picamera2-python/
from picamera2 import Picamera2
picam2 = Picamera2()
picam2.start_and_record_video("test_video.mp4", duration=5)
Press Ctrl+X to save your file, type Y and Enter.
Capture Video Script
Let’s take a quick look at the script.
Start by importing the required libraries.
from picamera2 import Picamera2
Create a Picamera2() object.
picam2 = Picamera2()
The next command records video for 5 seconds with the Raspberry Pi Camera and saves it with the test_video.mp4 name. You can modify the script to change the file name and extend the video recording duration.
picam2.start_and_record_video("test_video.mp4", duration=5)
Running the Script
Run the script to record a video. You can use the following command:
python3 capture_video.py
It will capture a 5-second video with the Raspberry Pi Camera and will save it with the test_video.mp4 name.
You can access your Raspberry Pi Desktop remotely and open the video file with the VLC player to watch it.
Troubleshooting
- If you are using a Raspberry Pi 3 or an older device, you’ll need to enable Glamor for this example script. To do this, run sudo raspi-config in a command window, choose Advanced Options, and then enable Glamor graphic acceleration. Finally, reboot your device.
- If you are using a Remote Desktop Connection, sometimes the image preview and video recording will show blurred like the screenshots below. However, if you download the files to your regular Windows PC or Mac OS, the files are fine.
Related content: Transfer Files to and from Raspberry Pi using FileZilla FTP (Windows PC)
Wrapping Up
This post was a quick introduction guide to the Raspberry Pi Camera with the Picamera2 Python library. You learned how to take photos and record video into a file.
We have other Raspberry Pi Projects that you may find useful:
- Install OpenCV on a Raspberry Pi (compatible with all RPi boards)
- Set Up USB Camera for OpenCV Projects with Raspberry Pi
- Install MediaPipe on a Raspberry Pi – Example Gesture Recognition
- Raspberry Pi: Control Outputs Based on Time of the Day (Python)
- Raspberry Pi: Send an Email using Python (SMTP Server)
- Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?
- All our Raspberry Pi Projects and Guides
We hope you’ve found this post useful.
Thanks for reading!
I installed this on a Raspberry Pi Zero W. When I ran ‘python take_photo.py’ I got this error: qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin “xcb” in “” even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.
Sorry to have bothered you with my previous comment. I did what I should have done–I read the appropriate section of the picamera2 manual, Section 3.3. Starting and stopping previews.
“For example, the following script would start the camera system running, run for a short while, and then attempt to autodetect which preview window to use in order actually to start displaying the images:
I made the changes and this was the result:
[0:19:00.527456235] [1158] INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:19:00.727789181] [1159] INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media0
[0:19:00.736081592] [1159] INFO RPI pipeline_base.cpp:1101 Using configuration file ‘/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml’
[0:19:00.799536736] [1158] INFO Camera camera.cpp:1033 configuring streams: (0) 640×480-XBGR8888 (1) 640×480-SGBRG10_CSI2P
[0:19:00.803759946] [1159] INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 – Selected sensor format: 640×480-SGBRG10_1X10 – Selected unicam format: 640×480-pGAA