I have a large collection of 7 mega pixel grayscale images and I want batch process them to adjust the contrast and brightness so that each image contains about:
50% highlights (pixels with a luminescence value of 200-255)
30% midtones (pixels with a luminescence value of 55-199)
20% shadows (pixels with a luminescence value of 0-54)
It needs to be reasonably efficient as I only have a 1.8GHz and many images. I understand that with NumPy you can get PIL/Pillow to process images much more efficiently than without, but I have never used it before.
A while ago I wrote some numpy code to solve this exact problem.
There are many possible ways to transform the histogram of the input image such that the correct number of pixel values fall within each bin. Perhaps the simplest is to find the difference between the current pixel values corresponding to each percentile and the desired value, then linearly interpolate across the bin edges to find out how much to add/subtract from each pixel value:
For example:
Plotting: