Skip to content

Instantly share code, notes, and snippets.

@schappim
Created July 28, 2024 23:57
Show Gist options
  • Save schappim/7fdd686e3e4c96dff974bb7fda7bcb4f to your computer and use it in GitHub Desktop.
Save schappim/7fdd686e3e4c96dff974bb7fda7bcb4f to your computer and use it in GitHub Desktop.

To control the Raspberry Pi camera from Python, you can use the picamera library, which provides an easy-to-use interface for capturing images and videos. Here's a basic example to get you started:

  1. Install the picamera library:

    sudo apt update
    sudo apt install python3-picamera
  2. Capture an image:

    from picamera import PiCamera
    from time import sleep
    
    camera = PiCamera()
    
    camera.start_preview()
    sleep(2)  # Allow the camera to adjust to lighting conditions
    camera.capture('/home/pi/image.jpg')
    camera.stop_preview()
  3. Record a video:

    from picamera import PiCamera
    from time import sleep
    
    camera = PiCamera()
    
    camera.start_preview()
    camera.start_recording('/home/pi/video.h264')
    sleep(10)  # Record for 10 seconds
    camera.stop_recording()
    camera.stop_preview()

These examples demonstrate basic usage. The picamera library provides many additional features, such as adjusting camera settings, adding overlays, and more. You can refer to the official documentation for further details and advanced functionalities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment