Bird
0
0
Raspberry Piprogramming~5 mins

Capturing still images in Raspberry Pi

Choose your learning style9 modes available
Introduction

Taking still pictures lets you save moments or objects as image files. This is useful for projects like photography, security, or documentation.

You want to take a photo with a Raspberry Pi camera module.
You need to capture an image for a security system snapshot.
You want to save a picture for a time-lapse project.
You want to capture an image to analyze it later in a program.
Syntax
Raspberry Pi
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.start_preview()
sleep(2)  # Wait for camera to adjust
camera.capture('/home/pi/image.jpg')
camera.stop_preview()

Use sleep to give the camera time to adjust before capturing.

The file path in capture() is where the image will be saved.

Examples
Capture and save an image named picture1.jpg in the home directory.
Raspberry Pi
camera.capture('/home/pi/picture1.jpg')
Save the image in a subfolder called photos with a custom name.
Raspberry Pi
camera.capture('/home/pi/photos/photo_2024.jpg')
You can save images in different formats like PNG by changing the file extension.
Raspberry Pi
camera.capture('/home/pi/image.png')
Sample Program

This program starts the camera preview, waits 3 seconds for the camera to adjust, captures a still image, saves it as test_image.jpg, then stops the preview and prints a confirmation message.

Raspberry Pi
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.start_preview()
sleep(3)  # Let camera adjust
camera.capture('/home/pi/test_image.jpg')
camera.stop_preview()
print('Image captured and saved as test_image.jpg')
OutputSuccess
Important Notes

Make sure the Raspberry Pi camera module is enabled in the Raspberry Pi settings.

Use absolute file paths to avoid confusion about where images are saved.

Camera preview helps you see what the camera sees before capturing.

Summary

Use the PiCamera library to control the Raspberry Pi camera.

Start the preview, wait a moment, then capture and save the image.

Save images with clear file paths and names for easy access.