-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
84 lines (61 loc) · 2.31 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Panel helper.
Manage the opacity of the Gnome-Shell panel by the current wallpaper's
color.
"""
from pydbus import SessionBus
import os
import subprocess
import time
PANEL = "Main.panel.actor"
LCORNER = "Main.panel._leftCorner.actor"
RCORNER = "Main.panel._rightCorner.actor"
VISIBLE = "black"
INVISIBLE = "rgba(0,0,0,0)"
def _set_panel_css(visible=True):
color = VISIBLE if visible else INVISIBLE
service = SessionBus().get("org.gnome.Shell")
service.Eval("{}.style='background-color: {};'".format(PANEL, color))
service.Eval(
"{}.style='-panel-corner-background-color: {};"
"-panel-corner-border-color: transparent;'".format(LCORNER, color))
service.Eval(
"{}.style='-panel-corner-background-color: {};"
"-panel-corner-border-color: transparent;'".format(RCORNER, color))
def _get_most_frequent_color(image):
color_list = image.getcolors(16777216) # 256 * 256 * 256 = 16.7m
color_item = color_list[0]
for occurance, colors in color_list:
if occurance > color_item[0]:
color_item = (occurance, colors)
return color_item[1]
def _is_bright_image(path):
"""Return the average color of the image."""
from PIL import Image
image = Image.open(path)
image = image.crop((0, 0, image.size[0], image.size[1] // 50))
print(image.size)
red, green, blue = _get_most_frequent_color(image)
return (0.2126 * red) + (0.7152 * green) + (0.0722 * blue) > 125
def _handle_image_path(path):
"""Verify the provied path is an image path."""
ext = os.path.splitext(path)[1]
if ext not in [".gif", ".jpeg", ".jpg", ".png"]:
return None
visible = _is_bright_image(path[7:])
_set_panel_css(visible)
def _monitor(cmd):
"""Monitor a long lived process.
:param cmd: Long lived command.
"""
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
line = p.stdout.readline()
if line == "" and p.poll() is not None:
break
event, _, path = line.decode("utf-8").partition(" ")
_handle_image_path(path.rstrip().replace("'", ""))
# Sleep the process to give the system a break.
time.sleep(5)
if __name__ == "__main__":
_monitor("gsettings monitor org.gnome.desktop.background picture-uri")