Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (2024)

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.

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (1)

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 anSSH 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:

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (2)

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.

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (3)

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, Previewimport timepicam2 = 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")

View raw code

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, Previewimport 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
Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (4)

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.

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (5)

You can access your Raspberry Pi Desktop remotely and open the image file to take a look at the picture.

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (6)

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 Picamera2picam2 = Picamera2()picam2.start_and_record_video("test_video.mp4", duration=5)

View raw code

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
Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (7)

It will capture a 5-second video with the Raspberry Pi Camera and will save it with the test_video.mp4 name.

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (8)

You can access your Raspberry Pi Desktop remotely and open the video file with the VLC player to watch it.

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (9)

Troubleshooting

  1. 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.
  2. 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.
Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (10)

Related content: Transfer Files to and from Raspberry Pi using FileZilla FTP (Windows PC)

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (11)

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!

Set Up Python Picamera2 on a Raspberry Pi | Random Nerd Tutorials (2024)
Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5856

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.