-
Notifications
You must be signed in to change notification settings - Fork 1
/
brightness.py
33 lines (30 loc) · 998 Bytes
/
brightness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pyscreeze
from PIL import ImageStat
import tempfile, os
from runcmd import run_cmd
class BrightnessController():
@staticmethod
def get():
raise NotImplementedError
@staticmethod
def set(new_level, time):
"""
:param new_level: float between 0 and 100
:param time: int in milliseconds
"""
raise NotImplementedError
class Xbacklight(BrightnessController):
@staticmethod
def get():
return float(run_cmd("xbacklight -get"))
@staticmethod
def set(new_level, time):
run_cmd(f"xbacklight -set {new_level} -time {int(time)}")
class ScreenBrightnessGetter():
def __init__(self):
self._tempdir = tempfile.TemporaryDirectory()
self._tempfile = os.path.join(self._tempdir.name, "screenshot.png")
def get(self):
if os.path.exists(self._tempfile):
os.remove(self._tempfile)
return ImageStat.Stat(pyscreeze.screenshot(self._tempfile).convert('L')).rms[0]