The Raspberry Pi boards include a CSI port to connect a camera. In this tutorial, you’ll learn how to use and connect a camera to Raspberry Pi boards.
For this tutorial, you need to install the Raspbian operating system on your board.
What You Will Learn
Launch the Camera with Raspberry Pi
Launching the Camera in Raspbian Desktop
Launching the Camera in Raspbian Light
1- Connect the camera to the Raspberry Pi board.
2- Type the following command in the terminal to enter Raspberry pi settings.
Taking Photo using the Raspberry Pi Camera
Taking Photo using Raspbian Terminal
The libraries required for the camera are installed on Raspbian OS by default.
You can take a photo by the raspistbill command. To learn how to use this command, type it in the raspbian terminal.
For example, with the following code, after 2 seconds, a picture is taken and stored as image1.
raspistill -o image1.jpg -t 2000
Taking Pictures Using Python Script
The following code enables the camera in preview mode for 5 seconds, then take a photo and save it as “image” on your desktop.
from camera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(5) #at least 2 seconds recommended camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()
Adding text to a photo
You can add any text to the photo using the annotate_text command. See the following example:
camera.start_preview() camera.annotate_text = "Electropeak" sleep(5) camera.capture('/home/pi/Desktop/text.jpg') camera.stop_preview()
You can change the text color using color class and annotate_background and annotate_foreground commands.
You can also change the text size with annotate_text_size command:
Adding Effect to a photo:
The Picamera library has several effects for applying to photos. You can apply them using camera.image_effect = name of effect command.
from picamera import PiCamera, Color from time import sleep camera = PiCamera() camera.start_preview() camera.image_effect = 'sketch' sleep(5) camera.capture('/home/pi/sketch.jpg') camera.stop_preview()
none, negative, solarize, sketch, denoise, emboss, oilpaint, hatch, gpen, pastel, watercolor, film, blur, saturation, colorswap, washedout, posterise, colorpoint, colorbalance, cartoon, deinterlace1, and deinterlace2
you can find the complete description of effects here.
Recording Video with Raspberry Pi Camera:
Recording Video Using Raspbian Terminal:
You can use raspivid command to start recording a video.
You will see a brief description of how to use raspvid by typing this command in the terminal.
for example, the following code records video for 10 seconds and save it as video_test and with .h264 format.
raspivid -o video_test.h264 -t 10000
Recording Video Using Python Script:
You can enable the camera in recording mode and start recording using the Picamera library and strat_recording command.
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() camera.start_recording('/home/pi/Desktop/video.h264') sleep(10) camera.stop_preview()
- Try to control the camera with a button. Each time the button is pressed, the camera takes a photo and saves it.
- Try to control the camera with the PIR sensor. When a motion is observed in the environment, start recording automatically for a few seconds and store it on the Raspberry Pi.