17017295012_85894daf50_z_d.jpg


The Raspberry Pi is capable of many great things but one of the most accessible and rewarding is something incredibly simple, taking a photograph. We take this for granted as a camera comes with our cell phone, but how do I take a photograph?

With our cell phone we open the camera application, frame the shot and then touch the screen to take a picture. But behind the scenes there are many steps to be taken in sequence in order to capture an image or a video.


In this tutorial we will learn via three projects how to use the official Raspberry Pi CameraRaspberry Pi Camera to take a photograph using a command from the terminal, then using the Python AP and finally we shall record video in super slow motion!

The code and commands used in this tutorial will also work with the Pi NoirPi Noir camera to capture low light images as essentially they are the same camera but the Pi Noir does not have an infra red (IR) filter which when used with an IR light source, such as an IR LED, it can see in the dark! Pi Noir is a great piece of kit and it enables nocturnal projects to be created such as a nature camera or CCTV system.


19120644886_70229e6990_z_d.jpg


All of these projects can be completed with either of the cameras, but remember that the Pi Noir requires an IR light source in order for it to take photos/video in low light.

Raspberry Pi CameraRaspberry Pi Camera

Pi NoirPi Noir


All of the projects can be completed with any model of Raspberry Pi but for best results we recommend the Raspberry Pi 2Raspberry Pi 2


Getting started


17017296792_7ebc444898_z_d.jpg


Installing the camera is quick and easy and to start the installation you will need to power down your Raspberry Pi and remove the power cable from the Pi. Next you will need to locate the port marked CAMERA between the HDMI and Ethernet ports.

Carefully lift the top and bottom edges of the connector vertically, they will gently slide up and then stop when in place. Be careful as the connector is rather fragile, you will only need to use a little pressure to lift the cover of the port

Remove your camera from the box and slide the ribbon connector into the CAMERA connector, ensure that the blue edge faces the ethernet port. Be careful handling the camera it is rather fragile and sensitive to static. With the ribbon inside the connector gently push the connector edges back down, locking the ribbon in place. With the camera hardware installed you can now power up your Raspberry Pi.

You should now boot up your Raspberry Pi to the desktop and when ready open up a new terminal and type the following, remember to press Enter to run the command


sudo raspi-config

 

17215355418_e6b3bf3382_z_d.jpg

 

Raspi-config is a configuration suite to automate setting up your Pi, in this case we want to enable the camera, which is option 5.

At the menu navigate to Enable Camera and press enter. In the next screen select Enable, and then navigate to Finish, which will prompt you to reboot your Raspberry Pi. Do this and allow the Pi to reboot, thus enabling your camera.


17403158425_1d3fdcccbe_z_d.jpg


With the camera enabled we next need to check that it has been configured correctly and to do that we use the command raspistill in  LXTerminal.


raspistill -o image.jpg


The camera will now activate, we can see this via a red light on the unit, and a preview will appear on screen for a few seconds, compose yourself and it will take the picture. You can then open the picture via the file manager. It should be in /home/pi or in the directory where you used the command. If this does not work, check that you have connected the camera correctly and that raspi-config shows the camera as enabled. Remember do not remove the camera from the connector while the Raspberry is on, it will cause damage to the camera.


19170737145_d6fcd2e4e2_z_d.jpg


With a successful test of the camera we now need to install the latest Python API for the camera. Chances are that you are using the latest version of Raspbian but it is always prudent to update your software. For this next step your Pi will require an Internet connection and the easiest way to do that is by using an Ethernet cable direct to your router, once connected open a terminal.

In a terminal type the following, remember to press Enter to run the command.


sudo apt-get install python-picamera python3-picamera python-rpi.gpio


Once complete we are ready to take a picture using Python 3.


Project 1: Taking One Picture Using Python 3 (Both Cameras)


If you wish to capture images in low light or at night, then please use an infra red LED connected to the Raspberry Pi 3v3 and GND pins via a 220 ohm resistor. All models of Raspberry Pi have two 3v3 pins, located at Pin 1 and Pin 17.


To open Python 3 go to the main menu, in the latest Raspbian this is the top left of the screen. Left click and navigate to Programming and select Python 3. An application called IDLE will open and you will see the IDLE Python shell, in here we can write code that will produce instant results, we don’t want that so lets click on File >> New Window to open a blank document where we shall store our code.


Our code is relatively simplistic for this test.

Firstly we import two libraries, time which is used to control the duration of the preview and picamera which is the Python API for the camera.



We then move to the main body of code where we start a preview window, wait 2 seconds before capturing the image as ‘foo.jpg’ in the same directory as where the code is ran.


Save you code as camera_test.py and click on Run >> Run Module in IDLE to run the code. You should see a preview window appear, then resize after 2 seconds to indicate that a picture has been taken. Open the File Manager and navigate to where you saved the Python code. Your photo will be ready to view. Remember that every time the code is ran the original file is overwritten so make sure to backup or rename the file if you wish to keep it.


Project 2: Creating a timelapse (Both Cameras)


If you wish to capture images in low light or at night, then please use an infra red LED connected to the Raspberry Pi 3v3 and GND pins via a 220 ohm resistor. All models of Raspberry Pi have two 3v3 pins, located at Pin 1 and Pin 17.


In Project 1 we captured a single file that was overwritten each time the code was ran. But what if you wish to create a sequence of images? Timelapse videos take one image every minute, hour,day (the choice is yours) and are then stitched together to form a video that accelerates through that time period. For example a plant growing from seed. We are going to create a timelapse to capture an image every 5 minutes as a separate file.



 

This code looks very similar to Project 1 but it uses a for loop to capture an image every 30 seconds and automatically create a filename by setting the variable i to 0 and then incrementing it by 1 every time the loop is run. We use the try..except construction to instruct the code to attempt the code in the try section first. If there is an error or if the user presses CTRL + C to close the application then “Exiting’ is printed to the screen.

Save the code as timelapse.py and compose your shot. When ready click on Run >> Run Module to launch the code. The camera will come to life and start a preview window before taking your picture. It will then wait 30 seconds before repeating the process. To stop at any time press CTRL + C on your keyboard.


18984655369_2bbc319834_m_d.jpg18548267074_3bd4dfed5e_m_d.jpg18984655729_59b4146fb3_m_d.jpg


Project 3: Slow Motion (Both cameras)


For our final project we shall push the camera to its limits and use it to record 90 frames per second!

At the time of writing the Python picamera library is not able to record at such speed but we can use the raspivid command at the terminal instead.


in a terminal type the following and press Enter to run


raspivid -w 640 -h 480 -fps 90 -t 100000 -o slowmo.h264


This command will record 10 seconds of video at a resolution of 640 x 480 pixels and at 90 frames per second. The camera will provide a preview window for you to compose a shot and once it has finished recording it will return the terminal control to you.


To play the video, in the terminal type


omxplayer slowmo.h264


You should see the video appear in super slow motion!



This is great fun to use with fast moving projects such as robotics and recording races.


Just for fun I created a simple Python script to automate the process. It uses the os library to enable Python to run terminal commands. Then an infinite loop contains the code to be constantly ran. It creates a variable called name which is used to store the user input for the filename. The input is wrapped inside of a str() function which converts the data captured into a string. The next line handles attaching the suffix “.h264” to the filename provided and this updates the name variable. Then using the os.system function we call the raspivid terminal command to record the video. You can see at the end of the line that the name variable is used for the filename. The last line of code handles the playback of the video.


 

So there we have three projects for the Raspberry Pi Camera and the Pi Noir Camera.


Happy hacking!