title: How can I train a python function to return the result I want?
body: <p>The problem I'm looking at is that I want to detect with a fairly reasonable level of certainty whether an image is black or mostly black. I already have the code written to get the color histogram, and the next step is to write a function that will take the <code>(r,g,b)</code> tuple and give me a <code>bool</code> indicating whether it's black or close to it. It's OK for this to not be 100% accurate, but it would be better to err toward false positives.</p>
  
  <pre><code>def average_image_color(i):
      h = i.histogram()
  
      # split into red, green, blue
      r = h[0:256]
      g = h[256:256*2]
      b = h[256*2: 256*3]
  
      # perform the weighted average of each channel:
      # the *index* is the channel value, and the *value* is its weight
      return (
          sum( i*w for i, w in enumerate(r) ) / sum(r),
          sum( i*w for i, w in enumerate(g) ) / sum(g),
          sum( i*w for i, w in enumerate(b) ) / sum(b))
  </code></pre>
  
  <p>I have a set of test images that I can use as a corpus. What's the best library/approach to this?</p>
  
  <p>The function I'd be hoping to train would be something like</p>
  
  <pre><code>def is_black(r, g, b):
      if magic_says_black():
          return True
      return False
  </code></pre>