Progress
This commit is contained in:
parent
921ae2fe04
commit
1ebe34049e
3 changed files with 135 additions and 6 deletions
68
code/overlay_functions.py
Normal file
68
code/overlay_functions.py
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
# With credit to Ben Nuttall and for the original version of this code
|
||||
|
||||
from PIL import Image
|
||||
from time import gmtime, strftime
|
||||
from itertools import cycle
|
||||
|
||||
# Change these values for your own setup
|
||||
overlays_dir = "/home/pi/allseeingpi/overlays"
|
||||
pictures_dir = "/home/pi/allseeingpi"
|
||||
overlays = ['girl', 'cowboy', 'top', 'pink', 'glassesnose', 'moustache', 'sunglasses', 'elvis', 'emo', 'blackhat', 'emo2', 'baseball', 'flowers', 'santa', 'alps', 'mop', 'glasses']
|
||||
|
||||
# Overlay functions
|
||||
def _get_overlay_image(overlay):
|
||||
|
||||
# Open the overlay as an Image object
|
||||
return Image.open(overlays_dir + "/" + overlay + ".png")
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
def remove_overlays(camera):
|
||||
|
||||
# Remove all overlays from the camera preview
|
||||
for o in camera.overlays:
|
||||
camera.remove_overlay(o)
|
||||
|
||||
def gen_filename():
|
||||
|
||||
# Generate a filename with a timestamp
|
||||
filename = strftime(pictures_dir + "/image-%d-%m %H:%M.png", gmtime())
|
||||
return filename
|
||||
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
all_overlays = cycle(overlays)
|
||||
|
|
@ -56,20 +56,76 @@ from gpiozero import Button
|
|||
|
||||

|
||||
|
||||
## Add fun overlays
|
||||
# Set up the camera
|
||||
|
||||
1. The All Seeing Pi isn't a normal photo booth. The second button we set up, `next_overlay_btn`, is used to change between 'overlays' or funky additional pictures to make your photos look cool. Here is an example of a picture taken with an overlay:
|
||||
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:
|
||||
|
||||
```python
|
||||
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:
|
||||
|
||||
```python
|
||||
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:
|
||||
|
||||
```python
|
||||
from time import gmtime, strftime
|
||||
```
|
||||
|
||||
1. Underneath your camera set up code, add the following line:
|
||||
|
||||
```python
|
||||
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](https://docs.python.org/2/library/time.html#time.strftime) 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:
|
||||
|
||||
```python
|
||||
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:
|
||||
|
||||

|
||||
|
||||
You can make your own overlays or we have provided some ready made ones that you can download
|
||||
You can make your own overlays or we have provided some ready made ones that you can download in the [overlays folder](https://github.com/raspberrypilearning/the-all-seeing-pi) 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.
|
||||
|
||||
1. Create a subfolder within your `allseeingpi` folder called `overlays` and place your overlay images inside it.
|
||||
|
||||
1. We will need some functions to be able to work with our overlays. If you would like to use our [pre-written overlay functions](code/overlay_functions.py), 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](worksheet3.md) to find out how they work.
|
||||
|
||||
1. Next to the other `import` statements in your program, add another one to import this file:
|
||||
|
||||
```python
|
||||
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
|
||||
|
||||
## Take a picture when button pressed
|
||||
|
||||
from picamera import PiCamera
|
||||
## Save overlay on your picture
|
||||
|
||||
## Tweet picture
|
||||
|
||||
|
|
|
|||
5
worksheet3.md
Normal file
5
worksheet3.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# The All Seeing Pi (Overlay Functions)
|
||||
|
||||
This page explains the overlay functions
|
||||
|
||||
## Making the photo booth
|
||||
Loading…
Reference in a new issue