How to use Raspberry Pi Camera via Python
There are lots of ways to install and use Pi camera, but at this post we're going to handle this with Python!
- Enable Pi camera via Raspberry Pi Configuration
- Testing taking still image and recording working
- Install Python drivers for Pi cam (http://picamera.readthedocs.io/en/release-1.9)
- Sample Python script to test Pi cam
- Enable Pi camera via Raspberry Pi Configuration
sudo raspi-config
and choose 'Enable Camera', enter and finish.
- Testing taking still image and recording working
To test capturing still image,
raspistill -o testpicture.jpg
To test video capturing,
raspivid -o testvideo.h264
- Install Python drivers for Pi cam (http://picamera.readthedocs.io/en/release-1.9)
Now, we're ready to install Python drivers
sudo apt-get install python-picamera
if you're working on Python3,
sudo apt-get install python3-picamera
More information and options are available at PICAMERA
- Sample Python script to test Pi cam
You can write and test Python script with Raspberry Pi Camera from now on.
Start a preview for 10 secs
import time import picamera
camera = picamera.PiCamera()
try:
camera.start_preview()
time.sleep(10)
camera.stop_preview()
finally:
camera.close()
Capturing to a file
import time
import picamera
with picamera.PiCamera() as camera:
camera.resolution = (1024, 768)
camera.start_preview()
# Camera warm-up time
time.sleep(2)
camera.capture('sampleImage.jpg')
Recoding video to a file
import picamera
with picamera.PiCamera() as camera:
camera.resolution = (640, 480)
camera.start_recording('testing_video.h264')
camera.wait_recording(60)
camera.stop_recording()
There are tons of useful examples here at PICAMERA
please look into it!
We've just installed and learned how to use Pi camera basically.
so now fully ready to extend your projects with this raspberry pi with awesome camera!
Leave a Reply
Want to join the discussion?Feel free to contribute!