This gist contains a very minimal Python script that shows how to set the analogue and digital gains of the Raspberry Pi camera module from within Python. It is a trivial thing, all the actual work was done by the developers of userland in particular 6by9, and of course the developers of picamera. It does require the latest version of the userland libraries in order to be able to send the gain-setting commands. These are not yet (as of January 2018) in the main Raspbian distribution, but it is not scary to build this yourself:
sudo apt-get install cmake
git clone https://github.com/raspberrypi/userland.git
cd userland
./buildme
sudo shutdown -r now
After you've got the latest version of the userland libraries, you should be able to run
python set_gain.py
This will open a preview window and then freeze the analogue and digital gains at 1. Look at the code to see how it's done - it is not complicated. To use this in your own code, just do:
import picamera
from set_picamera_gain import set_analog_gain, set_digital_gain
with picamera.PiCamera() as cam:
set_analog_gain(cam, 2)
set_digital_gain(cam, 1)
Hopefully I will get round to making a pull request to picamera - but of course that's only really useful once the userland code is updated in the distribution.
@dogadogan, the easiest way to get this working is to use my fork of the picamera library, I've uploaded a wheel here:
https://github.com/rwb27/picamera/releases/tag/JOH_article
You can download the
.whl
file, and simply install that using pip. It replaces the standard picamera library, and makesanalog_gain
anddigital_gain
writeable, so the script above is no longer needed.The master branch of my repository is closer to the latest version of the picamera library, but it is not yet extensively tested or packaged up. You can install it with
All the best,
Richard