adjust colors
This commit is contained in:
78
main.py
78
main.py
@@ -1,7 +1,7 @@
|
||||
from dataclasses import dataclass, field
|
||||
from src.api import Timetable, Stop
|
||||
|
||||
from matplotlib.colors import hex2color
|
||||
import fire
|
||||
|
||||
import cv2
|
||||
from PIL import ImageFont, ImageDraw, Image
|
||||
@@ -52,23 +52,23 @@ def denormalize(coords, setup: ImageSetup, use_margins=True):
|
||||
int(y * (setup.height - margins[0] - margins[1]) + margins[0]),
|
||||
)
|
||||
|
||||
|
||||
def create_image(tt: Timetable, setup: ImageSetup | None = None):
|
||||
if setup is None:
|
||||
setup = ImageSetup()
|
||||
|
||||
img = np.zeros((setup.height, setup.width, 3), dtype=np.uint8)
|
||||
# img = np.zeros((setup.height, setup.width, 3), dtype=np.uint8)
|
||||
img = Image.new("RGBA", (setup.width, setup.height), setup.bg_color)
|
||||
|
||||
# fill with background color
|
||||
for i, c in enumerate(hex2color(setup.bg_color)):
|
||||
img[:, :, i] = int(c * 255)
|
||||
# # fill with background color
|
||||
# for i, c in enumerate(hex2color(setup.bg_color)):
|
||||
# img[:, :, i] = int(c * 255)
|
||||
|
||||
### setup done ###
|
||||
|
||||
draw = ImageDraw.Draw(Image.fromarray(img))
|
||||
draw = ImageDraw.Draw(img)
|
||||
|
||||
title_font = ImageFont.truetype(*setup.fonts["title"])
|
||||
# ctitle_font = ImageFont.truetype(*setup.fonts["column_title"])
|
||||
ctitle_font = ImageFont.truetype(*setup.fonts["column_title"])
|
||||
badge_font = ImageFont.truetype(*setup.fonts["badge"])
|
||||
default_font = ImageFont.truetype(*setup.fonts["default"])
|
||||
|
||||
@@ -96,6 +96,7 @@ def create_image(tt: Timetable, setup: ImageSetup | None = None):
|
||||
xy_start = denormalize((0,0.15), setup)
|
||||
dest_x = denormalize((0.12, 0), setup)[0]
|
||||
dep_x = denormalize((0.5, 0), setup)[0]
|
||||
delay_x = denormalize((0.6, 0), setup)[0]
|
||||
max_dest_len = dep_x - dest_x - 60
|
||||
until_x = denormalize((0.5+0.5-0.12, 0), setup)[0]
|
||||
# col_title_y = denormalize((0, 0.15), setup)[1]
|
||||
@@ -163,26 +164,38 @@ def create_image(tt: Timetable, setup: ImageSetup | None = None):
|
||||
|
||||
until_text = f"{time_until:d} min"
|
||||
|
||||
until_color = setup.text_colors["default"]
|
||||
delay_color = setup.text_colors["column_title"]
|
||||
dep_color = setup.text_colors["default"]
|
||||
dest_color = setup.text_colors["default"]
|
||||
|
||||
delay_text = ""
|
||||
|
||||
if stop.departure_delay is not None:
|
||||
dep_text = f"{dep_text} ({stop.departure_delay:+d})"
|
||||
delay_text = f"{"+" if stop.departure_delay >= 0 else "-"} {abs(stop.departure_delay):d}"
|
||||
if stop.departure_delay > 5:
|
||||
delay_color = "#c8683f"
|
||||
elif stop.departure_delay < 0:
|
||||
delay_color = "#48c144"
|
||||
|
||||
if time_until <= 6:
|
||||
dep_color = "#c83f4e"
|
||||
elif 6 < time_until < 10:
|
||||
dep_color = "#c8633f"
|
||||
until_color = "#c8683f"
|
||||
elif 6 < time_until <= 10:
|
||||
until_color = "#b79b46"
|
||||
|
||||
if stop.departure_canceled:
|
||||
dep_color = "#c83f4e"
|
||||
dest_color = "#c83f4e"
|
||||
dep_text = "Fällt aus :("
|
||||
dep_color = "#9f3c76"
|
||||
until_text = ""
|
||||
until_text = "----"
|
||||
delay_text = ""
|
||||
|
||||
draw.text(
|
||||
xy=(dest_x, center_xy[1]),
|
||||
text=dest_text,
|
||||
font=default_font,
|
||||
anchor="lm",
|
||||
fill=dep_color,
|
||||
fill=dest_color,
|
||||
)
|
||||
|
||||
draw.text(
|
||||
@@ -193,30 +206,49 @@ def create_image(tt: Timetable, setup: ImageSetup | None = None):
|
||||
fill=dep_color,
|
||||
)
|
||||
|
||||
draw.text(
|
||||
xy=(delay_x, center_xy[1]),
|
||||
text=delay_text,
|
||||
font=ctitle_font,
|
||||
anchor="lm",
|
||||
fill=delay_color,
|
||||
)
|
||||
|
||||
draw.text(
|
||||
xy=(until_x, center_xy[1]),
|
||||
text=until_text,
|
||||
font=default_font,
|
||||
anchor="lm",
|
||||
fill=dep_color,
|
||||
fill=until_color,
|
||||
)
|
||||
|
||||
current_xys_line_badge[1] += line_height
|
||||
current_xys_line_badge[3] += line_height
|
||||
|
||||
|
||||
# if gif is not None:
|
||||
# gif = Image.open(gif)
|
||||
# gif.seek(gif_frame % gif.n_frames)
|
||||
# frame = gif.convert("RGBA")
|
||||
# frame_size = frame.size
|
||||
# frame_anchor = denormalize((1, 1), setup, use_margins=False)
|
||||
# img.paste(frame, (frame_anchor[0]-frame_size[0], frame_anchor[1]-frame_size[1]), frame)
|
||||
|
||||
# last step, convert from rgb to bgr
|
||||
img = np.asarray(draw.im, dtype=np.uint8).reshape(setup.height, setup.width, 3)
|
||||
return np.flip(img, axis=-1)
|
||||
img = img.convert("RGB")
|
||||
img = np.array(img)
|
||||
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
||||
return img
|
||||
|
||||
|
||||
def main():
|
||||
# eva = 8002377
|
||||
eva = 8004158
|
||||
def main(eva=None):
|
||||
eva = 8002377 if eva is None else eva
|
||||
# eva = 8098263
|
||||
# eva = 8004158
|
||||
|
||||
cv2.namedWindow("fs", cv2.WND_PROP_FULLSCREEN)
|
||||
cv2.setWindowProperty("fs", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
||||
cv2.moveWindow("fs", 1920, 0)
|
||||
# cv2.moveWindow("fs", -1920, 0)
|
||||
|
||||
tt = Timetable(eva=eva)
|
||||
|
||||
@@ -236,4 +268,4 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
fire.Fire(main)
|
||||
|
||||
Reference in New Issue
Block a user