1

I want to adjust screen brightness and contrast using Python. Does anyone know a library that can do this? How can the script be triggered using a keyboard shortcut?

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
trante
  • 33,518
  • 47
  • 192
  • 272

3 Answers3

1

This is something that is OS-specific and probably not doable without system-specific bindings.

Amber
  • 507,862
  • 82
  • 626
  • 550
0

I found what looks like a Linux-specific recipe here.

For windows I think you need to find out what function you need to call in which dll (probably driver-specific) and use ctypes to make the required call.

Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
-4

I m using the equation define here.

So, to adjust contrast and brightness at the same time, do for each pixel:

new_value = (old_value - 0.5) × contrast + 0.5 + brightness 

Bellow a nice function which do the job :

def brightness_contrast(image, brightness = -100, contrast = 300):
    def vect(a):
        c   = contrast
        b   = 100 * brightness
        res = ((a - 127.5) * c + 127.5) + b
        if res < 0 :
            return 0
        if res > 255:
            return 255
        return res

    transform = np.vectorize(vect)
    data = transform(fromimage(image)).astype(np.uint8)
    return toimage(data)

You can use it like this:

img = Image.open("calibration/gland_89_0.jpg")
brightness_contrast(img, brightness=-20, contrast=200).show()

I think this function should be better, regarding paramaters. Actually, there is no limit, I should update the code to make arguments in percent.

arghtype
  • 4,376
  • 11
  • 45
  • 60
dridk
  • 180
  • 2
  • 13