In this tutorial, we see how to test and start using the Raspberry Pi Camera V2.
Below you will see different codes for the different use case in Python. Feel free to copy and past it in your own code.
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 camera copy the below command
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(10) camera.stop_preview()
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 90
, 180
, 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 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 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
- The Raspberry Pi Foundation official website – raspberrypi.org
Also, Check out: Pi hole ® Network-wide Ad Blocking