Saturday, 11 January 2014

Raspberry Pi - Camera, Python & PiCamera

For my raspberry pi car cam project I needed to find a better way of syncing data with video so I modified raspivid and created python class to run raspivid.  I did this hack mainly because I couldn't be bothered and didn't really have the skill to write an interface to the camera in python, luckily someone else has!

Picamera is a python interface for the raspberry pi camera board, created by Dave Jones (aka waveform80), think of it as the python equivalent of raspivid and raspistill.  Using picamera you can 'programmatically' take videos and images and is much easier way of capturing video/images in your python projects.

There is some great documentation for Picamera at, which includes detailed instructions about how to install picamera, getting started and a complete reference for the module.  You can also find the code at

Install picamera
sudo apt-get install python-picamera

Take a video
The following code will record a video and save it to 'foo.h264'.
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)

Take a picture
The following code will take a single picture and save it to 'foo.jpg'.
import time
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (1024, 768)
    # Camera warm-up time
There is much more information on and its well worth a read.

There are also some great functions such as split recording, capturing images while recording and pulling out data from the encoder while the video is recording (such as frame number).


  1. hii martin when i followed your code im getting error as
    AttributeError: 'module' object has no attribute 'picamera'... plz help

    1. Are you sure picamera has been installed correctly? Did you receive any errors during the installation? Or did this error occur during the install?

    2. sudo apt-get install python-picamera

    3. Thanks for the update tilaprimera, i didnt know it had been added to the raspbian repositories

  2. This comment has been removed by the author.