I am trying write a contrast adjustment for images in gray scale colors but couldn\'t find the right way to do it so far. This is what I came up with:
import nu
I'm learning Python and numpy
and thought I'd try to implement a "LookUp Table" (LUT). It works, and the output image has the full range from black to white, but I'm happy to receive suggestions for improvement.
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Open the input image as numpy array, convert to greyscale and drop alpha
npImage=np.array(Image.open("cartoon.png").convert("L"))
# Get brightness range - i.e. darkest and lightest pixels
min=np.min(npImage) # result=144
max=np.max(npImage) # result=216
# Make a LUT (Look-Up Table) to translate image values
LUT=np.zeros(256,dtype=np.uint8)
LUT[min:max+1]=np.linspace(start=0,stop=255,num=(max-min)+1,endpoint=True,dtype=np.uint8)
# Apply LUT and save resulting image
Image.fromarray(LUT[npImage]).save('result.png')
Keywords: Python, Numpy, PIL, Pillow, image, image processing, LUT, Look-Up Table, Lookup, contrast, stretch.
The easiest way to increase the contrast (i.e. pull apart darker and brighter pixels), is just to "stretch out" the current existing range (144 to 216) over the entire spectrum (0 to 255):
Setup, same way as in this answer.
import numpy as np
from PIL import Image
pixvals = np.array(Image.open("image.png").convert("L"))
And then expand the range
pixvals = ((pixvals - pixvals.min()) / (pixvals.max()-pixvals.min())) * 255
Image.fromarray(pixvals.astype(np.uint8))
The result is effectively the same as in this answer, just with slightly less code:
Now, in this image, that might be enough. However some images might have a few pixels that are really close to 0 or 255, which would render this method ineffective.
Here numpy.percentile() comes to the rescue. The idea is to "clip" the range in which pixels are allowed to exist.
minval = np.percentile(pixvals, 2)
maxval = np.percentile(pixvals, 98)
pixvals = np.clip(pixvals, minval, maxval)
pixvals = ((pixvals - minval) / (maxval - minval)) * 255
Image.fromarray(pixvals.astype(np.uint8))
Which results in a little bit higher contrast, since all values below 2% and above 98% are effectively removed. (Play with these values as you see fit)
You need to apply a mapping curve like this:
It makes the dark tones darker, the light tones lighter, and increases the range of the medium shades.
To achieve that, I'd find the minimum and maximum, then create a lookup table that expands the narrow remaining range into a whole range between 0 and 255. After that, I'd apply the lookup table.
This will certainly leave some blocking, because the ranges of nice gradients of the source were compressed in a lossy way. To fix it, you might consider applying a "smart blur" algorithm that blurs only pixels that have low contrast between them, and does not touch those with high contrast. (I don't see a nice link with a numpy-friendly algorithm, though).