202 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE
The camera.capture function tells Python to save a still image, and it needs to know not
only what the image should be called but in what folder it should be saved. In this example,
you’re saving it to the desktop – find it by looking just below the Wastebasket. If the Thonny
window is in the way, just click and drag on the title bar to move it. Double-click on the file to
see the image you captured (Figure 8-8). Congratulations: you’ve programmed a camera!
5 Figure 8-8: Opening the captured image
Capturing moving video
As well as taking still images, you can capture video. Delete everything between the lines
camera.start_preview() and camera.stop_preview(), then type the following
under camera.start_preview():
camera.start_recording('/home/pi/Desktop/video.h264')
sleep( 10 )
camera.stop_recording()
The camera preview will appear, as before, but this time it will also be recorded to a file on
the desktop. Wait for the 10 seconds you’ve told Python to sleep – perhaps do a little dance in
front of the camera to make the video interesting – then, when the preview has closed, you’ll
find your video file on the desktop.
To play the video, simply double-click on the video.h264 file on your desktop. The video
will start playing – and if you did a dance, you’ll see it played back to you! After the video has
finished, the player software will quit with a friendly message in the Terminal. Congratulations:
you can now capture video using your Raspberry Pi Camera Module!