Testing Multiple Pi Camera Options With Python

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.

Pi Camera Options Test ResultsTrying 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.

Python Script

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 :

wget http://www.raspberrypi-spy.co.uk/archive/python/pi_camera_options.py

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.

Results

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 :

Pi Camera Options Test Results

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).

Camera Options Test - Cloud Camera Options Test - Horizon

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.

Final Thoughts

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.

This entry was posted in Camera Module, Python and tagged , , , . Bookmark the permalink.

10 Responses to Testing Multiple Pi Camera Options With Python

  1. Darren says:

    Can see this being incredibly useful, thanks!

  2. arun says:

    How to do this using c language?Please post me..

  3. arun says:

    Hi Giskard,

    i want code in c language because it can be used in my camera application..please post me in c language…

  4. Shyam says:

    Amazing and useful!
    I am glad google brought me to your website. This is one of the most informative website on ‘practical’ implementation – no beat around the bush. Keep updating ;)

    @Arun: Don’t be lazy learn python. I am versed in C,C++, C#, Python is next in my list and already learnt much in just 2 days time.
    If you still want, google “python to c# converter”..

    Regards..

  5. Darren says:

    Hi this is a great blog! I have just purchased the PI and keen to learn Python! What is the best way for a novice to learn?

  6. Ben says:

    Hi, Thanks a lot for your Python script. I have been running it with the Pi Cam against the sun going down to view what setting fits best and tonight I’ll have it running with the Pi cam against the moon ;-)

    One small thing I found a little confusing: unremarking the “other” awb and ex settings still left me with only 6 images. Might be clear to other people but if you unremark settings you have to remark the other settings because the script uses the last values for awb and ex which it reads from the script.

    Now, I have 130 images ;-)

    Thanks a lot !!

  7. Ben says:

    One other suggestion: I am a big fan of the tool feh which I run on both Ubuntu and the Pi. After installing the latest version you can sort the images in a directory by making date. So no more worries about filenames and wrong sorting.

    I prefer to use it with this command line

    feh -d -F -Smtime

    this way it displays the filename in the topleft corner, show the images fullscreen and sorted by creation time / date. Browsing through all the images goes with arrow right (and left)

    Regards,
    Ben

  8. Rick says:

    Thanks for the great script! I made a few changes based on the newest RaspiStill binary.
    https://github.com/ntalekt/RaspiStill_Camera_Options

  9. Kevin H says:

    Thanks for the script. It gives a good overlook of the settings and options. One thing you could add is making the photo contact sheet with the Python Imaging Library instead of XnView. This way, the script will be all in one. Maybe even add a text overlay to each image with the settings. Just a thought, but thanks for the post.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>