The libcamera-based Python interface to Raspberry Pi cameras, based on the original Picamera library
—
Essential camera functionality that forms the foundation of all picamera2 applications. These operations handle camera initialization, lifecycle management, and basic control functions.
Create and initialize a Picamera2 instance with optional camera selection and tuning parameters.
def __init__(self, camera_num: int = 0, tuning: Tuning = None):
"""
Initialize Picamera2 instance.
Parameters:
- camera_num: int, camera index (default 0 for first camera)
- tuning: Tuning object or file path for camera tuning parameters
Raises:
- RuntimeError: If camera cannot be acquired or libcamera fails to initialize
"""Start, stop, and close camera operations with proper resource management.
def start(self, config: CameraConfiguration = None, show_preview: bool = None):
"""
Start the camera system.
Parameters:
- config: CameraConfiguration, optional configuration to apply before starting
- show_preview: bool, whether to automatically start preview (if available)
Raises:
- RuntimeError: If camera fails to start or configuration is invalid
"""
def stop(self):
"""
Stop the camera system and all active operations.
Stops preview, encoders, and request processing. Camera remains configured
and can be restarted with start().
"""
def close(self):
"""
Close camera and release all resources.
Stops camera if running, releases libcamera resources, and closes all
associated objects. Camera cannot be used after closing.
"""Apply camera configurations to set up streams and camera parameters.
def configure(self, camera_config: CameraConfiguration):
"""
Configure camera with specified configuration.
Parameters:
- camera_config: CameraConfiguration object defining camera setup
Raises:
- RuntimeError: If configuration is invalid or cannot be applied
"""Access camera properties and capabilities.
@property
def camera_properties(self) -> dict:
"""
Dictionary of camera properties including model, sensor capabilities, etc.
Returns:
dict: Camera properties from libcamera
"""
@property
def camera_controls(self) -> dict:
"""
Dictionary of available camera controls and their ranges.
Returns:
dict: Available controls with min/max/default values
"""
@staticmethod
def global_camera_info() -> list[GlobalCameraInfo]:
"""
Get information about all available cameras.
Returns:
list: List of GlobalCameraInfo dictionaries with camera details
"""Retrieve current camera and stream configurations.
def camera_configuration(self) -> CameraConfiguration:
"""
Get current camera configuration.
Returns:
CameraConfiguration: Current active configuration
"""
def stream_configuration(self, name: str) -> StreamConfiguration:
"""
Get configuration for specific stream.
Parameters:
- name: str, stream name ("main", "lores", "raw")
Returns:
StreamConfiguration: Configuration for specified stream
Raises:
- KeyError: If stream name is not found
"""from picamera2 import Picamera2
# Initialize camera
picam2 = Picamera2()
# Check camera properties
print(f"Camera model: {picam2.camera_properties.get('Model', 'Unknown')}")
print(f"Available controls: {list(picam2.camera_controls.keys())}")
# Create and apply configuration
config = picam2.create_still_configuration()
picam2.configure(config)
# Start camera
picam2.start()
# Camera is now ready for capture operations
# ... perform captures ...
# Clean shutdown
picam2.stop()
picam2.close()from picamera2 import Picamera2
# Check available cameras
cameras = Picamera2.global_camera_info()
print(f"Found {len(cameras)} cameras")
for i, cam_info in enumerate(cameras):
print(f"Camera {i}: {cam_info['Model']}")
# Initialize specific camera
if len(cameras) > 1:
picam2 = Picamera2(camera_num=1)
else:
picam2 = Picamera2()from picamera2 import Picamera2
picam2 = Picamera2()
# Create different configurations
preview_config = picam2.create_preview_configuration()
still_config = picam2.create_still_configuration()
# Start with preview
picam2.configure(preview_config)
picam2.start()
# Later switch to still capture
picam2.stop()
picam2.configure(still_config)
picam2.start()
# Camera ready for high-quality still captureInstall with Tessl CLI
npx tessl i tessl/pypi-picamera2