Turn your Raspberry Pi into a stop-motion video studio
Build your own animation studio by using your Raspberry Pi as a stop-motion camera.
What have you done with your Raspberry Pi camera lately? While it gives us plenty of new ways to use the Pi, unless you’ve got your computer set up as a security webcam or you’re particularly a fan of time-lapse photography, the chances are that you’ve overlooked the Pi camera module for a while.
If you’re a fan of animation or you simply want to extend the possibilities of the module, why not build a stopmotion camera? By using Python and an external button to capture images, the Raspberry Pi can actually be the perfect tool for animators.
Better still, you can go beyond animating toys or bits of Lego and go old school by mounting the Pi on a rostrum and creating a cartoon. Even if you can’t buy or build one, you can mount the stop motion Pi camera with a smartphone mount for stability.
MOUNT YOUR STOP-MOTION 1 PI CAMERA
Before you get started, think about the type of animation you’re going to be capturing. If you’re using the traditional top-down method, as used by classic cartoon animators, then you’ll need a rostrum to mount the Raspberry Pi.
Alternatively, you may be animating something on a desk, table or perhaps the floor, but you’ll need your Pi camera mounted in a similar way, looking across rather than looking down.
Various options are available, such as smartphone tripods and dashboard mounts. Most of these should be suitable for securely mounting your Raspberry Pi.
FIND SOMEWHERE TO SHOOT 2
For your first attempts at shooting a stop-motion video, you should use a wide and uncluttered space. This might be a desk, a kitchen work surface or even the floor, but it should be a hard and flat area in most cases (unless you have need for a bumpy carpeted environment for your video) to aid with the creation of your stop-motion film.
As time progresses and your skill develops, other surfaces can prove useful alternatives, but keep it simple for now and stick with flat surfaces while you get to grips with the art form using the Raspberry Pi stop-motion camera.
CONNECT THE PI CAMERA 3 MODULE
Next you’ll need to connect the Pi camera module to your Raspberry Pi. All models have the necessary connector, although where it is found on the device will depend on the version of your Raspberry Pi.
The Model A has the Pi-camera connector next to the Ethernet port, as does the Model B.
On the B+ and the Raspberry Pi 2, the connector is in a similar position, but it’s a little further from the Ethernet port between the audio-out and HDMI ports.
Connecting the camera module can be tricky. Begin with taking your Pi out of its case or remove the top where possible and disconnect all cables. Take precautions before removing the device from its antistatic bag, as the camera module is very sensitive to static electricity.
On the Pi, lift the plastic catch on the connector and slot the camera module flex into place with the shiny contacts facing away from the Ethernet port. Once the flex is fully slotted in, push the plastic catch back into place.
TEST YOUR PI CAMERA 4 MODULE
After connecting the Pi camera, check that it works by booting the Raspberry Pi (we’re assuming you’re running Raspbian) and entering this in the command line: sudo raspi-config
With the keyboard arrows, move down to option five, ‘Enable Camera’, and tap Enter. In the following screen, hit Enter again to enable the camera and exit. If you’re not already signed into the GUI, do so now (if you’re in the command line interface, enter “startx” to launch the desktop view). Open the terminal and enter: raspistill -o image1.jpg You can review the resulting image in your Home directory.
STRAIGHTEN OUT THE IMAGE 5
With the Pi camera up and running, you may notice that it’s outputting the image with the axes flipped. We can fix this using Python, so open the terminal and enter:
sudo apt-get install pythonpicamera python3-picamera
In the Python editor, open ‘File > New Window’ and enter the code below, setting the ‘camera.vflip’ and ‘camera. hflip’ as True or False as required. Save (perhaps as ‘camflip.py’), then press F5 to run the script and view the correctly outputted image.
To save time, however, you might try rotating the position of your camera or Pi camera module! import picamera from time import sleep with picamera.PiCamera() as camera: camera.vflip = True camera.hflip = True camera.start_ preview() sleep(3) camera.capture(‘/home/pi/ image2.jpg’)
SET UP THE BREADBOARD 6 AND BUTTON
We have two ways to add a button to the Raspberry Pi, but before proceeding, ensure you have switched the computer off and disconnected it from the mains. You should also disconnect any cables and hardware.
The simplest method of adding a button is to employ a solder-free breadboard and a single-state pushbutton. Connect the button to the breadboard with two male-to-female wires running to GPIO pins GND and 17. With a script designed to detect action from the button on the GPIO, each frame of your animation can be captured with a single button push.
CODE FOR STOP MOTION 7
Once satisfied with the results of your Pi camera, it’s time to turn it into a stop-motion camera. The first step is to type up the code shown below, which will capture an image of your subject and save it into a folder called ‘Stop motion’. Each image is numbered sequentially and they can all be stitched together once your animation is complete. Save the code as animation.py: import picamera from RPi import GPIO button = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(button, GPIO.IN, GPIO.PUD_ UP)
with picamera.PiCamera() as camera: camera.start_ preview() frame = 1 while True:
GPIO.wait_for_ edge(button, GPIO.FALLING)
frame%03d. jpg’ % frame) frame += 1 camera.stop_ preview() Then, in a new terminal window, enter the following: sudo python3 animation.py
Press the button to capture each frame, moving the subject as needed. When you’re all done, hit ‘Ctrl-C’ to terminate the script.
STITCH TOGETHER YOUR 8 STOP-MOTION ANIMATION
For true animation, the collected images will need to be compiled into one single file. In the terminal, install ffmpeg: sudo apt-get install ffmpeg
Once installed, you will then need to convert your images into a video clip, as follows:
ffmpeg -y -f image2 -i /home/ pi/Desktop/stop-motion/ frame%03d.jpg -r 24 -vcodec libx264 -profile high -preset slow /home/pi/Desktop/stopmotion/animation.mp4 With this file created, open with the command: omxplayer animation.mp4 The video will then be played in full-screen mode.
USE AN APP INSTEAD 9
Don’t fancy using the script? Try this stop-motion application. Begin by installing the raspicamextras package that includes the UB4L drives for the Pi:
repo/lrkey.asc && sudo aptkey add ./lrkey.asc
sudo sh -c ‘echo “deb http:// www.linux-projects.org/ listing/uv4l_ repo/raspbian/ wheezy main” >> /etc/apt/ sources.list’ sudo apt-get update sudo apt-get install uv4l uv4l-raspicam uv4l-raspicamextras With that done, enter: sudo apt-get install stopmotion
Launch with the stopmotion command to open a GUI with a live camera for you to line up each shot. This is a more elegant solution and captured images can be stitched together using the ‘Number of images’ slider and the camera button above it.
PUT IT ALL TOGETHER 10
Now you have the camera set up, a device for keeping it steady (whether a DIY rostrum or a tripod), and you’ve constructed a button or plan to capture each frame via SSH. Your stop-motion Raspberry Pi camera is finally ready!
By now, you’re probably aching to get started, so with your stop-motion Pi camera ready to use (and close to a power supply), it’s time to start building your film set. While this might simply be an empty table top, there might equally be a few props you would like to include.
STORYBOARD YOUR SHOOT 11
It’s easy to get tied up with the idea of creating a stop-motion camera and forget all about a subject and how it will act.
You can avoid any problems here by taking the time to carefully plan what will happen in your film: your story. Remember, each second of the video will require 26 frames!
The best way to plan at this level is to simply write up an outline, but beyond this, you may prefer to storyboard instead by making pencil sketches to help you progress the story.
CAST YOUR STOP-MOTION 12 SHOOT
You’ll also need a good idea of what your subject will be; this means who or what you’re going to be using the stop-motion camera to capture frames of. Typically, amateur stop-motion films make use of household objects, toys and child’s play clay.
The beauty of this kind of animation is that you can use almost anything that you can get your hands on, from a cup and saucer to an Action Man, as long as you have a way to support the subject(s) in the positions you wish them to take throughout.
STOP-MOTION WITH TOYS 13
If you cast toys as your stopmotion stars, you will get a much better result from something that is built to stand up on its own than toys that tend to sit or fall over. Lego sets and Minifigs appear in many stop-motion productions on YouTube. This is with good reason, as they’re really easy to place in a desired position. The construction element of the bricks is also a major attraction. Another popular option is Transformers toys. These are both good places to start, but you should aim to develop your own approach over time.
“Don’t want to build your own rostrum? Why bother when a camera tripod can be positioned.”
It isn’t only inanimate objects that you can include in stop-motion films. People can feature too! Pop videos such as Peter Gabriel’s 1985 hit ‘Sledgehammer’ have taken advantage of stop motion (that video was produced by Aardman Animations, the eventual creators of Wallace and Gromit) and the technique can be used on humans to create surreal effects. If you want your subject to be moving around a room too, they can appear to be floating or gliding. The results can be strange, but useful if you know what you want.
MAKE YOUR OWN 15 WALLACE AND GROMIT
Known as ‘claymation’, the practice of animating lumps of clay has been a popular form of animation for years, but there’s more to it than just clay. These forms, whether they’re cheeseloving old men, remarkably clever dogs or the wrong pair of trousers, have a wire skeleton that is used to keep movement in the desired position.
This makes it much easier to capture the frames efficiently, but for the best results, you should also have several versions of the same figures available. This is just in case one gets deformed and damaged during production!
FROM STOP MOTION 16 TO TIME-LAPSE
Similar to stop motion, time lapse is a technique that automatically captures images on a preset timer. We can use a Python script to control this, saving the captures in a directory and using ffmpeg to compile them into a film.
However, what you may not want for this project is a mains cable trailing all over, especially if you’re attempting to capture the movement of the stars at night or nature activity. We suggest employing a Pi-compatible battery pack to make your time-lapse Pi camera truly mobile, using SSH to run the script remotely: import time import picamera VIDEO_ DAYS = 1 FRAMES_ PER_ HOUR = 60 FRAMES = FRAMES_ PER_ HOUR * 24 * VIDEO_
DAYS def capture_frame(frame): with picamera.PiCamera() as cam: time.sleep(2) cam.capture(‘/home/pi/ Desktop/frame%03d.jpg’ % frame) # Capture the images for frame in range(FRAMES):
# Note the time before the capture start = time.time() capture_frame(frame)
# Wait for the next capture. Note that we take into
# account the length of time it took to capture the
# image when calculating the delay time.sleep(
int(60 * 60 / FRAMES_ PER_ HOUR) - (time.time() v- start) )
TAKE YOUR STOP-MOTION 17 STUDIO TO THE NEXT LEVEL
At the risk of encouraging you to become the next Ivor Wood (creator of The Wombles, Paddington and Postman Pat, among others), it is possible to use the Raspberry Pi’s camera module for ambitious projects, as well as small ones. After all, this device photographs in high resolution so there is no reason not to adopt this setup and incorporate it into a working stop-motion studio with a miniature set.
Sharing your work through YouTube is a great idea, as well, especially as it will make it simple to add a soundtrack over the top, using YouTube’s browserbased editor.
Here’s the stop motion program in action — it’s a simple enough GUI to get your head around and gives you a nice preview window.