Raspberry Pi Camera V2 – Tutorial

Avatar Ammar | May 30, 2019

In this tutorial we see how to test and start using Raspberry Pi Camera V2.

Below you will see different codes for different use case in Python. Feel free to copy and past it in your own code.

raspberry pi camera tutorials

All codes below start with:
from picamera import PiCamera
from time import sleep
however you can see few options depends on what you are looking for and your setup

Python code:

To test the Pi Camera

To test the camera copy the below command

from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview()
sleep(10)
camera.stop_preview()

 

To rotate the Pi Camera

If your preview was upside-down, you can rotate it with the following code:

To rotate the PiCamera copy the below command "//un-hash the needed rotation angel//"

You can rotate the image by 90180, or 270 degrees, or you can set it to 0 to reset.

from picamera import PiCamera
from time import sleep

camera = PiCamera()

#camera.rotation = 90
camera.rotation = 180
#camera.rotation = 270

camera.start_preview()
sleep(10)
camera.stop_preview()

 

Save photos from the Pi Camera

Save photos from the PiCamera is the most common use for the Camera Module

from picamera import PiCamera
from time import sleep

camera = PiCamera()

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.

You can find the photo on the Desktop. Double-click the photo to open it:

 

Save Video from the Pi Camera

Save Video from the PiCamera

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_recording()
camera.stop_preview()

 

Now you’ve got started with the Camera Module, I Hope you enjoy this website and its content. feel free to suggest topics you would like to be covered.

 

 

Sources

1 Rate it

Written by Ammar


Comments

This post currently has no responses.

Leave a Reply