Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
58 lines (49 loc) · 1.6 KB
/
Copy pathutils.py
File metadata and controls
58 lines (49 loc) · 1.6 KB
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
"""
Shared utilities: asset paths, clock drawing.
"""
import os
import math
from PIL import Image, ImageDraw
# Resolve images directory relative to this file
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
IMAGES_DIR = os.path.join(BASE_DIR, "images")
def img_path(filename: str) -> str:
"""Return the absolute path for an image file."""
return os.path.join(IMAGES_DIR, filename)
def draw_clock(hour_angle: float, min_angle: float, sec_angle: float) -> str:
"""
Draw an analog clock face and save it to images/clock.png.
Returns the path to the saved file.
"""
clock = Image.new("RGB", (400, 400), (255, 255, 255))
draw = ImageDraw.Draw(clock)
frame_path = img_path("frame.png")
if os.path.exists(frame_path):
bg = Image.open(frame_path).resize((300, 300), Image.LANCZOS)
clock.paste(bg, (50, 50))
origin = (200, 200)
# Hour hand
draw.line(
[origin,
(200 + 50 * math.sin(math.radians(hour_angle)),
200 - 50 * math.cos(math.radians(hour_angle)))],
fill="#df005e", width=4,
)
# Minute hand
draw.line(
[origin,
(200 + 80 * math.sin(math.radians(min_angle)),
200 - 80 * math.cos(math.radians(min_angle)))],
fill="#033054", width=3,
)
# Second hand
draw.line(
[origin,
(200 + 100 * math.sin(math.radians(sec_angle)),
200 - 100 * math.cos(math.radians(sec_angle)))],
fill="red", width=2,
)
draw.ellipse((195, 195, 210, 210), fill="white")
out_path = img_path("clock.png")
clock.save(out_path)
return out_path