Skip to content

Raspberry Pi Timelapse or Flipbook

jallwine edited this page Aug 17, 2014 · 9 revisions

This tutorial is very similar to Raspberry Pi Photobooth, but this one doesn't go quite as step by step. It is recommended to start with the Photobooth tutorial.

Setup

picamera module

A photobooth can be a way show off your wacky side or just a way to remember an important event such as a birthday party (or both!). In this tutorial, we'll show you how to set one up with your Raspberry Pi and camera module. First, install the picamera python module.

sudo apt-get install python-picamera

Camera Preview

Open IDLE either from your desktop or through the menu at the bottom left. IDLE is an integrate development environment (IDE) for python. We'll be using it to input and run our photobooth python script. Let's start by taking our first picture. Click File - New Window, or press Control+N. In the new window, enter the following:

import picamera     # module for controlling the camera
import time         # module for adding delays

camera = picamera.PiCamera()   # initialize the camera
camera.start_preview()         # start previewing
time.sleep(5)                  # wait for 5 seconds
camera.stop_preview()          # stop the preview
camera.close()                 # close the camera

Save the file by clicking File - Save or by pressing Control+S. Name the file timelapse.py. To run your script press F5. You should see a preview of what the camera can see for 5 seconds before it closes. Now we'll edit our program above to behave like a photobooth. If you don't see a preview, then you may need to troubleshoot your camera connection or enable the camera in raspi-config settings.

Capturing Photos

To take a picture with the camera module use the capture() method. We'll be taking several pictures so we'll be calling capture() in a for loop. For efficiency, we're scaling down the images to a quarter of their original size. You can remove the resize=(480,270) (along with the , before it), if you want a full 1920x1080 timelapse, but it will take considerably longer to process the images.

Add these lines just under the import statements:

num_pictures = 10     # Number of pictures we want to take
delay = .5            # Number of seconds to wait between each picture. We've set this to a small number
                      # suitable for a flipbook. Make this longer for time lapse. For example, to take a
                      # picture once every 5 minutes set it to 300 (60 sec per min x 5 min = 300 sec)

And replace the time.sleep(5) line with these lines. Make sure the indentation is correct. for i in range(num_pictures): camera.capture("frame%s.jpg" % i, resize=(480,270)) time.sleep(delay)

Run your script with F5 and when its done open a terminal and run the following command:

animate -delay 10 frame*.jpg

Animated GIF

To save all your images as an animated gif, we'll use the Image Magick command, convert. First, add this line to the top of your script:

from os import system

Then add these lines at the bottom:

pages = []
for i in range(num_pictures):
    pages.append("-page +0+0 frame%s.jpg" % i)
convert_cmd = "convert -delay 10 %s animation.gif" % (" ".join(pages))

print "Creating animated gif..."
system(convert_cmd)

Note that it can take a while to finish, especially if you've turned up the number of pictures or the size of your photos. The file animation.gif will contain your timelapse or flipbook.

Make a video

To encode your images into a video, you can use mencoder. To install it type the following into a terminal:

sudo apt-get install mencoder

Once it's done you can run these commands to encode a video:

ls frame*.jpg > animation.txt
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=480:270 -o animation.avi -mf type=jpeg:fps=10 mf://@animation.txt

To play it you can use mplayer:

mplayer animation.avi