title: Python: Normalize image exposure
body: <p>I'm working on a project to measure and visualize image similarity. The images in my dataset come from photographs of images in books, some of which have very high or low exposure rates. For example, the images below come from two different books; the one on the top is an over-exposed reprint of the one on the bottom, wherein the exposure looks good:</p>
  
  <p><a href="https://i.stack.imgur.com/E8lBj.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/E8lBj.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/y2Kk4.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/y2Kk4.jpg" alt="enter image description here"></a></p>
  
  <p>I'd like to normalize each image's exposure in Python. I thought I could do so with the following naive approach, which attempts to center each pixel value between 0 and 255:</p>
  
  <pre><code>from scipy.ndimage import imread
  import sys
  
  def normalize(img):
    '''
    Normalize the exposure of an image.
    @args:
      {numpy.ndarray} img: an array of image pixels with shape:
        (height, width)
    @returns:
      {numpy.ndarray} an image with shape of `img` wherein
        all values are normalized such that the min=0 and max=255
    '''
    _min = img.min()
    _max = img.max()
    return img - _min * 255 / (_max - _min)
  
  img = imread(sys.argv[1])
  normalized = normalize(img)
  </code></pre>
  
  <p>Only after running this did I realize that this normalization will only help images whose lightest value is less than 255 or whose darkest value is greater than 0.</p>
  
  <p>Is there a straightforward way to normalize the exposure of an image such as the top image above? I'd be grateful for any thoughts others can offer on this question.</p>