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.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
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 ~/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 ~ 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
#!/usr/bin/env python3
from picamera2 import Picamera2, Preview
import os
# Get the current user's login name
user = os.getlogin()
# Get the path to the user's home directory
user_home = os.path.expanduser(f'~{user}')
# Create a Picamera2 instance
camera = Picamera2()
# Retrieve the default preview configuration
preview_config = camera.preview_configuration
try:
# Set preview size and format
preview_config.size = (800, 600)
preview_config.format = 'XRGB8888'
# Start the camera preview in QTGL mode
camera.start_preview(Preview.QTGL)
# Start the camera
camera.start()
# Capture and save a photo to the user's home directory
camera.capture_file(f'{user_home}/my_photo.jpg')
except KeyboardInterrupt:
# Stop the camera preview if a KeyboardInterrupt (e.g., Ctrl+C) occurs
camera.stop_preview()
pass
Code Explanation
Imports the
Picamera2andPreviewclasses from the picamera2 library, and theosmodule for operating system interactions.#!/usr/bin/env python3 from picamera2 import Picamera2, Preview import os
Retrieves the current user’s login name and the path to their home directory.
# Get the current user's login name user = os.getlogin() # Get the path to the user's home directory user_home = os.path.expanduser(f'~{user}')
Creates an instance of the
Picamera2class and retrieves the default preview configuration.# Create a Picamera2 instance camera = Picamera2() # Retrieve the default preview configuration preview_config = camera.preview_configuration
Sets the size and format of the camera preview. The size is set to 800x600 pixels, and the format is set to
XRGB8888.try: # Set preview size and format preview_config.size = (800, 600) preview_config.format = 'XRGB8888' ...
Starts the camera preview in QTGL mode, which is a method for displaying the preview, and then starts the camera itself.
try: ... # Start the camera preview in QTGL mode camera.start_preview(Preview.QTGL) # Start the camera camera.start() ...
Capture an image from the camera, storing it in
~/.try: ... # Capture and save a photo to the user's home directory camera.capture_file(f'{user_home}/my_photo.jpg')
Note
You can use
camera.capture()function andforloop together to achieve continuous shooting. And use thedelayfunction to adjust the time interval for taking pictures.for i in 5: camera.capture(f'{user_home}/my_photo%s.jpg' % i)
This block of code handles a KeyboardInterrupt (such as Ctrl+C) by stopping the camera preview. The
passstatement is used to handle the exception without doing anything else.except KeyboardInterrupt: # Stop the camera preview if a KeyboardInterrupt (e.g., Ctrl+C) occurs camera.stop_preview() pass