Skip to content
This repository has been archived by the owner on Aug 7, 2022. It is now read-only.
Matha Goram edited this page Jul 26, 2018 · 8 revisions

Welcome to the picamera wiki!

picamera

PiCamera Basic and Advanced Recipes adapted from "official" documentation for training purposes. The purpose of this document to record my experience in following the documentation prepared by Dave Jones.

Basic Recipes

  1. Capturing to a file

This example is a basic use of the capture method in the PiCamera classs. The capture method has the following parameters:

  • output: the value can a string (i.e. filename) or an object that has a write method; if this parameter is omitted then the image data is written to a buffer where overflow constraints are the responsibility of the client code.

  • format: the value can a string or a MIME-type; if this parameter is omitted then the format is deduced from the filetype extension otherwise an exception, PiCameraValueError is raised. The supported formats are JPEG, PNG, GIF, BMP, YUV, RGB, RGBA, BGR, BGRA and RAW.

  • use_video_port: the value is boolean and defaults to False whereby the higher quality image port is used.

  • resize: a two element tuple specifying the width and height of the image if the default value None needs to be overriden

  • splitter_port: see PiCamera documentation if you really need to use this parameter

  • bayer: if the value is True then the Exif metadata receives the raw bayer data from the camera's sensor

  • options: if the format is "jpeg" then the addtional parameters are:

  • quality: an integer from 1 to 100 representing the quality of the image; the default value is 85.

  • restart: the restart intervval for the encoder.

  • thumbnail: the size and quality of the thumbnail image as a tuple (width, height, quality) with the default being (64,48,35); if the value is None then no thumbnail is prepared.

  1. Capturing to a stream

Binary I/O

The binary input/ouput method, BytesIO(), instantiates a buffer memory to retain bytes without encoding, decoding or translation. This representation is useful for non-textual data. The optional parameter to the constructor method can be a buffer stream.

Start preview

This method displays the preview overlay with a rendering instance based the parameter options. The complementary method, `stop_preview(), stops the rendering. This pair of methods can be used throughout the lifetime of the corresponding active PiCamera object. All camera properties are accessible during the preview mode of operation.

Capture

The capture format must be specified as a string for a MIME-type or one of the following standards:

  1. Capturing to a PIL Image

The seek method has the following parameters:

  • offset: given byte offset
  • whence: int, optional, =0 for start (default), =1 for current, =2 for end

The Python Imaging Library (PIL) object has an open method that processes the image header but operates on the image object using a lazy operation that requires an explicit setting of the seek method.

  1. Capturing resized images

Many image processing applications may perform more efficiently at a resolution that is different than the camera setting. The resize parameter in the capture method provides a simple way to accomplish this by leveraging the camera's hardware than the more compute intensive methods of the image processing libraries.

The resize data is passed as a tuple - (width, height). If resize is specified, or use_video_port is True, then Exif metadata cannot be included in JPEG output owing to a current firmware limitation.

  1. Capturing consistent images

There may occur a need to have some consistency for all images across a specific series of shots. This consistency is reflected in brightness, contrast and color of the images. The key parameters towards capturing consistent images are:

  • shutter speed: exposure time set through shutter_speed
  • shooting speed: ISO
  • exposure gain: set exposure_mode to off and use pragmatic values for either analog_gain or digital_gain
  • white balance: set awb_mode to off and tweak values for awb_gains

This process may require iteration since the hardware characteristics of the camera may differ.

ISO

The ISO value for PiCamera v2 ranges from 100 to 1600. Values greater than 800 can be estimated internally when exposure_mode is set to sports and iso is set to 0.

shutter speed

The shutter speed is specified in microseconds. If the value is zero then the camera auto exposure process calculates the working shutter speed value.

exposure mode

The exposure mode settings are:

  • off
  • auto
  • night
  • nightpreview
  • backlight
  • spotlight
  • sports
  • snow
  • beach
  • verylong
  • fixedfps
  • antishak
  • fireworks

auto white balance mode

The auto white balance mode settings are:

  • off
  • auto
  • sunlight
  • cloudy
  • shade
  • tungsten
  • fluorescent
  • incandescent
  • flash
  • horizon

The command line options for CaptureConsistentImages are (with default values in parentheses):

  • -d, --delay (2)
  • -i, --iso (100)
  • -h, --help
  • -h1, --horz (1024)
  • -o, --ofile ("../images/CaptureConsistentImages.png")
  • -s, --shots (10)
  • -v, --vert (768)
  1. Capturing time-lapse sequences

  2. Capturing in low light

  3. Capturing to a network stream

  4. Recording video to a file

  5. Recording video to a stream

  6. Recording over multiple files

  7. Recording to a circular stream

  8. Recording to a network stream

  9. Overlaying images on the preview

  10. Overlaying text on the output

  11. Controlling the LED

Advanced Recipes

  1. Capturing to a numpy array

  2. Capturing to an OpenCV object

  3. Unencoded image capture in YUV format

  4. Unencoded image capture in RGB format

  5. Custom outputs

  6. Unconventional file outputs

  7. Rapid capture and processing

  8. Unencoded video capture

  9. Rapid capture and streaming

  10. Web streaming

  11. Capturing images whilst recording

  12. Recording at multiple resolutions

  13. Recording motion vector data

  14. Splitting to/from a circular stream

  15. Custom encoders

  16. Raw Bayer data captures

  17. Using a flash with the camera

Conclusions

References

IoTlogo