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
Pages: 2
(incl. VAT)