It’s easy taking photos using the Pi camera module using the excellent raspistill command line utility provided by the Raspberry Pi Foundation. In it’s basic form the utility uses a set of default values. I wanted to experiment with the different settings available for EX (Exposure) and AWB (Auto White Balance) and see how they performed in different environments.
Trying different settings manually seemed like a lot of work so I decided to write a script that would take a sequence of photos using all available options. I could then quickly review the photos and see which ones looked best in a particular environment.
The script could be used whenever I needed to quickly check if tweaking the options was worth considering.
Controlling Exposure and Auto White Balance
An example command which uses the EX and AWB settings is shown below :
raspistill -o myphoto.jpg -ex night -awb shade
The plan was to set those ex and awb options using all of the available values which are shown below :
-ex off,auto,night,nightpreview,backlight,spotlight,sports snow,beach,verylong,fixedfps,antishake,fireworks -awb off,auto,sun,cloud,shade,tungsten,fluorescent, incandescent,flash,horizon
The total number of photos required to try out all of the combinations will be 130 (13 x 10). I didn’t want quite this number of photos so I trimmed my list to only include settings I was likely to be interested in.
Here is the Python script I use to test my Pi camera module. In this case it only takes 6 photos but you can uncomment the other definitions of “list_ex” and “list_awb” to increase the number of options used :
#!/usr/bin/env python import os import time import subprocess # Full list of Exposure and White Balance options #list_ex = ['off','auto','night','nightpreview','backlight', # 'spotlight','sports','snow','beach','verylong', # 'fixedfps','antishake','fireworks'] #list_awb = ['off','auto','sun','cloud','shade','tungsten', # 'fluorescent','incandescent','flash','horizon'] # Refined list of Exposure and White Balance options. 50 photos. #list_ex = ['off','auto','night','backlight','fireworks'] #list_awb = ['off','auto','sun','cloud','shade','tungsten', # 'fluorescent','incandescent','flash','horizon'] # Test list of Exposure and White Balance options. 6 photos. list_ex = ['off','auto'] list_awb = ['off','auto','sun'] # EV level photo_ev = 0 # Photo dimensions and rotation photo_width = 640 photo_height = 480 photo_rotate = 0 photo_interval = 0.25 # Interval between photos (seconds) photo_counter = 0 # Photo counter total_photos = len(list_ex) * len(list_awb) # Delete all previous image files try: os.remove("photo_*.jpg") except OSError: pass # Lets start taking photos! try: print "Starting photo sequence" for ex in list_ex: for awb in list_awb: photo_counter = photo_counter + 1 filename = 'photo_' + ex + '_' + awb + '.jpg' cmd = 'raspistill -o ' + filename + ' -t 1000 -ex ' + ex + ' -awb ' + awb + ' -ev ' + str(photo_ev) + ' -w ' + str(photo_width) + ' -h ' + str(photo_height) + ' -rot ' + str(photo_rotate) pid = subprocess.call(cmd, shell=True) print ' [' + str(photo_counter) + ' of ' + str(total_photos) + '] ' + filename time.sleep(photo_interval) print "Finished photo sequence" except KeyboardInterrupt: # User quit print "\nGoodbye!"
In the code above I’ve split some of lines for the benefit of this webpage. If you want to download this script directly to your Pi you can use the command :
It can then be run using :
sudo python pi_camera_options.py
The images will be saved in the same directory as the script so you may want to run it in a sub-directory to keep your test files together.
My tests were run by taking photos out my study window. I used the free image utility XnView to create a “Contact Sheet” with a set of the photos :
As you can see there is big difference between the images. In this case one of the better images is the Automatic setting (top left) but the clouds are slightly clearer where EX was “auto” and AWB was “horizon”. I suspect “horizon” is better for images where the top half of the scene is brighter than the lower half.
Funnily enough the “cloud” setting (below left) didn’t do as good a job on the clouds compared to the “horizon” setting (below right).
In this example the default settings were great. This script will prove more useful when you are dealing with a more challenging scene, perhaps with lower light levels.
I plan on using the same process to test the camera performance in my garage. This is where my security system project is based and has very specific lighting conditions. This script will allow me to chose the best command line options to ensure I get the best photos.
I can run sequences using different lighting solutions and see which work out the best. Better photos in this case will increase my chances of identifying potential burglars. Catching a burglar with a Raspberry Pi camera is on my list of things to do before I die.