diff --git a/code/finished_allseeingpi.py b/code/finished_allseeingpi.py index ae0aedd..836435a 100644 --- a/code/finished_allseeingpi.py +++ b/code/finished_allseeingpi.py @@ -19,6 +19,8 @@ def next_overlay(): # Tell the take picture button what to do def take_picture(): + global output + output = strftime("/home/pi/allseeingpi/image-%d-%m %H:%M.png", gmtime()) camera.capture(output) camera.stop_preview() remove_overlays(camera) @@ -67,7 +69,7 @@ camera.hflip = True camera.start_preview(alpha=128) # Set up filename -output = strftime("/home/pi/allseeingpi/image-%d-%m %H:%M.png", gmtime()) +output = "" latest_photo = '/home/pi/allseeingpi/latest.gif' diff --git a/code/overlay_functions.py b/code/overlay_functions.py index 9c03f6f..a84dc47 100644 --- a/code/overlay_functions.py +++ b/code/overlay_functions.py @@ -1,3 +1,6 @@ +# Adapted from some original code by bennuttall and waveform80 +# ------------------------------------------------------------- + from PIL import Image from itertools import cycle diff --git a/worksheet2.md b/worksheet2.md index c53818a..2165125 100644 --- a/worksheet2.md +++ b/worksheet2.md @@ -213,6 +213,35 @@ We have an almost working All Seeing Pi. However, when a picture is taken, the c 1. Save and run your program 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. +## 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 filename for the picture is generated by this existing line of code: + +```python +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 filename. To fix this, we need to regenerate the filename every time we take a picture. + +1. 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: + + ```python + output = "" + ``` + +1. Now find 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: + + ```python + 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 which 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 finished 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, and to save the generated filename. We need to save the filename to be used again because it is not just used in the `take_picture()` function, but in other places within the program. + ## Display the picture You probably don't want your photo booth participants to have to go digging through the Raspbian filesystem to see the picture they took either, so let's display the picture they took on the GUI.