The flexibility of the Raspberry Pi knows no bounds, and just when you think you’ve reached your limits, something else comes along. It could be due to a great idea you or someone else had, or inspired by a recently released device extension component.
One of the first extensions you should buy for your Raspberry Pi is the camera module. With a dedicated connector, the camera can be used for a variety of tasks. Let’s look at them.
First: turn on the camera
Make sure you have connected your Raspberry Pi camera to your mini computer. Then boot up the device and log in (we’re assuming you’re using the default Raspberry Pi OS, Raspbian. ). At the command line, type
From the menu select » Turn on camera» .
From here select enable, then Finish and Yes to reboot.
When your Pi restarts, log in again and, when prompted, type
raspistill –o image.jpg
This will capture your first image which you can view in the GUI. If you are not already using the terminal from the GUI, you must switch to this using the command
The following commands can be run in the Terminal and the results checked in the Raspbian file manager. You can take as many photos as you like with this command, although note that the filename, image.jpg, will need to be changed each time it’s iterated through the command to avoid overwriting the previous image.
Let’s go a little further and instruct Pi to take a temporary photo after one keystroke.
Start by installing Python support for the camera.
sudo apt-get install python-picamera python3-picamera
After that enter
sudo idle &
This will launch the Python environment. Python appears regularly in Raspberry Pi manuals and is a remarkably easy language to understand. For more help, we suggest you check out our five best sites to learn Python best sites to learn Python programming best sites to learn Python and visit Lynda.com if you are interested in further developing your Python skills.
Go to File > New Window to open a text editor and enter the following code:
import time import picamera with picamera.PiCamera() as camera: camera.start_preview() time.sleep(0) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()
Use File > Save to save your work by naming it something like timedsnap.py. When you’re ready to run the script, go to Run > Run Module or just click F5 .
We can use this same script — with some modifications — to use the Raspberry Pi camera module for other projects.
PiCamera with timer
The same script can be used with a slight modification to create a camera with a countdown timer, which is a huge benefit for any selfie lover. Let’s face it, this is a Raspberry Pi, so you might find some way to put a case and camera on a selfie stick and go out in public with it.
To add a 5 second countdown, change the line
When you’re done, don’t forget to save and hit F5 to start the countdown. Say «Cheese!»
Record Video with Raspberry Pi Camera
Taking photos is one thing, but what about video? Just like with a smartphone camera or a standard desktop webcam (which is basically what a Pi camera is, just without the case), you can also record video.
On the command line, modify the script as follows:
import time import picamera with picamera.PiCamera() as camera: camera.start_preview() camera.start_recording('/home/pi/Desktop/video.h264') time.sleep(30) camera.stop_recording() camera.stop_preview()
You will notice that I have set the value time.sleep() by 30, which means the script will start recording, wait 30 seconds, and then stop. Save this script as videocapture.py and press F5 to run.
Pay attention to the use of the function camera.start_recording() . This will save the footage as a file named video.h264 a high-definition video clip that can be opened from the Raspbian desktop. The best way to do this is to go to your Desktop folder (or whatever file path you chose in the script above), press F4 to open a terminal, and type
Add the right Raspberry Pi battery and display and you have a compact camcorder!
slow motion shooting time lapse shooting time lapse slow motion Smartphone cameras have exploded in popularity over the past few years, making what was once the province of professional photographers available to just about anyone.
The disadvantage of using a smartphone for such a photo is obvious; it’s time consuming and resource intensive, which you might need to, well, make and receive phone calls. A Raspberry Pi with a camera connected is a good alternative, and with a battery connected it can be as portable and versatile as an Android or iPhone app, and makes more sense than just using the Pi as a slow start for a DSLR.
Before proceeding, install ffmpeg:
sudo apt-get install ffmpeg
Then use this Python script to capture images at intervals:
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() - start) )