Dynamic Range

 

Often we receive pictures that are "dark" because they were underexposed, or "washed out" because they were overexposed, and sometimes we create them as a result of our processing. For example:

One way to enhance images like these is to increase their dynamic range. An image's dynamic range is the range of pixel values it uses. Greyscale PIL images assign each pixel a value from 0 to 255 (inclusive). The problem with the images above is that they don't take full advantage of this range. The penguin image has an upper limit far less than 255, and the moon image doesn't have any true blacks because its pixel values don't go as low as 0.

The mathematical problem of dynamic range is to take an image with pixel values in the range [a,b] and reassign them so they use the range [0,255]. We can do that by subtracting a from each pixel value to shift the range of pixel values so it starts at 0, and then multiplying each shifted pixel value by the ratio of the maximum shifted pixel value in the image to 255. This will make the new maximum pixel value in the image be 255 (while leaving the 0 values at 0 because that's how multiplication works).

What could go wrong?