Adjust screen brightness/contrast in Python? [closed]

旧巷老猫 提交于 2019-12-01 14:14:12

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.

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

dridk

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!