Controlling the Raspberry Pi Camera with Python

Today at the Raspberry Pi club, myself and a couple of the kids decided to play with the Raspberry Pi camera and the new Python API that was announced this week. We made the beginnings of our very own photo booth!

The Raspberry Pi Camera

Our science technician lent us his Raspberry Pi camera the other day, saying he wanted a bit of help trying to work out how to knit it into school projects. One typical idea is to use it as a “wildlife camera”, bolting a Pi and camera to a tree in the fields, and getting it to snapshot the local wildlife as it passes, and uploading pictures to the school network.

However, his early attempts to do this with the standard raspistill and raspivid were a bit in vain, as (like a lot of Pi users), he’s not overly familiar with shell scripting. “What I want is to be able to write a python program that does all sorts of clever things with the camera, let’s not muck about with all this shell scripting rubbish” he said :-)

So, I thought, well it would be possible to write a Python API to access the camera, and had a quick sniff around on Monday to see what was already out there. As luck would have it, Ben Nuttall from Pi Towers had just posted a blog entry on the Raspberry Pi website, announcing that someone had written a great Python API and it was now released[1]! What luck!

The Photo Booth Idea

We’re toying with the idea of getting the kids at the Saturday club to build some exhibits out of Raspberry Pi’s for the centre that kindly hosts our club at the weekends, and one idea mentioned was a photo booth that you walk up to, it takes your picture, and does something with it.

I thought we’d start simple, and we wrote a tiny python program to take a single snapshot to a file – and this did indeed work first time! We started by following the camera setup instructions in [2] and then tried the example programs on Dave’s page linked from here [1].

After about half an hour of waiting for the kids to stop taking funny face “SnapChat”[3] style photos of themselves, we decided to do something with it.

One of the kids suggested we could overlay random text on the image, and being a bit of a dab-hand already with pygame, he jumped right in and we had the basics in a few minutes. More time was spent adding silly messages than actually writing the main program, but that’s a testament to how easy the new Python picamera API is to use!

Here’s the code we used:

import picamera
from time import sleep
import pygame
import random


def quote():
    options = ["Strange person",
               "You should have smiled",
               "you will break the camera",
               "you need a hair cut",
               "You should try SnapChat",
               "Are you a model?",
               "You've grown an inch",
               "Have you shrunk?"
    return random.choice(options)

camera = picamera.PiCamera()
camera.vflip = False
camera.hflip = False
camera.brightness = 60

screen = pygame.display.set_mode((WIDTH,HEIGHT))
black = pygame.Color(0, 0, 0)
textcol = pygame.Color(255, 255, 0)

while True:
    camera.capture('image.gif', format='gif', resize=(WIDTH,HEIGHT))

    img = pygame.image.load('image.gif')
    screen.blit(img, (0, 0))

    text = quote()
    font = pygame.font.Font('freesansbold.ttf', FONTSIZE)
    font_surf = font.render(text, True, textcol)
    font_rect = font_surf.get_rect()
    font_rect.left = 100 = 100
    screen.blit(font_surf, font_rect)

    # WAIT A BIT


Watch those long lines if you type this in – better to cut and paste it into IDLE then save it.

This has got to be one of the nicest API’s I’ve used in a long time – huge thanks and a million awesome points go to @waveform80 (Dave Jones) for putting together such a well written and comprehensive programming interface.

Here’s our setup. Great thanks go to @pimonori who kindly donated some of the kit that we now regularly use at our Saturday Raspberry Pi club, including the pibow clear case, piglow and coloured USB lead you see in the photo below.

Future Work

We hope to build on this idea to build a proper photo booth, and one idea is to use tin foil to construct our own pressure pad, see reference[4] for what we might use.

This was a great little project to do, if we ignore all the time mucking around taking funny photos of ourselves, it probably took us 30 minutes end to end to put this together and get it all working, and it serves as the basis for a much bigger project now.

And finally, our science technician will be very pleased on Monday when we give him his camera and SDCard back with a lovely project for him to continue in the school clubs!

Update September 2014

I’d like to thank all the staff and children at @SchoolPiClub who tried out this code and built it into something better. It’s always so rewarding to hear that people have used my code and projects as a starting point, and that they built on them to create something of their own. That’s why I develop projects and code snippets in the first place – to help others get started and build even more awesome things than I could possibly imagine myself.

Here is a link to their amazing blog, do look through their projects for much inspiration!

@SchoolPiClub blog


[1] Anouncing python.picamera

[2] Setting up the Raspberry Pi Camera

[3] SnapChat

[4] Tin-Foil pressure pad hack

[5] Official Python camera module from @waveform80

This entry was posted in How to, Raspberry Pi. Bookmark the permalink.

Comments are closed.