How to Take a Photo Using Raspberry Pi Camera Module
To take a photo using Raspberry Pi, connect the official camera module and enable it in
raspi-config. Then use the libcamera-still command or Python code with picamera2 library to capture and save an image.Syntax
Use the libcamera-still command to capture a photo from the Raspberry Pi camera module.
libcamera-still -o filename.jpg: Takes a photo and saves it asfilename.jpg.-t 0: Optional flag to keep the preview window open indefinitely.-n: Optional flag to disable the preview window.
Alternatively, use Python with the picamera2 library to control the camera programmatically.
bash
libcamera-still -o image.jpg
Example
This example shows how to take a photo using Python and the picamera2 library on Raspberry Pi. It captures an image and saves it as photo.jpg.
python
from picamera2 import Picamera2 import time picam2 = Picamera2() picam2.start_preview() time.sleep(2) # Wait for camera to adjust picam2.capture_file("photo.jpg") picam2.stop_preview() print("Photo saved as photo.jpg")
Output
Photo saved as photo.jpg
Common Pitfalls
Common mistakes when taking photos with Raspberry Pi camera include:
- Not enabling the camera interface in
raspi-config. - Using old
raspistillcommands on newer Raspberry Pi OS versions wherelibcamerais required. - Not waiting for the camera to warm up before capturing the image, causing dark or blurry photos.
- Incorrect camera connection or loose ribbon cable.
Always check camera connection and enable it before running commands or code.
bash
## Wrong way (old command, may not work):
# raspistill -o image.jpg
## Right way (new command):
# libcamera-still -o image.jpgQuick Reference
| Command/Code | Description |
|---|---|
| libcamera-still -o image.jpg | Capture photo and save as image.jpg |
| picamera2.capture_file('photo.jpg') | Python method to capture and save photo |
| sudo raspi-config | Enable camera interface under Interface Options |
| time.sleep(2) | Wait for camera warm-up before capture |
Key Takeaways
Enable the camera interface in Raspberry Pi settings before use.
Use the modern libcamera commands or picamera2 Python library for capturing photos.
Wait a couple of seconds after starting the camera to get clear images.
Check physical camera connection if photos are not captured.
Avoid using deprecated raspistill commands on new Raspberry Pi OS versions.