Adjust screen brightness/contrast in Python? [clos

2019-02-21 08:45发布

问题:

I need to adjust screen brightness/contrast by using Python. It will be a function in my code and it will be run when user presses a key, else script won't change the brigness/contrast. How can i make it in Python?

Thank you very much

回答1:

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.



回答2:

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



回答3:

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.