title: Normalize histogram (brightness and contrast) of a set of images using Python Image Library (PIL)
body: <p>I have a script which uses Google Maps API to download a sequence of equal-sized square satellite images and generates a PDF. The images need to be rotated beforehand, and I already do so using PIL.</p>
  
  <p>I noticed that, due to different light and terrain conditions, some images are too bright, others are too dark, and the resulting pdf ends up a bit ugly, with less-than-ideal reading conditions "in the field" (which is backcountry mountain biking, where I want to have a printed thumbnail of specific crossroads).</p>
  
  <p>(EDIT) The goal then is to make all images end up with similar apparent brightness and contrast. So, the images that are too bright would have to be darkened, and the dark ones would have to be lightened. (by the way, I once used imagemagick <code>autocontrast</code>, or <code>auto-gamma</code>, or <code>equalize</code>, or <code>autolevel</code>, or something like that, with interesting results in medical images, but don't know how to do any of these in PIL).</p>
  
  <p>I already used some image corrections after converting to grayscale (had a grayscale printer a time ago), but the results weren't good, either. Here is my grayscale code:</p>
  
  <pre><code>#!/usr/bin/python
  
  def myEqualize(im)
      im=im.convert('L')
      contr = ImageEnhance.Contrast(im)
      im = contr.enhance(0.3)
      bright = ImageEnhance.Brightness(im)
      im = bright.enhance(2)
      #im.show()
      return im
  </code></pre>
  
  <p>This code works independently for each image. I wonder if it would be better to analyze all images first and then "normalize" their visual properties (contrast, brightness, gamma, etc).</p>
  
  <p>Also, I think it would be necessary to perform some analysis in the image (histogram?), so as to apply a custom correction depending on each image, and not an equal correction for all of them (although any "enhance" function implicitly considers initial contitions).</p>
  
  <p>Does anybody had such problem and/or know a good alternative to do this with the colored images (no grayscale)?</p>
  
  <p>Any help will be appreciated, thanks for reading!</p>