Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

3.1.1 Photograph Module¶

Introduction¶

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

Required Components¶

In this project, we need the following components.

../_images/photo1.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Camera Module

BUY

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 Access for Raspberry Pi.

Step 2: Check if the camera is enabled. For instructions, please refer to: Enable the Camera Interface.

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

cd ~/raphael-kit/python/

Step 4: 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 ~ directory.

Note

You can also open 3.1.1_PhotographModule.py in the ~/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
import os
user = os.getlogin()
user_home = os.path.expanduser(f'~{user}')

camera = PiCamera()

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

def main():
    camera.capture(f'{user_home}/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(f'{user_home}/my_photo.jpg')

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

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(f'{user_home}/my_photo%s.jpg' % i)