Changing overlays

This commit is contained in:
Laura 2017-02-17 08:58:00 +00:00
parent 1ebe34049e
commit 6eb54f14f4

View file

@ -24,9 +24,9 @@ In this resource you will make a tweeting touch screen photo booth using a Raspb
1. We will need the `gpiozero` library. At the start of your Python file add an import statement:
```python
from gpiozero import Button
```
```python
from gpiozero import Button
```
1. Next we will set up the buttons. On the [previous worksheet](worksheet.md) we wired our buttons to pins 23 and 25. Let's go ahead and set both buttons up.
@ -64,7 +64,7 @@ from gpiozero import Button
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:
1. Locate the existing line `take_pic_btn.when_pressed = take_picture` and below it add the following code to set up the camera object:
```python
camera = PiCamera()
@ -73,12 +73,12 @@ from gpiozero import Button
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.
This code creates a PiCamera 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 the preview image 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:
1. Since we will probably take lots of pictures with the All Seeing Pi, we will put the date and time the picture was taken within the filename 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 with the other `import` statements:
```python
from time import gmtime, strftime
@ -90,13 +90,14 @@ from gpiozero import Button
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()`.
This will create a variable called `output` which contains the location and filename 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 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 is provided by calling 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()
def take_picture():
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.
@ -113,7 +114,7 @@ from gpiozero import Button
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. 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, or you would prefer to write them yourself, head to the [overlay functions explanation page](worksheet3.md) to find out how to do this.
1. Next to the other `import` statements in your program, add another one to import this file:
@ -125,6 +126,18 @@ from gpiozero import Button
## Change overlays with a button
1. 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 `def next_overlay():` line and delete the line `print ("Next overlay")`. In its place, add the following code, making sure the lines are indented:
```python
overlay = next(all_overlays)
preview_overlay(camera, overlay)
```
The first line gets the *next overlay* from the list of `all_overlays` which is defined within the `overlay_functions.py` file. Then, the function `preview_overlay()` is called to add the overlay by giving it both the camera object and the overlay we want.
1. 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!)
## Save overlay on your picture
## Tweet picture