TimeLapse Project — Raspberry Pi. PART 1: CAPTURING IMAGES
By: Inventor Girl
Tags: Timelapse, Raspberry Pi, Python Programming, Girls Who Code, Girls Education

Save this <untitled> empty file as “timelapse.py” first. Then we will type the following code in it. I will explain what each line does next.

If you’re new to python, you will notice that python codes including this one, look neat and tidy, with equal number of spaces in the lines that belong in the same function or block. This is important! In python, you do need to keep the number of spaces the same between the lines otherwise you will either get an error or your code will run incorrectly. Make sure your code looks like mine in terms of spaces in the beginning of the lines!

The first three lines with “import” in them, load some (pre-existing) set of codes/libraries that we can use later. The three libraries we are importing already come with your python installation and you just need to import them. The picamera library has a bunch of useful code for working with the camera module. The PIL library (short for Python Imaging Library) has a bunch of code for image processing. We won’t use it right away but later, I will teach you how to do a smart flash that only turns on if the image is too dark. We can use PIL functions for that. The third library is time, which has some functions that allow our timelapse program to go to sleep for 5 seconds (you can change that in line 14), wake up, take a picture, and go back to sleep.

Now let’s look at lines 19 and 20 next. This strange if __name__==’__main__’: line exists in many python programs. If you run your python program, this is usually the first line that will be executed. So this block is, in a way, the main controller for all your program. In our case, we will call a function called “capture_image” in there, and we give it a parameter 3 as input. i.e. capture_image(3).

The function capture_image is defined in lines 5 to 16. Note that it has a parameter (n), and the way it is written in line 5, i.e (n=3) it means that if we call this function *without* specifying this n, then by default, it will be given the value of 3. It is a good habit to give default values to your parameters, but you don’t have to. Your code will eventually complain (i.e. exit with an error message!), if it is expecting a value for the parameter and nothing has been specified for it.

So what happens inside capture_image? First, we get access to our camera by creating a camera object, which we can then work with. i.e. camera = PiCamera() in line 6. We can set the image resolution by camera.resolution=(xx, yy). My camera device, which is the Raspberry Pi Camera module (i.e. the hardware) has the image resolution of 2560, 1920 so images will be saved with best quality if we use that resolution. You will then need to activate the camera via camera.start_preview(). Ignore lines 9 & 15 for a second and let’s first look at lines 10 to 14. What happens there is that we have a loop that repeats itself n times. (Remember that n is something you can specify when you call capture_image in line 20, and by default it is 3). In that loop, i changes from 0 to n-1.

Inside the loop, at each round, we first specify a file name (in our case our filenames will be 0.jpg, 1.jpg, 2.jpg, and so on).

We then repeatedly take a picture with the camera in line 12, i.e. camera.capture(filename), which saves the picture into the filename. Since this is in a loop, each time the filename will be different so we will save multiple images.

In line 13, we read that image that we just saved. We actually don’t need this line right now, and only will need this for our smart flash. (i.e. we can write a little code to check the brightness of image_just_saved and turn a flash on.) But let’s deal with that later. For now, just keep the line.

Finally, in line 14, we tell the program to wait 5 seconds before repeating the loop step. You can change this 5, and for longer timelapse videos I recommend a 60-second wait time.

If any error happens in our code, we want to be able to stop the camera and end the program. This is why we added this try: and finally: lines to our code. “try” is a special word in python that means that whenever an error happens inside the block that is under the “try”, we can catch the error inside the program and handle it somehow. I will not get into too much detail on it right now but if you’re curious learn more about it here: https://docs.python.org/3/tutorial/errors.html. We will learn more about these in future posts.

OK so if you have everything written, you can save your code. Then, there’s a green button “Run” on top that “runs” your code. Try it out! If everything is correct, you will see 3 image files (0.jpg, 1.jpg, 2.jpg) showing up on the same directory. If you get any error messages, reply to this post and I’ll help you fix it.

Most of your coding is done already! (if you don’t need flash-light). If you change the sleep(5) into sleep(60) (line 14), you will capture the image every 1 minute. And if change capture_image(3) into capture_image(60*24*7) your timelapse will continue for 7 days, (each 24 hours, each hour 60 minutes) and you will end up with 10,080 jpg images on your desktop, named 0.jpg to 10,079.jpg