Getting started with picamera
The Camera Module is a great accessory for the Raspberry Pi, allowing users to take still pictures and record video in full HD.
Connect the Camera Module
First of all, with the Pi switched off, you'll need to connect the Camera Module to the Raspberry Pi's camera port, then start up the Pi and ensure the software is enabled.
Locate the camera port and connect the camera:
Start up the Pi.
Open the Raspberry Pi Configuration Tool from the main menu:
Ensure the camera software is enabled:
If it's not enabled, enable it and reboot your Pi to begin.
Now your camera is connected and the software is enabled, you can get started by trying out the camera preview.
Open Python 3 from the main menu:
Open a new file and save it as
camera.py. It's important that you do not save it as
Enter the following code:
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(10) camera.stop_preview()
Save with Ctrl + S and run with F5. The camera preview should be shown for 10 seconds, and then close. Move the camera around to preview what the camera sees.
The live camera preview should fill the screen like so:
Note that the camera preview only works when a monitor is connected to the Pi, so remote access (such as SSH and VNC) will not allow you to see the camera preview
If your preview was upside-down, you can rotate it with the following code:
camera.rotation = 180 camera.start_preview() sleep(10) camera.stop_preview()
You can rotate the image by
270degrees, or you can set it to
You can alter the transparency of the camera preview by setting an alpha level:
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview(alpha=200) sleep(10) camera.stop_preview()
alphacan be any value between
The most common use for the Camera Module is taking still pictures.
Amend your code to reduce the
sleepand add a
camera.start_preview() sleep(5) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()
It's important to sleep for at least 2 seconds before capturing, to give the sensor time to set its light levels.
Run the code and you'll see the camera preview open for 5 seconds before capturing a still picture. You'll see the preview adjust to a different resolution momentarily as the picture is taken.
You'll see your photo on the Desktop. Double-click the file icon to open it:
Now try adding a loop to take five pictures in a row:
camera.start_preview() for i in range(5): sleep(5) camera.capture('/home/pi/Desktop/image%s.jpg' % i) camera.stop_preview()
icontains the current iteration number, from
4, so the images will be saved as
image1.jpg, and so on.
Run the code again and hold the camera in position. It will take one picture every five seconds.
- Once the fifth picture is taken, the preview will close. Now look at the images on your Desktop and you'll see five new pictures.
Now you've used the camera to take still pictures, you can move on to recording video.
Amend your code to replace
camera.start_preview() camera.start_recording('/home/pi/video.h264') sleep(10) camera.stop_recording() camera.stop_preview()
Run the code; it will record 10 seconds of video and then close the preview.
To play the video, you'll need to open a terminal window by clicking the black monitor icon in the taskbar:
Type the following command and press Enter to play the video:
- The video should play. It may actually play at a faster speed than what has been recorded, due to
omxplayer's fast frame rate.
At the beginning, you created a
camera object with
camera = PiCamera(). You can manipulate this
camera object in order to configure its settings. The camera software provides a number of effects and other configurations you can apply. Some only apply to the preview and not the capture, others apply to the capture only, but many affect both.
The resolution of the capture is configurable. By default it's set to the resolution of your monitor, but the maximum resolution is 2592 x 1944 for still photos and 1920 x 1080 for video recording. Try the following example to set the resolution to max. Note that you'll also need to set the frame rate to
15to enable this maximum resolution:
camera.resolution = (2592, 1944) camera.framerate = 15 camera.start_preview() sleep(5) camera.capture('/home/pi/Desktop/max.jpg') camera.stop_preview()
The minimum resolution allowed is 64 x 64. Try taking one at that resolution.
You can easily add text to your image with
annotate_text. Try it:
camera.start_preview() camera.annotate_text = "Hello world!" sleep(5) camera.capture('/home/pi/Desktop/text.jpg') camera.stop_preview()
You can alter the brightness setting, which can be set from
100. The default is
50. Try setting it to another value:
camera.start_preview() camera.brightness = 70 sleep(5) camera.capture('/home/pi/Desktop/bright.jpg') camera.stop_preview()
Try adjusting the brightness in a loop, and annotating the display with the current brightness level:
camera.start_preview() for i in range(100): camera.annotate_text = "Brightness: %s" % i camera.brightness = i sleep(0.1) camera.stop_preview()
Similarly, try the same for the contrast:
camera.start_preview() for i in range(100): camera.annotate_text = "Contrast: %s" % i camera.contrast = i sleep(0.1) camera.stop_preview()
You can set the annotation text size with the following code:
camera.annotate_text_size = 50
Valid sizes are
160. The default is
You can also alter the annotation colours. First of all, ensure that
Coloris imported by amending your
importline at the top:
from picamera import PiCamera, Color
Then amend the rest of your code as follows:
camera.start_preview() camera.annotate_background = Color('blue') camera.annotate_foreground = Color('yellow') camera.annotate_text = " Hello world " sleep(5) camera.stop_preview()
You can use
camera.image_effectto apply a particular image effect. The options are:
deinterlace2. The default is
none. Pick one and try it out:
camera.start_preview() camera.image_effect = 'colorswap' sleep(5) camera.capture('/home/pi/Desktop/colorswap.jpg') camera.stop_preview()
Try looping over the various image effects in a preview to test them out:
camera.start_preview() for effect in camera.IMAGE_EFFECTS: camera.image_effect = effect camera.annotate_text = "Effect: %s" % effect sleep(5) camera.stop_preview()
You can use
camera.awb_modeto set the auto white balance to a preset mode to apply a particular effect. The options are:
horizon. The default is
auto. Pick one and try it out:
camera.start_preview() camera.awb_mode = 'sunlight' sleep(5) camera.capture('/home/pi/Desktop/sunlight.jpg') camera.stop_preview()
You can loop over the available auto white balance modes with
You can use
camera.exposure_modeto set the exposure to a preset mode to apply a particular effect. The options are:
fireworks. The default is
auto. Pick one and try it out:
camera.start_preview() camera.exposure_mode = 'beach' sleep(5) camera.capture('/home/pi/Desktop/beach.jpg') camera.stop_preview()
You can loop over the available exposure modes with
Now you've got started with the Camera Module, you could try adding GPIO controls using GPIO Zero, integrate with Minecraft Pi, or even post your pictures to Twitter! Try some more camera resources:
There's also an infrared version of the camera (called Pi NoIR) which gives you everything the regular Camera Module offers, with one difference: it doesn't use an infrared filter. This gives you the ability to see in the dark with infrared lighting. See the Infrared bird box resource for making the most of the Pi NoIR camera.
Also, see the extensive picamera documentation for more information.