This tutorial provides a comprehensive guide on how to take a photo using a Raspberry Pi Camera Module 3 with a modern Raspberry Pi running the latest Raspberry Pi OS. It covers the command-line and Python methods.
While this guide was requested for a “Compute Module 5,” that module is not yet released. The instructions here are for any modern Raspberry Pi (Pi 4, Pi 5, CM4) running a recent version of Raspberry Pi OS (Bullseye or newer) and will be applicable when the CM5 is released.
libcamera stack.You must enable the camera interface before the OS will recognize it.
sudo raspi-config
3 Interface Options and press Enter.I1 Legacy Camera and, when prompted, select <No> to disable the legacy stack. Then, back in the Interface Options menu, select I1 Camera and select <Yes> to enable the modern libcamera interface.<Finish> in the main menu. It will ask if you want to reboot. Select <Yes>.The modern tool for taking photos from the command line is rpicam-still.
This is the simplest command. It will show a 5-second preview on the connected display and then save the image.
rpicam-still -o test_image.jpg
-o: Specifies the output filename.Useful for scripts or SSH sessions where you don’t have a display.
rpicam-still -o no_preview.jpg --nopreview
--nopreview: Skips the live preview window.rpicam-still Optionsrpicam-still -o high_res.jpg --width 1920 --height 1080
rpicam-still -o delayed.jpg --timeout 3000
(This waits 3 seconds before capturing)
rpicam-still -o image.png -e png
-e: Specifies the encoding.# Take a picture every 10 seconds for 1 minute
rpicam-still --timelapse 10000 -t 60000 -o timelapse_%04d.jpg
--timelapse: Interval between shots in milliseconds.-t: Total timeout for the entire process in milliseconds.%04d: A special formatter that names files timelapse_0001.jpg, timelapse_0002.jpg, etc.For more control and integration into applications, use the picamera2 library. It should be pre-installed on recent Pi OS versions. If not, install it with sudo apt install python3-picamera2.
Create a file named take_photo.py:
# take_photo.py
from picamera2 import Picamera2
import time
# 1. Initialize the camera
print("Initializing camera...")
picam2 = Picamera2()
# 2. Create a configuration for a still image
# You can customize this, e.g., main={"size": (1920, 1080)}
config = picam2.create_still_configuration()
picam2.configure(config)
# 3. Start the camera system
print("Starting camera...")
picam2.start()
# 4. Give the camera time to adjust to light levels
# This is important to avoid dark or poorly exposed images.
time.sleep(2)
# 5. Capture the image and save it
output_filename = "python_photo.jpg"
print(f"Capturing image to {output_filename}...")
picam2.capture_file(output_filename)
# 6. Stop the camera
picam2.stop()
print("Done. Camera stopped.")
take_photo.py.python3 take_photo.py
python_photo.jpg, will be created in the same directory.raspi-config and rebooted.libcamera interface, not the legacy one.ModuleNotFoundError: No module named 'picamera2':
sudo apt update && sudo apt install python3-picamera2.