#!/usr/bin/env python # add keyboard brightness import os import time from datetime import datetime from PIL import Image import numpy as np SCREEN_BRIGHTNESS_FILE = "/home/pascal/screen_brightness.png" HIGH_SCREEN_BRIGHTNESS_CUTOFF = 70 MAX_AVG_RGB = 255 def write(n): with open("/sys/class/backlight/gmux_backlight/brightness", "w") as f: #print("new", n) f.write(str(int(n))) def get_screen_brightness(): im = Image.open(SCREEN_BRIGHTNESS_FILE) im_arr = np.fromstring(im.tobytes(), dtype=np.uint8) im_arr = im_arr.reshape((im.size[1], im.size[0], 3)) crop = im_arr[-100:, -100:, :] return crop.mean() with open("/sys/class/backlight/gmux_backlight/max_brightness") as f: max_v = int(f.read()) print("running brightness, did you run with root?") prev_check = time.time() while True: os.system("/home/pascal/screensy " + SCREEN_BRIGHTNESS_FILE) with open("/sys/devices/platform/applesmc.768/light") as f: backlight = int(f.read()[1:-1].split(",")[0]) sb = get_screen_brightness() print(sb, backlight) new_value = max_v # if v < 10: # # do not do comparison constantly # hour = datetime.now().hour # if hour > 20 or hour < 8: # new_value = 100 # else: # new_value = 300 # if new_value == max_v and sb > HIGH_SCREEN_BRIGHTNESS_CUTOFF: new_value = (1 - (sb / MAX_AVG_RGB)) * 0.8 * new_value + (0.2) * new_value new_value = new_value if backlight > 10 else 0.3 * new_value print(new_value) write(new_value) time.sleep(0.5)