Which Raspberry Pi photo was funniest?

We had a great day at the Egham Raspberry Pi Jam, and Rabbit Escape and our Photo Booth:

seemed to go down well:


But which photo was funniest? Here are some of the entries (I had to choose kids’ ones without faces to go on here, but there were some other great ones!):





But the winner has to be the eyeball wearing a hat!

Thanks everyone, see you next time!

Raspberry Pi Funniest Photo Game

For our latest Egham Raspberry Pi Jam, we worked on a photo booth program, using the Raspberry Pi camera. Here’s how we did it.

Downloads: funniest-photo-game.odp, photo-booth.zip.

Update 1: fixed a bug where it tried to create a directory that already exists

Update 2: see the winning photo!

Equipment

What we did

  • Made and decorated a box that held the Pi and camera steady for taking photos:
    Decorated Box for Raspberry Pi
    (Note the holes for the wires!)
  • Created some costumes (see “Costumes” below)
  • Wrote a Python program (see “Program” below) to display pictures on top of the camera picture, and take photos
  • Wrote up the instructions and competition rules

Costumes

We made lots of costumes that show up over the picture. They all needed to be 1280×720 pixels, PNG files that use Indexed Color mode. (In Gimp we clicked “Image”, then “Mode”, then “Indexed Color” before choosing “File” then “Export” or “Overwrite” to save them as .png files.)

Here are some examples:

Once we’d made the costumes we put them in a directory called “costumes” next to the program file, photo-booth.py.

Setup

To get the Pi ready to run our program we needed to type these commands:

sudo apt-get update
sudo apt-get install python-picamera
sudo apt-get install python-imaging

Once we’d done this, we created the Python program and directories described in the next section, and then we ran the program with:

python photo-booth.py

Program

We made a directory to hold our program on the Desktop of our Raspberry Pi, which we called “photo-booth”. Inside that, we made a “costumes” directory containing our costumes, and a “gallery” directory to hold the saved photos. Also inside “photo-booth” we saved this code as “photo-booth.py”:

import io
import picamera
from PIL import Image
import time
import pygame
import subprocess
import os

class PhotoBooth:
    def __init__( self, camera ):
        self.camera = camera
        self.costumes = os.listdir( 'costumes' )
        self.current_overlay = None
        self.cos_num= 0
        self.current_costume = None

        self.set_up_camera()

        self.change_costume(0)

    def set_up_camera(self):
        self.camera.resolution = ( 1280, 720 )
        self.camera.framerate = 24
        self.camera.start_preview()

    def change_costume(self, change):
        self.cos_num += change
        self.cos_num = self.cos_num%len(self.costumes)
        self.current_costume = Image.open( 'costumes/'+self.costumes[self.cos_num])
        self.overlay(self.current_costume, 128)

    def overlay(self, image, alpha):
        pad = Image.new('RGB', (
            ((image.size[0] + 31) // 32) * 32,
            ((image.size[1] + 15) // 16) * 16,
            ))
        pad.paste(image, (0, 0))

        if self.current_overlay is not None:
            self.camera.remove_overlay(self.current_overlay)

        self.current_overlay = camera.add_overlay(pad.tostring(), size=image.size)
        self.current_overlay.alpha = alpha
        self.current_overlay.layer = 3

    def take_photo(self):
        stream = io.BytesIO()
        camera.capture( stream, format='jpeg' )
        stream.seek( 0 )
        captimg = Image.open( stream )
        imgrgba = self.current_costume.convert("RGBA")
        captimg.paste( imgrgba, ( 0, -100 ), imgrgba )
        self.overlay(captimg, 255)
        captimg.save( 'gallery/photo%d.png' % time.time().real )
        self.overlay(self.current_costume, 128)

    def run(self):
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        return
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_LEFT:
                            self.change_costume(1)
                        if event.key == pygame.K_RIGHT:
                            self.change_costume(-1)
                        if event.key == pygame.K_SPACE:
                            self.take_photo()
                        if event.key == pygame.K_ESCAPE:
                            return
                        if event.key == pygame.K_g:
                            subprocess.call('ls gallery/*.png | head -1 | xargs xdg-open', shell = True)
                            return
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        self.take_photo()

pygame.init()
pygame.display.set_mode()
if not os.path.exists( 'gallery' ):
    os.makedirs( 'gallery' )

with picamera.PiCamera() as camera:
    booth = PhotoBooth( camera )
    booth.run()

Download code and costumes

You can download the code and costumes we used here: photo-booth.zip.

If you have any questions, feel free to leave a comment below!

Snake in Elm

Snake Series: Groovy, Ruby, BASIC, Dart, Elm

Elm Series: Snake in Elm, Elm makes me happy, Elm Basics

I’m writing the game Snake in lots of programming languages, for fun, and to try out new languages.

Elm brings the comfortable feeling and scary syntax of a pure functional language to writing dynamic web sites.

Slides: Snake in Elm

If you want to, you can Support me on Patreon.

Out-of-school education settings

Here is the comment I sent to the Government consultation on out-of-school education settings. Feel free to re-use, modify, distribute as you like.

I am writing to express my opposition to the government’s plans to regulate the content of out-of-school education.

I volunteer on an annual residential Christian holiday for young people which includes teaching on Christianity and the Bible. I am concerned that the work of this holiday would be regulated under these proposals. However, my concern is much wider than simply this and similar work.

I want to defend people I disagree with, as well as those with whom I agree.

I do not believe it is beneficial to society for governments to control the content of education provided outside of the state education system. In fact, I think it is a dangerous path to take, since while we may trust our current government and inspection regime, we have no reason to suppose it will always be tolerant and respectful of diverse views.

The use of terms such as “British values”, “emotional harm” and “tolerance” in the proposals is concerning since these are open to widely-varying interpretations. In recent history, it could certainly have been considered “un-British” to lobby for the legalisation of homosexuality or the enfranchisement of women, or to support a communist political party.

Personally, I interpret “British values” to mean being tolerant of views very different from my own, and keeping state interference in individual decisions to a minimum. In that context, I believe these proposals are in conflict with British values, and my own values.

I believe that the best way to counteract violent religious extremism is to practice the tolerance that we preach: to demonstrate in words and actions that we respect individual freedom of speech and action. Where children are being taught values with which we disagree, we should argue in public against those values, not ban them from being taught.

New open source project on work time – git-history-data

Announcing a little open source project that I have built at work and been allowed to publish Freely.

git-history-data analyses a Git source code repository and dumps out data in a form that is easy to analyse.

I wrote an article demonstrating how to use it to find out some interesting information about the codebase of Git itself and got it published on IBM DeveloperWorks Open: Learning about the Git codebase using git-history-data.