The Raspberry Pi camera module opens the door for your Pi projects to incorporate aspects of photography and movie making. We’re combining both here to create a fully featured stop-motion animation application, Pi-Mation, which makes it incredibly easy to create impressive HD animations.
We’ve written this project with Python and it relies on two libraries that you will need to install. Picamera (picamera.readthedocs.org) is a pure Python interface to the Raspberry Pi camera module and is a must for all camera module owners. Pygame (www.pygame.org), which ensures our images can be displayed on demand.
Step 01 Set up the camera module
First things first, you need to make sure your Raspberry Pi is up to date. In the terminal, type:
$ sudo apt-get update && sudo apt-get upgrade
Next we need to update the Pi’s firmare and ensure camera module is activated. Bear in mind that this takes some time.
$ sudo rpi-update sudo raspi-config
Step 02 Install other dependencies
Next we’ll make sure Pygame and picamera are installed:
$ sudo apt-get install python-setuptools $ easy_install -user picamera
Finally, to install Pygame and the video apps, type:
$ sudo apt-get install python-pygame $ sudo apt-get install libav-tools && sudo apt-get install omxplayer
Step 03 Final setup
We’re going to install Pi-Mation with Git, so let’s make sure it’s installed:
$ sudo apt-get install git
With a terminal open, navigate to your home directory (with cd ~) and type:
$ git clone https://github.com/russb78/pi-mation.git
If you play with the code and break it, you can revert it back to its original state with:
$ git checkout pi-mation.py
Step 04 Running and testing Pi‐Mation
Now navigate into the pi-mation folder and run the application with:
$ python pi-mation.py
Pressing the space bar calls take_pic() from the main() loop, which saves an image and creates a preview that’s loaded by update_display(). The Tab button is coded to toggle between two states by asking two variables to switch values.
Step 05 Getting animated
The main() loop checks for keyboard events before updating the screen around 30 times per second. Since the camera’s live preview is working independently of that loop, update_display() only needs to worry about updating the preview image (prev_pic) Since take_pic() adds to pics_taken, only the very latest picture is shown. The animate() function is essentially a microcosm of update_display(). When the P key is pressed, the live preview is suspended and for all the pictures taken (pics_taken), each will be ‘blitted’ (updated) on the main window.