Working with the Raspberry Pi camera module

Using the Camera Module within Python Programs

It is often useful to save time and energy by automating commands within a program. The camera board is not supported by an official Python module from the Raspberry Pi Foundation, however, an unofficial Python library has been created for camera support.

Possibly the simplest way to automate the camera (using a Python library that will already be present on your Raspberry Pi) is to use the call function within the subprocess library. You can add that function to a Python program as follows (from subprocess import call):

call (["raspistill -o test.jpeg"], shell=True)
call (["raspivid -o test.h264 -t 30000"], shell=True)

Pure Python Library

A pure Python library for the camera module has been developed by an enthusiastic member of the Raspberry Pi community, Dave Jones, to make it easier and neater to use the Raspberry Pi camera module within Python programs. The new library was only released on the 24th of September, and as such, it is in early stages of development. This means that the Python library has not been released in a .deb package yet, so you can't install it using the standard apt-get method. However, it is still fairly easy to install:

$ sudo apt-get install python-setuptools
$ easy_install --user picamera

These commands will install the picamera library for the current user only, not for the entire system. Installing the library for the current user not only makes it easier to uninstall later, it also helps you avoid complications later if a full system install occurs using the apt-get method (when a suitable package becomes available).

Once installed, the interface is very Python like, and therefore intuitive and easy to use. For example, to start a preview for 10 seconds with the default settings:

import time
import picamera
camera = picamera.PiCamera()
try:
    camera.start_preview()
    time.sleep(10)
    camera.stop_preview()
finally:
    camera.close()

You'll find more information on how to use the Python library online [3].

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Using the RPi Cam Web Interface

    You can access and control the Raspberry Pi camera module from your favorite browser using the RPi Cam Web Interface software.

  • Snapshot

    The remarkable Raspberry Pi has spawned a myriad of supporting projects – Android apps, program libraries, specialized Linux distributions, and an assortment of hardware accessories. The rapid changes within these projects is testament to the excitement and enthusiasm that developers around the world have given to the Raspberry Pi.

  • Building a Motion Detector using Scratch GPIO server

    As of the Raspbian Jessie release, Scratch provides easy access to the Raspberry Pi's GPIO pins. This project incorporates the new GPIO server to build a motion detector.

  • Build your own infrared camera

    The human eye is sensitive to a narrow range of wavelengths that make up the visible light spectrum, but not to the infrared range. However, a Rasp Pi outfitted with an IR camera module offers a low-cost solution for taking photographs using infrared light.

  • SunRover Part 4 – Adding a Pi Camera and Diagnostics System

    A Raspberry Pi Camera module and a diagnostics system allows SunRover to see and check that all systems are go.