How can I adjust the contrast and brightness of an image to match GIMP's Colours
> Brightness-Contrast
dialogue?
A brightness of -110 and a contrast of 127 is what I want achieve.
I found this function from this answer:
def change_contrast(img, level):
factor = (259 * (level + 255)) / (255 * (259 - level))
def contrast(c):
value = 128 + factor * (c - 128)
return max(0, min(255, value))
return img.point(contrast)
This function reproduces the contrast with the values I want, but I can not find an equivalent for brightness.
I know that the ImageEnhance
module exists, but that has an unbounded float as a parameter, how do I convert the values I use in gimp to this format? Or is there another solution that I can plug these values into. The ImageEnhance docs don't explain how this parameter works.
from PIL import Image, ImageEnhance
img = Image.open(path)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(0.2)
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(2.0)