Skip to content

Instantly share code, notes, and snippets.

@pdeutsch
Created February 27, 2019 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdeutsch/742432462d1e909a7184f54746b7dbe1 to your computer and use it in GitHub Desktop.
Save pdeutsch/742432462d1e909a7184f54746b7dbe1 to your computer and use it in GitHub Desktop.

I installed pyv4l2 on the Rasperry Pi. https://pypi.org/project/pyv4l2/

It failed at first, but I did a pip3 install Cython first and then pip3 install pyv4l2 installed correctly.

There are four methods available on the Control object, as shown below. I also executed the get_controls() method and you can see that this dumps out a data structure containing all the possible settings, their names, values, default values, control ID, and other configuration parameters.

>>> from pyv4l2.control import Control
>>>
>>> control = Control('/dev/video0')
>>> control.

    control.get_controls(
    control.get_control_value(
    control.set_control_value(
    control.close(

>>> control.get_controls()
  [
    { 'default': -8193, 'name': b'Brightness',                 
      'type': 'int', 'value': 1,
      'disabled': False, 'max': 15, 'step': 1,
      'min': 0, 'id': 9963776 },
    { 'default': 57343, 'name': b'Contrast',                   
      'type': 'int', 'value': 8,
      'disabled': False, 'max': 15, 'step': 1,
      'min': 0, 'id': 9963777},
    { 'default': 57343, 'name': b'Saturation',                 
      'type': 'int', 'value': 7,
      'disabled': False, 'max': 15, 'step': 1,
      'min': 0, 'id': 9963778},
    { 'default': -8193, 'name': b'Hue',                        
      'type': 'int', 'value': 0,
      'disabled': False, 'max': 10, 'step': 1,
      'min': -10, 'id': 9963779},
    { 'default': 1, 'name': b'White Balance Temperature, Auto',
      'type': 'bool', 'value': 1,
      'disabled': False, 'max': 1, 'step': 1,
      'min': 0, 'id': 9963788},
    { 'default': 57343, 'name': b'Gamma',                      
      'type': 'int', 'value': 7,
      'disabled': False, 'max': 10, 'step': 1,
      'min': 1, 'id': 9963792},
    { 'default': 20478, 'name': b'Gain',                       
      'type': 'int', 'value': 0,
      'disabled': False, 'max': 0, 'step': 0,
      'min': 0, 'id': 9963795},
    { 'default': 2, 'name': b'Power Line Frequency',           
      'type': 'menu',
      'menu': {b'50 Hz': 1, b'Disabled': 0, b'60 Hz': 2},
      'value': 2,
      'disabled': False, 'max': 2, 'step': 1,
      'min': 0, 'id': 9963800},
    { 'default': 57343, 'name': b'White Balance Temperature',  
      'type': 'int', 'value': 2800,
      'disabled': False, 'max': 6500, 'step': 1,
      'min': 2800, 'id': 9963802},
    { 'default': 57343, 'name': b'Sharpness',                  
      'type': 'int', 'value': 6,
      'disabled': False, 'max': 15, 'step': 1,
      'min': 0, 'id': 9963803},
    { 'default': 57343, 'name': b'Backlight Compensation',     
      'type': 'int', 'value': 0,
      'disabled': False, 'max': 1, 'step': 1,
      'min': 0, 'id': 9963804},
    { 'default': 0, 'name': b'Exposure, Auto',                 
      'type': 'menu',
      'menu': {b'Manual Mode': 1, b'Aperture Priority Mode': 3},
      'value': 1,
      'disabled': False, 'max': 3, 'step': 1,
      'min': 0, 'id': 10094849},
    { 'default': 625,   'name': b'Exposure (Absolute)',          
      'type': 'int', 'value': 4,
      'disabled': False, 'max': 5000, 'step': 1,
      'min': 4, 'id': 10094850},
    { 'default': 57343, 'name': b'Focus (absolute)',           
      'type': 'int', 'value': 16,
      'disabled': False, 'max': 21, 'step': 1,
      'min': 0, 'id': 10094858},
    { 'default': 1, 'name': b'Focus, Auto',                    
      'type': 'bool', 'value': 1,
      'disabled': False, 'max': 1, 'step': 1,
      'min': 0, 'id': 10094860}
  ]

So if you want to set the camera up to all default values first, and then configure the brightness and exposure you could do the following:

# get all default settings
settings = control.get_controls()

# set all to defaults
for setting in settings:
    print("Setting {} to {}".format(setting['name'], setting['default']))
    control.set_control_value(setting['id'], setting['default'])

# set desired values
control.set_control(9963776,  1)     # brightness
control.set_control(10094849, 1)     # exposure_auto
control.set_control(10094850, 4)     # exposure_absolute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment