title: How to loop over histogram to get the color of the picture?
body: <p><a href="https://stackoverflow.com/questions/2270874/image-color-detection-using-python/2270895#2270895">In this answer</a> about detecting the color of an image <code>olooney</code> said that "loop over the histogram and take the average of pixel color weighed by the pixel count".</p>
  
  <p>I ran the histogram like this:</p>
  
  <pre><code>class ImageResize(webapp.RequestHandler):
      def get(self):
          q = HomePage.all()
          q.filter("firm_name", "noise")
          qTable = q.get()
          id = qTable.key().id()
          if id:
              homepage = HomePage.get_by_id(id)
              if homepage:
                  img = images.Image(homepage.thumbnail)
                  hist = img.histogram()
  </code></pre>
  
  <p>then in IDLE, for each color of the histogram <code>hist2</code>, I tried to get the average and divided by pixel count, but I get the same number. What am I doing wrong?</p>
  
  <pre><code>&gt;&gt;&gt; average_red = float(sum(hist2[0]))/len(hist2[0])
  &gt;&gt;&gt; average_red
  789.2578125
  &gt;&gt;&gt; average_green = float(sum(hist2[1]))/len(hist2[1])
  &gt;&gt;&gt; average_green
  789.2578125
  &gt;&gt;&gt; average_blue = float(sum(hist2[2]))/len(hist2[2])
  &gt;&gt;&gt; average_blue
  789.2578125
  &gt;&gt;&gt;
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to Saxon Druce for <a href="https://stackoverflow.com/questions/7563315/how-to-loop-over-histogram-to-get-the-color-of-the-picture/7564929#7564929">the answer</a>. Here's the code I used:</p>
  
  <pre><code>&gt;&gt;&gt; def hist_weighed_average(hist):
      red_hist = hist[0]
      green_hist = hist[1]
      blue_hist = hist[2]
  
      red_weighed_sum = float(sum(i * red_hist[i] for i in range(len(red_hist))))
      green_weighed_sum = float(sum(i * green_hist[i] for i in range(len(green_hist))))
      blue_weighed_sum = float(sum(i * blue_hist[i] for i in range(len(blue_hist))))
  
      red_num_pixels = float(sum(red_hist))
      green_num_pixels = float(sum(green_hist))
      blue_num_pixels = float(sum(blue_hist))
  
      red_weighed_average = red_weighed_sum / num_pixels
      green_weighed_average = green_weighed_sum / num_pixels
      blue_weighed_average = blue_weighed_sum / num_pixels
      return red_weighed_average, green_weighed_average, blue_weighed_average
  &gt;&gt;&gt; hist = hist3
  &gt;&gt;&gt; hist_weighed_average(hist)
  (4.4292897797574859, 4.8236723583271468, 5.2772779015095272)
  &gt;&gt;&gt; hist = hist2
  &gt;&gt;&gt; hist_weighed_average(hist)
  (213.11471417965851, 220.01047265528334, 214.12880475129919)
  &gt;&gt;&gt; 
  </code></pre>