Sie sind auf Seite 1von 27

12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Projects

The All-Seeing Pi
In this resource, you will make a tweeting
touchscreen photo booth using a Raspberry Pi.

Step 1 What you will make

In this resource, you will make a tweeting touchscreen photo booth using a Raspberry Pi.

What you will learn

By creating the All-Seeing Pi with your Raspberry Pi, you will learn:

How to set up a Raspberry Pi Camera Module


How to connect buttons and a touchscreen display
How to control GPIO pins with Python code
How to control the Camera Module with Python code
How to tweet a picture taken with the Camera Module

This resource covers elements from the following strands of the Raspberry Pi Digital Making
Curriculum (https://www.raspberrypi.org/curriculum/):

Design multiple and integrating assets for use in complex nished projects and models (http
s://www.raspberrypi.org/curriculum/design/maker)
Apply higher-order programming techniques to solve real-world problems (https://www.raspb
errypi.org/curriculum/programming/maker)
Create automated systems to solve complex real-world problems (https://www.raspberrypi.or
g/curriculum/physical-computing/maker)
Independently use fabrication systems to produce complex nished projects (https://www.ra
spberrypi.org/curriculum/manufacture/maker)
Engage and share with the digital making community (https://www.raspberrypi.org/curriculu
m/community-and-sharing/creator)

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 1/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 2 What you will need

Hardware

Raspberry Pi Camera Module


Raspberry Pi touchscreen display or standard monitor
2 x tactile push buttons
Breadboard
4 x Male-female jumper leads
2 x large buttons (optional, to replace tactile push buttons)

Software

Software Installation

This resource requires a number of additional software libraries. You will need to be connected to the
internet to install these extra libraries.

To install the software you need, run the following commands in a terminal window:

sudo pip3 install guizero


sudo pip3 install twython

This will install the necessary software to control the Camera Module, create a GUI, and tweet and
manipulate images.

If you are using the Raspberry Pi touchscreen to make this resource, you will also need to enter the
following commands:

sudo apt-get update


sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install -y raspberrypi-ui-mods
sudo apt-get install -y raspberrypi-net-mods

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 2/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 3 Making the photo booth housing

You can create your All-Seeing Pi using any housing you like. Our rst iteration used a humble cardboard
box, but you might want to create a masterpiece of carpentry or laser-cutting wizardry to hold your All-
Seeing Pi.

If you don’t have fancy buttons or a touchscreen, that’s OK too: you can still make the All-Seeing Pi! Here
is a picture of the setup we used when creating this resource: the only extra hardware requirements are
two tactile push buttons, four jumper leads, a breadboard, and a Camera Module. You can use your usual
monitor, keyboard, and mouse.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 3/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 4/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 4 Connecting the Raspberry Pi touchscreen

Set up your Raspberry Pi touchscreen. There is a good tutorial (https://thepihut.com/blogs/rasp


berry-pi-tutorials/45295044-raspberry-pi-7-touch-screen-assembly-guide) available to help
you. Ensure that you power the touchscreen via the Raspberry Pi with jumper leads as shown in the
tutorial, and that the power supply you use is a good quality one.

Situate your screen within the housing of your choice, ensuring you can still access the Raspberry
Pi.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 5/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 5 Connecting the Camera Module

Connect the Camera Module to the Raspberry Pi with the blue side of the connector facing the USB
ports:

Situate the Camera Module in your chosen housing. If you are using glue to secure it in place, be
careful not to get any glue onto the connectors or camera components or they may stop working.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 6/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 6 Connecting the buttons

Using jumper leads, wire one button to GPIO 23 and any ground pin, and the other button to GPIO
25 and any ground pin.

Situate the buttons in your chosen housing. In the software, the button connected to GPIO 23 will
select the next overlay, and the button connected to GPIO 25 will take the picture. (Don’t worry if you
accidentally wire your buttons up the other way around: you can simply swap the pin numbers in the
code!)

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 7/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 7 Finishing the setup

To write the software, you will also need to connect a keyboard and mouse to your Raspberry Pi, as
well as a display if you are not using the touchscreen.

Power your Raspberry Pi on and, if you are using one, check that the touchscreen works.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 8/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 8 Test the buttons

To begin, open the File Explorer, then right click on a blank space inside the File Explorer window.

Select Create New and then click Folder.

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

From the Programming menu, open up Python 3.

Create a new Python le by clicking on File > New File.

Click on File > Save and save your le into the allseeingpi folder you just created, with the
lename allseeingpi.py.

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

from gpiozero import Button

Next we will set up the buttons. In a previous section, we wired our buttons to GPIO 23 and GPIO 25.
Let’s go ahead and set both buttons up.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 9/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

next_overlay_btn = Button(23)
take_pic_btn = Button(25)

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

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 following two functions immediately after the import statement, with some placeholder code to
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")

Press F5 to run your program. Try pressing each button and check that a di erent message pops up
for each in the Python shell.

If your buttons do not produce this result, check that they are wired up correctly, and that they are
connected to GPIO pins 23 and 25 on the Raspberry Pi. The button pins should be in di erent rows
of the breadboard, like this:

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 10/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

You may have buttons with two legs on each side. These should be placed across the gap on your
breadboard with the jumper wires both attached into one side. Take care to ensure the jumper wires
are in the same rows as the legs of the button.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 11/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 9 Set up the camera

Now that we know the buttons work, let’s set up the code for the camera. First add an import
statement to the existing ones at the top of the program:

from picamera import PiCamera

Locate the existing 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 ‘PiCamera’ object with the resolution set to 800 × 480, which is the resolution of the
Raspberry Pi touchscreen. We also tell the camera to ip the preview horizontally (hflip): if we don’t do
this, the preview image will be mirrored, which 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; this is in case we
get an error and need to see what is happening underneath. When you are con dent that your code
works, you can remove the alpha=128 to make the preview fully opaque.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 12/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 10 Take a picture when the button is pressed

Since we will probably take lots of pictures with the All-Seeing Pi, we will put the date and time at
which the picture was taken within the lename to avoid a picture being overwritten each time a
new one is taken. To do this, we will need the gmtime and strftime functions from the time
library, so add this line to the other import statements:

from time import gmtime, strftime

Underneath the code to set up the camera, 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 and lename of where the
captured photo will be saved. 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
lename to be di erent, there is a full reference guide (https://docs.python.org/2/library/time.h
tml#time.strftime) to strftime available. The current date and time is provided by calling the
function gmtime().

Now let’s revisit the take_picture() function and add some new code 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:

def take_picture():
camera.capture(output)
camera.stop_preview()

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

Press F5 to run your program, then press the button to take a picture.

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

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 13/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 11 Working with overlays

The All-Seeing Pi is no ordinary photo booth! The second button we set up, next_overlay_btn, is
used to change between ‘overlays’: these are 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:

You can make your own overlays, or use the ready-made ones we have provided for you to
download. If you are creating your own overlays, make sure that they are saved at 800 × 480
resolution as PNG les, with the background set to transparent.

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

Navigate to the overlays folder (https://github.com/raspberrypilearning/the-all-seeing-pi/tre


e/master/en/resources) of the GitHub repo for this project. Click on the lename of the overlay you
would like to use, then right-click on the download link and save the image into the overlays folder
you just created. Repeat this process until you have saved all of the overlays you would like to use.

Now right-click here (https://projects-static.raspberrypi.org/projects/the-all-seeing-pi/787d


c8a5891c84c130c01e62656837c9916c1bad/en/resources/overlay_functions.py) and save
this le as overlay_functions.py. Make sure you save this le in your allseeingpi directory
(where the allseeingpi.py script is also saved).

In the overlay_functions.py le, nd this comment:

# EDIT THESE VALUES ------------------------

You will need to change this code to specify two things:

- Set the `overlays_dir` to the directory where your overlays are stored. If you are following this tutorial
exactly, you will **not** need to change this directory location.
- Set the `overlays` to be a list of the filenames of the overlays (without extension), surrounded by quotes and
separated by commas. For example, if you had overlay images called `rock.png`, `paper.png`, and `scissors.png`,
your line of code would look like this:

overlays = ['rock', 'paper', 'scissors']

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 14/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Now go back to your allseeingpi.py program. Underneath the other import statements in your
program, add another one to import this le:

from overlay_functions import *

This will allow us to use all of the overlay functions de ned in the overlay_functions.py le from
within our allseeingpi.py le.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 15/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 12 Change overlays with a button

The other button you wired up to your All-Seeing Pi (called next_overlay_btn) will be the one we
use to switch between the various overlays. Locate the function def next_overlay(): and
delete the indented line print ("Next overlay"). In its place, add the following code, making
sure the lines are indented to show that they are part of the function:

def next_overlay():
global overlay
overlay = next(all_overlays)
preview_overlay(camera, overlay)

First, we have to declare that we want to use the global variable, overlay. This means that when we
change the overlay, that value is saved so that we can access it and use it from anywhere, and the
change isn’t lost when we exit this function.

The second line gets the next overlay from the list of all_overlays (de ned within the
overlay_functions.py le), and sets this as the current overlay. Then, the function
preview_overlay() is called to display the new overlay.

Save your program, and run it by pressing F5. Check that when you press the button to change
between overlays, the overlays change. Ensure you have at least one overlay image in your overlays
folder to be able to change between them!

Here is the program so far (https://projects-static.raspberrypi.org/projects/the-all-seeing-pi/


787dc8a5891c84c130c01e62656837c9916c1bad/en/resources/change_overlays_and_take
_picture.py) if you want to check your progress.

You will notice that, when you take a picture, two things happen. Firstly, the overlay does not
disappear and probably makes it quite di cult to see what you are doing: close the Python shell
window to get rid of the overlay. Secondly, people can see a camera preview and can choose a silly
hat from the overlays, but, when they take the photograph, the overlay disappears. We need to add
code to remove the overlay from the screen once the picture is taken, and superimpose it onto the
saved photograph.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 16/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 13 Save an overlay on your picture

Locate the function def take_picture(): and add two lines of code at the end of the function:

def take_picture():
camera.capture(output)
camera.stop_preview()
remove_overlays(camera) # Add this line
output_overlay(output, overlay) # Add this line

Here we are using two more functions from the overlay_functions le. The function
remove_overlays does exactly what it says, and removes all of the overlays so they don’t hang
around after we take a photograph. The output_overlay function takes the photograph and the
overlay and glues them together so the resulting nal output is a photograph with the chosen
overlay superimposed.

Once again, save your le and run it using F5 to check that you can now change between overlays,
and that, when you take a photograph, your chosen overlay is saved as part of the picture.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 17/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 14 Create a GUI

We have an almost-working All-Seeing Pi. However, when a picture is taken, the camera preview
disappears and the user is left staring at the Python shell and the Raspbian desktop. You probably don’t
want your sel e-takers to have to restart the Python program every time someone takes a picture. We
will create a very simple GUI to display the picture that was taken and allow them to take another picture.

To create the GUI we will use a library called guizero, which you should have already installed in the
software installation (software.md) step. Add another import line with the others at the start of
your program to bring in the guizero functions we need:

from guizero import App, PushButton, Text, Picture

At the bottom of your current program, create the beginning of your GUI.

app = App("The All-Seeing Pi", 800, 480)


message = Text(app, "I spotted you!")
app.display()

   First, we create an app, which is the basic container for the GUI. The dimensions are 800 × 480
because that is the resolution of the touchscreen, and the title bar will contain the text “The All-Seeing
Pi”. It is possible to make the GUI full-screen, but we will not do this for now because it can be di cult for
testing. We also create a message, "I spotted you!", and add it to the app before displaying
everything.

Save and run your program again. Check that, when you press the button to take the photo, the
camera preview exits and you see a mostly blank GUI with a message saying “I spotted you!”.

Now, between the message line and the app.display() line, add another line of code to create a
button.

new_pic = PushButton(app, new_picture, text="New picture")

Examining the arguments passed to this PushButton object, we have three parts:

app: tells the button to add itself to the app


new_picture: this is the command. When the button is pushed, it will call the function
new_picture() (which we haven’t written yet!)
text="New picture": this is the text which will appear on the button
Now write the new_picture function so that the button knows what to do when it is pressed. Write
this code after the take_picture() function, but before the code where we set up the buttons.
Ensure that your cursor is not indented, otherwise the code you write now will become part of the
take_picture() function, which we do not want.

def new_picture():
camera.start_preview(alpha=128)
preview_overlay(camera, overlay)

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 18/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

This function is very straightforward: it simply tells the camera to restart the preview, and to display
the overlay (which will be the last overlay we used).

Save your program, and run it using F5 once again. Check that you can press your physical button to
take a picture, and that the GUI displays once the camera preview disappears. Check that you can
press the on-screen button to restart the camera preview and take another picture.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 19/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 15 Stop the picture overwriting

Now that we have introduced the ability to run the program only once but take multiple pictures, we
have a problem. The lename for the picture is generated by this existing line of code:

output = strftime("/home/pi/allseeingpi/image-%d-%m %H:%M.png", gmtime())

However, we only execute this line of code once during the program. This means that, every time the
button is pressed to take a picture, it is saved to the same location, with the same lename. To x this,
we need to regenerate the lename every time we take a picture.

Locate this line of code and copy it so you can paste it somewhere else shortly. Then, change the
output to be equal to an empty string:

output = ""

Now nd your take_picture() function. At the start of the code within the function, add the line
global output and then paste in the line you copied. The altered function should look like this:

def take_picture():
global output
output = strftime("/home/pi/allseeingpi/image-%d-%m %H:%M.png", gmtime())
camera.capture(output)
camera.stop_preview()
# .... code continues...

We are dealing with scoping here: this is an important concept for programmers to understand. Why
did we bother to create the variable output in the main part of the program, and initialise it as a
blank string, when we could have just created it within the take_picture() function? The answer
is that if we only created it within the take_picture() function, once the function nished
executing, the variable would no longer exist. By declaring that we are talking about the global
version of the output variable, we are telling the program that we want to use the variable output
which we created in the main part of the program. This means that once the function exits, the
variable output with the location of the saved picture will still exist. We need to have a permanent
record where the picture was saved because it is used in other places within the program.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 20/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 16 Display the picture

You probably don’t want your photo booth participants to have to go digging through the Raspbian
lesystem to see the picture they took either, so let’s display the picture they took on the GUI.

Locate the line of code where you intialise the output variable:

output = ""

Immediately underneath it, add a new line of code to de ne the location where we will store the
latest-photo, i.e. the photo most recently taken using the booth.

latest_photo = '/home/pi/allseeingpi/latest.gif'

Now locate the line of code where you added the PushButton to your GUI. Immediately before that
line, insert a line of code to display an image on the GUI:

your_pic = Picture(app, latest_photo)

The le we are referring to, latest.gif, does not yet exist, so if you run your program now you will
not see a photograph displayed on the GUI. We must add code inside the take_picture()
function to generate this image so that it can be displayed. Locate the take_picture() function
and, underneath the other code in the function, add the following lines (remembering to ensure that
the new lines of code are also indented):

size = 400, 400


gif_img = Image.open(output)
gif_img.thumbnail(size, Image.ANTIALIAS)
gif_img.save(latest_photo, 'gif')

your_pic.set(latest_photo)

This code opens the output image (the image containing the photo combined with the overlay),
creates a smaller thumbnail of that image in gif format, and saves it at the location set up in
latest_photo. It then sets the image on the GUI (your_pic) to be that latest photo image using
the set() function which is part of the guizero library.

Save your code and test whether, when you take a photograph, it is displayed on the GUI. You may
nd that there is a short delay between the camera preview exiting and the image displaying on the
GUI while it is saving.

You may notice that the picture quality of the image displayed on screen is not optimal. This is
because the picture has been converted to gif format to be displayed on the GUI. The full-quality
png version of the photograph will still be saved in the allseeingpi folder.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 21/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 17 Tweet picture

If you just want a fun photo booth to take and save pictures, you could stop there. Alternatively, you
could go one step further and make your All-Seeing Pi tweet the photo that was taken.

You will need to set up a Twitter account and create an app for your All-Seeing Pi. Follow steps 1-6
on the Getting started with the Twitter API (https://projects.raspberrypi.org/en/projects/gett
ing-started-with-the-twitter-api) resource in a separate le, and check that you can successfully
send a textual tweet from Python.

Save a copy of the auth.py le containing your Twitter API keys (which you created during the
‘Getting started’ tutorial) inside your /home/pi/allseeingpi folder.

Go back to your allseeingpi.py le and, after the other import statements, import Twython:

from twython import Twython

Immediately after importing Twython, add the following code to import your Twitter API credentials
from your auth.py le:

from auth import (


consumer_key,
consumer_secret,
access_token,
access_token_secret
)

Create a new function after the new_picture() function, called send_tweet():

def send_tweet():

Inside the function, instantiate a Twitter object:

def send_tweet():
twitter = Twython(
consumer_key,
consumer_secret,
access_token,
access_token_secret
)

Add some more code inside the send_tweet() function to tweet the output picture. You can
change the text in your message code if you want your tweet to say something di erent:

message = "The All-Seeing Pi saw you!"


with open(output, 'rb') as photo:
twitter.update_status_with_media(status=message, media=photo)

Now, nd the code for the GUI where you create the PushButton for a new picture, and add another
PushButton underneath it which will call the send_tweet() function when it is pressed:

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 22/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

tweet_pic = PushButton(app, send_tweet, text="Tweet picture")

Save and run your program. Test whether, when you take a picture and press the Tweet picture
button on the GUI, the picture is tweeted from your Twitter account.

The nished code is here (https://projects-static.raspberrypi.org/projects/the-all-seeing-pi/787d


c8a5891c84c130c01e62656837c9916c1bad/en/resources/ nished_allseeingpi.py): you can
check it against your code if you need to.

Once you are happy that your All-Seeing Pi works, you may wish to remove the alpha=128 command
from the camera preview to make it fully opaque. You can also make the GUI full-screen: locate the line
app = App("The All-Seeing Pi", 800, 480) and, immediately after it, add the line
app.tk.attributes("-fullscreen", True).

Other ideas

Can you add a text box or perhaps a touchscreen keyboard to your GUI to allow someone to enter
their Twitter handle?
Can you use this Twitter handle to add an @username mention to the tweet text?
Could you make a more imaginative housing for your All-Seeing Pi?

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 23/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 18 The All Seeing Pi (Overlay Functions)

The following section is for more advanced learners as it explains in detail what the code and functions
inside overlay_functions.py do. It is possible to make the All Seeing Pi without understanding what
these functions do - simply save a copy of the le overlay_functions.py (https://projects-static.rasp
berrypi.org/projects/the-all-seeing-pi/787dc8a5891c84c130c01e62656837c9916c1bad/en/res
ources/overlay_functions.py) into the folder with your code and they will be available.

Importing necessary libraries

These statements import functions from the PIL library to process and save the images and the
itertools library so that we can cycle through the overlays.

from PIL import Image


from itertools import cycle

Setting up the variables

This part sets up the directory where the overlays are saved, and the names of the various overlays. The
overlay variable is initialised with the rst value in the list.

# EDIT THESE VALUES ------------------------


overlays_dir = "/home/pi/allseeingpi/overlays"
overlays = ['girl', 'cowboy', 'top', 'pink', 'glassesnose', 'moustache', 'sunglasses', 'elvis', 'emo', 'blackhat',
'emo2', 'baseball', 'flowers', 'santa', 'alps', 'mop', 'glasses']
# ------------------------------------------
overlay = overlays[0] # Starting value

Get the overlay as a PIL Image

This function is only used within other functions in this le. Given the name of an overlay as a string, it
creates a PIL Image object of that overlay and returns it.

def _get_overlay_image(overlay):

# Open the overlay as an Image object


return Image.open(overlays_dir + "/" + overlay + ".png")

Pad the overlay

This function ensures that the overlay is padded correctly so it can be displayed on the preview.

def _pad(resolution, width=32, height=16):


# Pads the specified resolution
# up to the nearest multiple of *width* and *height*; this is
# needed because overlays require padding to the camera's
# block size (32x16)
return (
((resolution[0] + (width - 1)) // width) * width,
((resolution[1] + (height - 1)) // height) * height,
)

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 24/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Remove all overlays

This function iterates over all overlays attached to the camera object, and removes them.

def remove_overlays(camera):

# Remove all overlays from the camera preview


for o in camera.overlays:
camera.remove_overlay(o)

Put the overlay on the camera preview

This function is passed a PiCamera object (camera) and a string (overlay). It removes all overlays
currently associated with the camera object, creates a PIL Image object of the chosen overlay called
overlay_img, pads that image to display correctly and then adds it to the camera preview. The alpha of
the preview is set to 128 so that the overlay is semi transparent. If the overlay was made fully opaque it
would obscure the camera preview.

def preview_overlay(camera=None, overlay=None):

# Remove all overlays


remove_overlays(camera)

# Get an Image object of the chosen overlay


overlay_img = _get_overlay_image(overlay)

# Pad it to the right resolution


pad = Image.new('RGB', _pad(camera.resolution))
pad.paste(overlay_img, (0, 0))

# Add the overlay


camera.add_overlay(pad.tobytes(), alpha=128, layer=3)

Save picture with overlay

This function takes the location of the photograph (output) and the given overlay (overlay), both as
strings. It then creates a PIL Image object of the speci ed overlay, also creates a blank PIL Image to save
the nished output to, and then combines the photograph with the overlay, re-saving the nished
photograph at the output location.

def output_overlay(output=None, overlay=None):

# Take an overlay Image


overlay_img = _get_overlay_image(overlay)

# ...and a captured photo


output_img = Image.open(output).convert('RGBA')

# Combine the two and save the image as output


new_output = Image.alpha_composite(output_img, overlay_img)
new_output.save(output)

Overlays cycle

This code creates a cycle. We use the next() function on this cycle when the next_overlay_btn is
pressed in order to receive the next overlay in the list. A cycle is needed because when the end of the list
of overlays is reached, we want to automatically begin again with the rst overlay.

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 25/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

all_overlays = cycle(overlays)

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 26/27
12/4/2019 The All-Seeing Pi | Raspberry Pi Projects

Step 19 What next?

Can you add a text box or perhaps a touchscreen keyboard to your GUI to allow someone to enter
their Twitter handle?
Can you use this Twitter handle to add an @username mention to the tweet text?
Could you make a more imaginative housing for your All-Seeing Pi?

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons


license (https://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/the-all-seeing-pi)

https://projects.raspberrypi.org/en/projects/the-all-seeing-pi/print 27/27

Das könnte Ihnen auch gefallen