simplebooth/worksheet2.md
2017-02-15 13:24:17 +00:00

6.6 KiB

The All Seeing Pi (Part 2 - Software)

In this resource you will make a tweeting touch screen photo booth using a Raspberry Pi.

Test the buttons

  1. With the hardware set up, we can begin to program the software that will make everything work. To begin, open the file explorer, then right click on a blank space inside the file explorer window.

File Explorer

  1. Select Create new and then click Folder

Create folder menu

  1. Type in the name of the folder where you will store the code and the photographs. We chose to call ours allseeingpi. Make a note of the path to this folder which is displayed in the bar at the top, which should be /home/pi/allseeingpi

  2. From the "Programming" menu, open up "Python 3"

Open Python 3

  1. Create a new Python file by clicking on File > New File.

  2. Click on File > Save and save your file into the allseeingpi folder you just created, with the filename allseeingpi.py.

  3. We will need the gpiozero library. At the start of your Python file add an import statement:

from gpiozero import Button
  1. Next we will set up the buttons. On the previous worksheet we wired our buttons to pins 23 and 25. Let's go ahead and set both buttons up.
next_overlay_btn = Button(23)
take_pic_btn = Button(25)
  1. Now we will use gpiozero to tell the buttons what to do when pressed. In the code below, next_overlay and take_picture are functions which will be called when the corresponding button is pressed:
  next_overlay_btn.when_pressed = next_overlay
  take_pic_btn.when_pressed = take_picture
  1. We will write these two functions so that the buttons know what to do when they are pressed. Functions are usually written at the start of a program immediately after the import statements. Add the functions, but with some placeholder code to just print a message when they are pressed, so we can test them.
def next_overlay():
  print("Next overlay")

def take_picture():
  print("Take a picture")
  1. Press F5, save and run your program. Try pressing each button and check that a different message pops up for each in the Python shell.

    Test the buttons

Set up the camera

  1. Now that we know the buttons work, let's set up the camera. First add an import statement with the others at the top:
from picamera import PiCamera
  1. Locate the line take_pic_btn.when_pressed = take_picture and below it add the following code to set up the camera object:
camera = PiCamera()
camera.resolution = (800, 480)
camera.hflip = True
camera.start_preview(alpha=128)

This code creates a camera object with the resolution set to 800x480 which is the resolution of the Raspberry Pi touchscreen. We also tell the camera to flip the preview horizontally (hflip) because otherwise it will be mirrored and this makes it hard for people to align themselves with the overlays! We then start the preview with alpha set to 128 so that it is semi-transparent in case we get an error and need to see what is happening underneath. When you are confident your code works you can remove the alpha=128 to make the preview fully opaque.

Take a picture when the button is pressed

  1. Since we will probably take lots of pictures with the All Seeing Pi, we need filenames containing the date and time they were taken to avoid overwriting pictures each time a new one is taken. To do this, we will need the gmtime and strftime functions from the time library, so import them with this line of code:
from time import gmtime, strftime
  1. Underneath your camera set up code, add the following line:
output = strftime("/home/pi/allseeingpi/image-%d-%m %H:%M.png", gmtime())

This will create a variable called output which contains the location of where we will save our picture file, including the filename. The %d, %m (etc) characters are how we specify the time format - %d means the day and %m means the month, for example. If you would like the date format in your filename to be different, there is a full strftime reference available. The current date and time itself comes from the function gmtime().

  1. Now we will add some proper code to the take_picture() function, so that it actually takes a picture instead of just printing a message. Locate the line def take_picture(). Delete the line print("Take a picture") and in its place, add the following lines, making sure they are indented:
camera.capture(output)
camera.stop_preview()

This code captures a picture, saving it to the location we just defined in the variable output. It then stops the camera preview.

  1. Navigate to the folder /home/pi/allseeingpi and check that the picture you just took has saved correctly.

Working with overlays

  1. The All Seeing Pi is no ordinary photo booth! The second button we set up, next_overlay_btn, is used to change between 'overlays' - fun pictures such as hats, beards and glasses which appear on the screen as if you are wearing them. Here is an example of a picture taken with an overlay:

    Rik with pigtail overlay

    You can make your own overlays or we have provided some ready made ones that you can download in the overlays folder of the GitHub repo for this project. If you are creating your own overlays, make sure that they are saved at 800x480 resolution as PNG files, with the background set to transparent.

  2. Create a subfolder within your allseeingpi folder called overlays and place your overlay images inside it.

  3. We will need some functions to be able to work with our overlays. If you would like to use our pre-written overlay functions, download a copy of the file and save it as overlay_functions.py in your allseeingpi directory. If you would like to see a full explanation of what these functions do, head to the overlay functions explanation page to find out how they work.

  4. Next to the other import statements in your program, add another one to import this file:

from overlay_functions import *

This will allow us to use all of the overlay functions defined in the overlay_functions.py file, from within our allseeingpi.py file.

Change overlays with a button

Save overlay on your picture

Tweet picture

Create a GUI