How To Use Picamera2 to Take Photos With Raspberry Pi
when Raspberry Pi OS migration From Debian Buster base, bullseye, the transition was not the smoothest. For years, the Raspberry Pi OS used his three tools to access the official his Raspberry Pi camera. The first two were raspistill / raspivid, which provided camera control and access via the Linux terminal.
It was a powerful and flexible means of manipulating the camera. Create video effects and stream videos without extra work. Another avenue was a community-created project called PiCamera.originally created by Dave Jones, Piccamera It has grown from a community project to an essential tool. Picamera provided a pure Python means of interacting with the camera. Also, being Python-based, I was able to mix cameras in my project.
Unfortunately Picamera broke with the move to Bullseye. Raspberry Pi LTD “Legacy” version of Buster With Picamera and security updates. This was a stopgap measure while the developer worked on his Picamera2.with September 2022 release of Raspberry Pi OS Now you have a working Picamera2 module that you can use in your project.
In this how-to you will learn how to use Pretty nice API for Picamera2 [pdf] It captures images, records video, manipulates GPIOs, and reacts to inputs as a means of capturing images.
for the project you need
Connecting the Raspberry Pi camera
The Raspberry Pi camera is best raspberry pi accessories As long as the Pi is with us, it’s pretty much the same. Almost every model of Raspberry Pi has a camera (CSI) connector (except for the first model of Raspberry Pi Zero), which means that the camera has quickly become a must-have accessory for the Pi. The same is still true. Official HQ camera It offers much better image quality and a range of interchangeable lenses.
Connecting the official camera to the Raspberry Pi is easy. Please follow these steps:
3. insert cable Make sure the blue tab faces the USB/Ethernet port.
Four. Gently slide the tab down Lock the cable in place.
Five. Camera fixing/mounting Don’t flop and touch the Pi or its GPIO. One way is to use modeling clay/blue tack.
Installing the Picamera2 software
1. Start your Pi.
2. open terminal When Update installed software.
sudo apt update
sudo apt upgrade -y
3. Install the Picamera2 Python3 module. For the latest Raspberry Pi OS release (September 2022 or later), it is pre-installed, but this command will update the version to the latest release.
sudo apt install -y python3-picamera2
Take a picture with Picamera2
Taking pictures with Picamera2 is the most basic task that this module can perform. By design, it’s made to be easy to use, but beneath that simplicity lies a complex set of modules that can be tweaked to suit your needs.
In this project, we use Preview to frame the shot and capture the image.
1. Open Tony. You can find it in the main menu.
2. In the new file, Import the Picamera2 module with the preview class. On a new line, import the time module. The Picamera2 module provides camera control and time is used to control how long the preview image is displayed on the screen.
from picamera2 import Picamera2, Preview
import time
3. Create an object picam2. This references the Picamera2 module and is used to control the camera.
picam2 = Picamera2()
Four. create a new object camera_config When Use this to set the still image resolution (main) to 1920 x 1080 and a “low resolution” image of size 640 x 480. This low-res image is used as a preview image when framing the shot.
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
Five. Load the configuration.
picam2.configure(camera_config)
6. Start preview window When Launch the camera.
picam2.start_preview(Preview.QTGL)
picam2.start()
7. Pause the code for 2 seconds.
time.sleep(2)
8. capture image When Save it as test.jpg.
picam2.capture_file("test.jpg")
9. Save the code as camera-test.py When [実行]Click to get started. A preview window appears. Use this to frame your shots. If 2 seconds is too slow, change the delay as needed.
Ten. Open system file manager When Double-click test.jpg to view the image.
complete code listing
from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(2)
picam2.capture_file("test.jpg")
Record video with Picamera2
HD video recording is now commonplace. The same is true for the Raspberry Pi, thanks to numerous models of official (and unofficial) cameras. Picamera2 allows you to record videos in different resolutions using different encoders.
This project shows how to record a simple 1080P video stream while previewing the stream in a low resolution window.
1. open tony When Create a new file. You can find Thonny in the main menu.
2. Import H264 encoder from Picamera2 module.
from picamera2.encoders import H264Encoder
3. Import the Picamera2 module with the preview class. Then import the time module.
from picamera2 import Picamera2, Preview
import time
Four. Create an object picam2. This references the Picamera2 module and is used to control the camera.
picam2 = Picamera2()
Five. create a new object video_config When Used to set the still image resolution (main) to 1920 x 1080. When A “low resolution” image of size 640 x 480. This low-res image is used as a preview image when framing the shot.
video_config = picam2.create_video_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
6. Load the configuration.
picam2.configure(video_config)
7. Sets the bitrate for the H264 encoder.
encoder = H264Encoder(bitrate=10000000)
8. Set the output file to test.h264. This will create a file containing the video.
output = "test.h264"
9. Starts a preview window, uses your encoder settings to start recording, and saves the video to an output file.
picam2.start_preview(Preview.QTGL)
picam2.start_recording(encoder, output)
Ten. Record a 10 second video using sleep. The previous recording command is not a block line of code. Use the sleep command to prevent recording from stopping even momentarily.
time.sleep(10)
11. Stop camera recording and close the preview window.
picam2.stop_recording()
picam2.stop_preview()
12. Save the code as video-test.py When [実行]Click to get started. A preview window will appear and it will take 10 seconds to record the video.
13. Watch video.You can find test.h264 in your file manager and go there. Double click the video file to play it in VLC.
complete code listing
from picamera2.encoders import H264Encoder
from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
video_config = picam2.create_video_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
picam2.configure(video_config)
encoder = H264Encoder(bitrate=10000000)
output = "test.h264"
picam2.start_preview(Preview.QTGL)
picam2.start_recording(encoder, output)
time.sleep(10)
picam2.stop_recording()
picam2.stop_preview()
Take Photo with Raspberry Pi Using Trigger
Camera Trigger is a classic Raspberry Pi project. Used to capture images and videos of animals or intruders, or to prank reluctant family members. A trigger is a sensor such as a passive infrared (PIR) motion sensor, an ultrasonic sensor, or in this case a simple push button.
In this project, we will create a camera trap that operates with a simple trigger. Press the button, use the preview window to frame your shot, and the file will automatically save to your Pi using the current date and time as the filename.
Wiring this project is easy. The button is connected to GPIO17 and GND via the breadboard and two female-male wires.
1. open tony When Create a new file. You can find Thonny in the main menu.
2. Import the Picamera2 module with the preview class. Next Import the time module.
from picamera2 import Picamera2, Preview
import time
3. Import the datetime, GPIO Zero, and Signal modules. Datetime is used to generate timestamps for image filenames. GPIO zero is used for simple button interfaces. Signals are used to stop Python code from terminating.
from datetime import datetime
from gpiozero import Button
from signal import pause
Four. Create an object picam2. This references the Picamera2 module and is used to control the camera.
picam2 = Picamera2()
Five. object, button, When Use an object to store the GPIO pin that the button is connected to.
button = Button(17)
6. create a new object camera_config When Use this to set the still image resolution (main) to 1920 x 1080 and a “low resolution” image of size 640 x 480. This low-res image is used as a preview image when framing the shot.
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
7. Load the configuration.
picam2.configure(camera_config)
8. Create a function capture() to save a series of commands. Executed when the trigger is pressed. Code within a function is automatically indented to show that it belongs to the function.
def capture():
9. Start the preview window. This allows the image to be assembled.
picam2.start_preview(Preview.QTGL)
Ten. object, timestamp, When Used to store the date and time of the trigger event.
timestamp = datetime.now().isoformat()
11. Start the camera and pause for 2 seconds to give yourself time to frame the image.
picam2.start()
time.sleep(2)
12. Set the capture file (and ultimately the image file) to use the current timestamp as the file name.
picam2.capture_file('/home/pi/%s.jpg' % timestamp)
13. Finally in the function stop the preview, When Stop the camera.
picam2.stop_preview()
picam2.stop()
14. Use GPIO Zero’s button class to call the “capture” function to react to button presses. finally Use pause() to prevent the code from exiting.
button.when_pressed = capture
pause()
15. Save the code as trigger-test.py. When [実行]Click to start the code.
16. I press the button Launch the camera and take an image.
17. Open system file manager When Please double-click the image to view.
complete code listing
from picamera2 import Picamera2, Preview
import time
from datetime import datetime
from gpiozero import Button
from signal import pause
picam2 = Picamera2()
button = Button(17)
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
picam2.configure(camera_config)
def capture():
picam2.start_preview(Preview.QTGL)
timestamp = datetime.now().isoformat()
picam2.start()
time.sleep(2)
picam2.capture_file('/home/pi/%s.jpg' % timestamp)
picam2.stop_preview()
picam2.stop()
button.when_pressed = capture
pause()