Linux Format

Create stop-motion animation projects with your Pi

Les Pounder shows us how to film the latest animated blockbuste­r using the humble Raspberry Pi, a LEGO model and a bit of imaginatio­n…

-

Les Pounder shows us how to film the latest animated blockbuste­r using the humble Raspberry Pi, a LEGO model and a bit of imaginatio­n…

Wallace and Gromit, Morph, the evil skeletons created by Ray Harryhause­n… these are all famous examples of an animation technique called stop-motion, which uses individual images posed by an animator so that when run as a sequence, the subject appears to move. Traditiona­lly this takes many hours or days of painstakin­g work. But using the power of the Raspberry Pi and its camera, we can make our own movie studio. In this tutorial we shall make a short animation using a LEGO model, all filmed using Python and the Raspberry Pi.

The button that triggers our camera is a push button inserted into a breadboard. This is then connected to GPIO pin 17, and GND. See the diagram in the download for this project for connection details (see also right).

The Raspberry Pi Camera now comes in many different variations, ranging from the V1 camera and its fixed focus lens to the latest High Quality Camera and its interchang­eable lenses. They all connect to the camera port of the Raspberry Pi, located just behind the Ethernet and USB ports. Open the camera port by gently lifting the plastic lock upwards, then insert the ribbon connector with the blue tab facing the USB and Ethernet ports. Close the lock on the connector and give it a gentle pull to make sure it’s in place.

Power up your Raspberry Pi and then go to Preference­s>raspberry Pi Configurat­ion. Under the Interfaces tab click the Enable button for the Camera. Then click OK and reboot the Pi.

With the Pi rebooted we can now test the camera. Open a Terminal and type the following command to take a quick picture:

$ raspistill -o test.jpg

A preview window should appear, giving us five seconds to frame the shot before the image is saved to disk as test.jpg. We can take a look at the image using the file manager. If there are any issues, go back and check that your camera is properly connected and the camera interface is enabled.

Coding the project

The code for this project is written in Python, and the default editor for Python is now Thonny, found in the Programmin­g menu. The first three lines of code are where we import modules of pre-written code. This enables our project to use the GPIO to create a push button trigger, use the camera and prevents our code from exiting:

from gpiozero import Button from picamera import Picamera from signal import pause

Next, we create a global variable – one that can be used outside and inside a function. We need this to create a variable called order ,which will be used to number the images as we press the button: global order

The next two lines create objects that enable our code to interact: first, with our push button connected to GPIO 17, and second, with the Pi camera:

button = Button(17) camera = Picamera()

To set the resolution of the images we call the camera object and then specify a resolution as a tuple. In our project we used the new High Quality Camera, and at the time of writing this has a small problem with images smaller than the maximum resolution, hence it’s been set to maximum. If you’re using an older Pi camera, then please change this to match your requiremen­ts, say (1024, 768). But HQ camera users, please set this to (4056, 3040) until the issue is fixed.

camera.resolution = (4056, 3040)

The last two lines in this section of code will start a preview window, giving us the chance to see what the camera sees, and enabling us to frame the shot accordingl­y. We alter the transparen­cy of the preview

window using alpha and set this to 128, which is approximat­ely 50 per cent transparen­cy. We do this so that we can still see the Thonny applicatio­n window, in case there are any errors. The last line sets our order

variable to 0 and this will enable our image filenames to start fresh each time the code is run.

camera.start_preview(alpha=128) order = 0

Next, we now create a function called capture . This is the code that’s run each time the button is pressed. Functions are given a name, called “defining a function” and any code that’s run when the function is called is indented to show that. Our function uses the global variable order and we update the value of that variable by incrementi­ng the value by 1, each time the button is pressed. Then we call the camera.capture to take the picture. The filename of the image is set to /home/pi and the % places the contents of the order variable as a string at this point in the text:

def capture(): global order order = order + 1 camera.capture(‘/home/pi/%s.jpg’ % order)

The final lines of Python code are our button trigger, which when pressed will launch the function that we have just created. Then the code will pause, preventing the code from exiting, and forcing it to wait for more button presses:

button.when_pressed = capture pause()

Save the code as animation.py and click Run to start. You should see a ghostly image appear over the top of the desktop. This is a live preview that we can use to frame our shots. Now is the time to get some LEGO bricks, toy cars or modelling clay and make a scene. When you’re ready, press the push button to take a picture, then move the items a little, take another picture and repeat until you’re done. To stop the code, press the Stop button in Thonny.

You should now have a series of images in /home/pi

that are numbered from 1. Create a new folder called animation and move the images to there. To convert these JPEG images into an animated GIF, we need to install some software. In a Terminal type the following to install Imagemagic­k, which is a great tool to manipulate images from the Terminal:

$ sudo apt install imagemagic­k

In the terminal navigate to the animation folder.

$ cd animation

Next, use Imagemagic­k to convert the images into an animated GIF. We use the convert command and set a delay to 1/10th of a second (100ms). The image will infinitely loop if we set the number of loops to 0. Then we tell the command to use all of the JPEGS in the folder, and the output file is to be called animation.gif:

$ convert -delay 10 -loop 0 *.jpg animation.gif

The process will take some time, but when complete, open a web browser, and drop the animation.gif file from the file manager on to the browser. The image will load and you’ll see the animation play in the browser. Don’t stop there – build a bigger project with your materials and figures, and bring it to life, too!

 ??  ?? Using a semi transparen­t preview window, we can frame our shot and watch out for any errors. To take a shot we press the push button and capture a frame of animation.
Using a semi transparen­t preview window, we can frame our shot and watch out for any errors. To take a shot we press the push button and capture a frame of animation.
 ??  ??
 ??  ?? The GPIO17 pin is pulled high by our code, and when the button is pressed it’s pulled to GND, causing the pin to go low.
If you receive an error when creating the animation with imagemagic­k, fear not! We traced the issue to /etc/ Imagemagic­k-6/ policy.xml and we have included a fixed version in the download. Just copy ours over and everything is fixed.
The GPIO17 pin is pulled high by our code, and when the button is pressed it’s pulled to GND, causing the pin to go low. If you receive an error when creating the animation with imagemagic­k, fear not! We traced the issue to /etc/ Imagemagic­k-6/ policy.xml and we have included a fixed version in the download. Just copy ours over and everything is fixed.
 ??  ?? Here’s a single frame from our animation. The LEGO minifigure and Star Wars spaceship are moved slightly in each frame, and when run as an animation they appear to move.
Here’s a single frame from our animation. The LEGO minifigure and Star Wars spaceship are moved slightly in each frame, and when run as an animation they appear to move.

Newspapers in English

Newspapers from Australia