1 of 9

Camera Workshop

with Ben Nuttall

2 of 9

Test the Camera

raspistill -o image.jpg

3 of 9

Capturing with Python

import time

import picamera

with picamera.PiCamera() as camera:

camera.start_preview()

time.sleep(5)

camera.capture('/home/pi/Desktop/image.jpg')

camera.stop_preview()

4 of 9

Capturing with a button (wiring)

5 of 9

Capturing with a button (code)

import time

import picamera

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)

with picamera.PiCamera() as camera:

camera.start_preview()

GPIO.wait_for_edge(17, GPIO.FALLING)

camera.capture('/home/pi/Desktop/image.jpg')

camera.stop_preview()

6 of 9

Capture on a timer

import time

import picamera

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)

with picamera.PiCamera() as camera:

camera.start_preview()

GPIO.wait_for_edge(17, GPIO.FALLING)

time.sleep(5)

camera.capture('/home/pi/Desktop/image.jpg')

camera.stop_preview()

7 of 9

Recording Video

import time

import picamera

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)

with picamera.PiCamera() as camera:

camera.start_preview()

GPIO.wait_for_edge(17, GPIO.FALLING)

camera.start_recording('/home/pi/Desktop/video.h264')

time.sleep(1)

GPIO.wait_for_edge(17, GPIO.FALLING)

camera.stop_recording()

camera.stop_preview()

8 of 9

Recording the past

...

with picamera.PiCamera() as camera:

stream = picamera.PiCameraCircularIO(camera, seconds=20)

camera.start_preview()

camera.start_recording(stream, format='h264')

GPIO.wait_for_edge(17, GPIO.FALLING)

camera.stop_recording()

camera.stop_preview()

for frame in stream.frames:

if frame.header:

stream.seek(frame.position)

break

with io.open('/home/pi/Desktop/video.h264', 'wb') as output:

while True:

data = stream.read1()

if not data:

break

output.write(data)

9 of 9

Fin!

Picamera documentation (including lots of recipes!)

http://picamera.readthedocs.org/

Raspberry Pi Learning Resources (including this workshop)

http://www.raspberrypi.org/resources/learn/

Dave Jones (picamera author and database know-it-all)

dave@waveform.org.uk