3.1.1 Photograph Module

Introduction

In this kit, equipped with a camera module, let’s try to take a picture with Raspberry Pi.

Components

../_images/photo1.png

For more information on how to connect the camera module and its configuration, please refer to Camera Module.

Experimental Procedures

Step 1: Go into the Raspberry Pi Desktop. You may need a screen for a better experience, refer to: Connect your Raspberry Pi. Or access the Raspberry Pi desktop remotely, for a detailed tutorial please refer to Remote Desktop.

Step 2: Open a Terminal and get into the folder of the code.

cd /home/pi/raphael-kit/python/

Step 3: Run.

sudo python3 3.1.1_PhotographModule.py

After the code runs, the camera will take a photo. Now you can see the photo named my_photo.jpg in the /home/pi directory.

Note

You can also open 3.1.1_PhotographModule.py in the /home/pi/raphael-kit/python/ path with a Python IDE, click Run button to run, and stop the code with Stop button.

If you want to download the photo to your PC, please refer to Filezilla Software.

Code

from picamera import PiCamera

camera = PiCamera()

def setup():
    camera.start_preview(alpha=200)

def main():
    camera.capture('/home/pi/my_photo.jpg')
    while True:
        pass

def destroy():
    camera.stop_preview()

if __name__ == '__main__':
    setup()
    try:
        main()
    except KeyboardInterrupt:
        destroy()

Code Explanation

from picamera import PiCamera

camera = PiCamera()

Import the picamera library and instantiate the PiCamera class to use the camera module.

start_preview(**options)

Show the preview overlay and change the transparency level of the preview with alpha - from 0 to 255. This method starts a camera preview as an overlay on the Pi’s primary display (HDMI or composite). By default, the renderer will be opaque and fullscreen.

This means the default preview overrides whatever is currently visible on the display. More specifically, the preview does not rely on a graphical environment like X-Windows (it can run quite happily from a TTY console); it is simply an overlay on the Pi’s video output. To stop the preview and reveal the display again, call stop_preview() . The preview can be started and stopped multiple times during the lifetime of the PiCamera object.

camera.capture('/home/pi/my_photo.jpg')

Capture an image from the camera, storing it in /home/pi/.

Note

You can use camera.capture() function and for loop together to achieve continuous shooting. And use the delay function to adjust the time interval for taking pictures.

for i in 5:
    camera.capture('/home/pi/my_photo%s.jpg' % i)