What you will learn ● How to connect the camera module ● How to use the Python picamera module to control the camera ● How to use the GPIO Zero Button interface to control the camera ● How to use loops to repeat commands ● How simple changes to code make different projects ● Ideas for camera projects
Take a selfie from picamera import PiCamera from time import sleep with PiCamera() as camera: camera.start_preview() sleep(3) camera.capture("/home/pi/image.jpg") camera.stop_preview() Save and run: Ctrl + S F5
Add GPIO Button code from picamera import PiCamera from gpiozero import Button from time import sleep button = Button(17) with PiCamera() as camera: camera.start_preview() button.wait_for_press() sleep(3) camera.capture("/home/pi/image2.jpg") camera.stop_preview() Save and run: Ctrl + S F5
Add a loop with PiCamera() as camera: camera.start_preview() for i in range(5): button.wait_for_press() sleep(3) camera.capture("/home/pi/image%s.jpg" % i) camera.stop_preview() Save and run: Ctrl + S F5
What's the difference? for i in range(5): button.wait_for_press() sleep(3) camera.capture("/home/pi/image%s.jpg" % i) button.wait_for_press() for i in range(5): sleep(3) camera.capture("/home/pi/image%s.jpg" % i)
Custom capture function i = 0 def capture(): global i camera.capture("/home/pi/image%s.jpg" % i) i += 1 right.when_pressed = capture Alternatively, use the datetime module