-
Notifications
You must be signed in to change notification settings - Fork 5
/
binary_clock_wallpaper.py
82 lines (68 loc) · 2.41 KB
/
binary_clock_wallpaper.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
import argparse
import os
from PIL import Image, ImageDraw
from datetime import datetime
# Create argument parser
parser = argparse.ArgumentParser()
parser.add_argument(
"--accent-color",
type=str,
default="#FF2536",
help="the color of active bits in the clock",
)
parser.add_argument(
"--wire-color", type=str, default="#807675", help="the color of the wires"
)
parser.add_argument("--bg-color", type=str, default="#0f0f17", help="background color")
mutex_group = parser.add_mutually_exclusive_group(required=False)
mutex_group.add_argument(
"--use-12-hour", action="store_true", help="use 12 hour format"
)
mutex_group.add_argument(
"--no-use-12-hour", action="store_false", help="use 24 hour format (default)"
)
parser.set_defaults(use_12_hour=False)
# Parse the arguments
args = parser.parse_args()
accent_color = args.accent_color
wire_color = args.wire_color
bg_color = args.bg_color
use_12_hour = args.use_12_hour
# Create a canvas with bg_color and wires with wire_color
base_dir = os.path.dirname(__file__)
image_name = "base12.png" if use_12_hour else "base24.png"
base_image_path = os.path.join(base_dir, "img", image_name)
base_img = Image.open(base_image_path)
alpha = base_img.getchannel("A")
wires = Image.new("RGBA", base_img.size, color=wire_color)
wires.putalpha(alpha)
canvas = Image.new("RGBA", base_img.size, bg_color)
canvas.paste(wires, (0, 0), wires)
# Get the current time and parse it
time_format = "%I:%M:%p" if use_12_hour else "%H:%M"
time = datetime.now().strftime(time_format)
hour, minute, *rest = time.split(":")
am_pm = rest == ["PM"]
# Set the coordinates and size of the bits of the clock
hour_xs = [543, 733, 940, 1133, 1312]
if use_12_hour:
hour_xs.pop()
hour_y = 292
minute_xs = [586, 802, 1003, 1177, 1344, 1509]
minute_y = 627
size = 100
# Draw the image
def draw_unit(unit, xs, y, size, accent_color):
dots = bin(int(unit))[2:].zfill(len(xs))
print("{} -> {}".format(unit, dots))
for dot, x in zip(dots, xs):
color = accent_color if dot == "1" else wire_color
draw.rectangle((x, y, x + size, y + size), fill=color)
draw = ImageDraw.Draw(canvas)
draw_unit(hour, hour_xs, hour_y, size, accent_color)
draw_unit(minute, minute_xs, minute_y, size, accent_color)
if use_12_hour:
draw_unit(am_pm, [1560], 157, size, accent_color)
# save the result
result_image_path = os.path.join(base_dir, "img", "result.png")
canvas.save(result_image_path, quality=95)