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.
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting https://github.com/rwb27/picamera/archive/JOH_article.tar.gz
Using cached https://github.com/rwb27/picamera/archive/JOH_article.tar.gz
Building wheels for collected packages: picamera
Building wheel for picamera (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-c078tdel/setup.py'"'"'; file='"'"'/tmp/pip-req-build-c078tdel/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-r03b2ol7
cwd: /tmp/pip-req-build-c078tdel/
Complete output (58 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/picamera
copying picamera/exc.py -> build/lib/picamera
copying picamera/init.py -> build/lib/picamera
copying picamera/frames.py -> build/lib/picamera
copying picamera/renderers.py -> build/lib/picamera
copying picamera/mmalobj.py -> build/lib/picamera
copying picamera/display.py -> build/lib/picamera
copying picamera/encoders.py -> build/lib/picamera
copying picamera/bcm_host.py -> build/lib/picamera
copying picamera/array.py -> build/lib/picamera
copying picamera/vcsmobj.py -> build/lib/picamera
copying picamera/mmal.py -> build/lib/picamera
copying picamera/streams.py -> build/lib/picamera
copying picamera/color.py -> build/lib/picamera
copying picamera/user_vcsm.py -> build/lib/picamera
copying picamera/camera.py -> build/lib/picamera
running egg_info
creating picamera.egg-info
writing picamera.egg-info/PKG-INFO
writing dependency_links to picamera.egg-info/dependency_links.txt
writing requirements to picamera.egg-info/requires.txt
writing top-level names to picamera.egg-info/top_level.txt
writing manifest file 'picamera.egg-info/SOURCES.txt'
reading manifest file 'picamera.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'debian'
no previously-included directories found matching 'docs'
writing manifest file 'picamera.egg-info/SOURCES.txt'
installing to build/bdist.linux-armv7l/wheel
running install
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-req-build-c078tdel/setup.py", line 145, in
main()
File "/tmp/pip-req-build-c078tdel/setup.py", line 140, in main
cmdclass = {'install': CustomInstallCommand},
File "/usr/local/lib/python3.7/dist-packages/setuptools/init.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/usr/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/local/lib/python3.7/dist-packages/wheel/bdist_wheel.py", line 335, in run
self.run_command('install')
File "/usr/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-req-build-c078tdel/setup.py", line 109, in run
raise ValueError('This system does not appear to be a Raspberry Pi')
ValueError: This system does not appear to be a Raspberry Pi
ERROR: Failed building wheel for picamera
Running setup.py clean for picamera
Failed to build picamera
Installing collected packages: picamera
Running setup.py install for picamera ... error
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-c078tdel/setup.py'"'"'; file='"'"'/tmp/pip-req-build-c078tdel/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-qmm6frhh/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/pi/.local/include/python3.7m/picamera
cwd: /tmp/pip-req-build-c078tdel/
Complete output (18 lines):
running install
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-req-build-c078tdel/setup.py", line 145, in
main()
File "/tmp/pip-req-build-c078tdel/setup.py", line 140, in main
cmdclass = {'install': CustomInstallCommand},
File "/usr/local/lib/python3.7/dist-packages/setuptools/init.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/usr/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-req-build-c078tdel/setup.py", line 109, in run
raise ValueError('This system does not appear to be a Raspberry Pi')
ValueError: This system does not appear to be a Raspberry Pi
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-c078tdel/setup.py'"'"'; file='"'"'/tmp/pip-req-build-c078tdel/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-qmm6frhh/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/pi/.local/include/python3.7m/picamera Check the logs for full command output.
Hello, I am getting error while installing pip install https://github.com/rwb27/picamera/archive/JOH_article.tar.gz. Please let me know if there is any other solution.
I download zip file and extracted it. and ran python camera.py. It ge=ives me error of "new_picamera.exc.picamerammalerror: failed to enable connection: out of resources". Please let me know how to proceed if anybody know any solution.
Thank you.