advent22/api/advent22_api/routers/days.py

149 lines
3.5 KiB
Python
Raw Normal View History

2022-10-10 20:22:56 +00:00
import colorsys
import random
2022-10-10 23:46:04 +00:00
import re
2022-10-14 21:42:05 +00:00
# from datetime import date
from io import BytesIO
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
2022-10-14 22:09:23 +00:00
from PIL import ImageDraw, ImageFont
2022-10-10 23:46:04 +00:00
2022-10-14 22:15:20 +00:00
from ..dav_common import dav_get_file, dav_list_files
2022-10-14 22:09:23 +00:00
from ._image import AdventImage
2022-10-14 23:03:36 +00:00
from ._misc import set_length, shuffle
router = APIRouter(prefix="/days", tags=["days"])
loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX"
@router.on_event("startup")
2022-10-14 21:42:05 +00:00
async def startup() -> None:
print(loesungswort)
2022-10-14 22:47:16 +00:00
print("".join(await shuffle(loesungswort)))
@router.get("/letter/{index}")
async def get_letter(
index: int,
) -> str:
return (await shuffle(loesungswort))[index]
2022-10-14 21:42:05 +00:00
# @router.get("/date")
# def get_date() -> int:
# return date.today().day
2022-10-14 21:42:05 +00:00
# @router.get(
# "/picture",
# response_class=StreamingResponse,
# )
# async def get_picture():
# img = Image.open("hand.png").convert("RGBA")
# d1 = ImageDraw.Draw(img)
# font = ImageFont.truetype("Lena.ttf", 50)
# d1.text((260, 155), "W", font=font, fill=(0, 0, 255))
# # d1.text(xy=(400, 210), text="Deine Hände auch?",
# # font=Font, fill=(255, 0, 0))
# img_buffer = BytesIO()
# img.save(img_buffer, format="PNG", quality=85)
# img_buffer.seek(0)
# return StreamingResponse(
# content=img_buffer,
# media_type="image/png",
# )
2022-10-10 23:46:04 +00:00
_RE_IMAGE_FILE = re.compile(
r"\.(gif|jpe?g|tiff?|png|bmp)$",
flags=re.IGNORECASE,
)
2022-10-14 23:03:36 +00:00
async def get_images() -> list[str]:
ls = await dav_list_files(_RE_IMAGE_FILE, "/images")
ls = await set_length(ls, 24)
return await shuffle(ls)
2022-10-14 22:09:23 +00:00
async def load_image(
2022-10-11 00:29:27 +00:00
index: int,
2022-10-14 23:03:36 +00:00
images: list[str] = Depends(get_images),
2022-10-14 22:09:23 +00:00
) -> AdventImage:
2022-10-10 20:22:56 +00:00
"""
Bild laden und einen quadratischen Ausschnitt
aus der Mitte nehmen
"""
# Bild laden
2022-10-14 23:03:36 +00:00
img_buffer = await dav_get_file(images[index])
2022-10-14 22:09:23 +00:00
return await AdventImage.load_standard(img_buffer)
2022-10-10 18:44:01 +00:00
@router.get(
"/picture/{index}",
response_class=StreamingResponse,
)
async def get_picture_for_day(
2022-10-10 22:59:52 +00:00
index: int,
letter: str = Depends(get_letter),
2022-10-14 22:09:23 +00:00
adv_img: AdventImage = Depends(load_image),
) -> StreamingResponse:
2022-10-10 20:22:56 +00:00
"""
Bild für einen Tag erstellen
"""
# Font laden
font = ImageFont.truetype("Lena.ttf", 50)
2022-10-10 20:22:56 +00:00
# Position des Buchstaben bestimmen
2022-10-10 22:59:52 +00:00
rnd = random.Random(f"{loesungswort}{index}")
2022-10-11 00:29:27 +00:00
xy = tuple(rnd.choices(range(30, 470), k=2))
2022-10-10 20:22:56 +00:00
# betroffenen Bildbereich bestimmen
2022-10-14 22:09:23 +00:00
text_box = await adv_img.get_text_box(
2022-10-10 18:44:01 +00:00
xy=xy,
2022-10-10 02:12:24 +00:00
text=letter,
font=font,
)
2022-10-10 18:44:01 +00:00
if text_box is not None:
2022-10-10 20:22:56 +00:00
# Durchschnittsfarbe bestimmen
2022-10-14 22:09:23 +00:00
text_color = await adv_img.get_average_color(
2022-10-10 18:44:01 +00:00
box=text_box,
)
2022-10-10 20:22:56 +00:00
# etwas heller/dunkler machen
tc_h, tc_s, tc_v = colorsys.rgb_to_hsv(*text_color)
2022-10-10 22:59:52 +00:00
tc_v = int((tc_v - 127) * 0.97) + 127
2022-10-10 20:22:56 +00:00
if tc_v < 127:
tc_v += 3
else:
tc_v -= 3
text_color = colorsys.hsv_to_rgb(tc_h, tc_s, tc_v)
text_color = tuple(int(val) for val in text_color)
# Buchstaben verstecken
2022-10-14 22:09:23 +00:00
ImageDraw.Draw(adv_img.img).text(
2022-10-10 18:44:01 +00:00
xy=xy,
text=letter,
font=font,
anchor="mm",
fill=text_color,
)
2022-10-10 20:22:56 +00:00
# Bilddaten in Puffer laden
img_buffer = BytesIO()
2022-10-14 22:09:23 +00:00
adv_img.img.save(img_buffer, format="JPEG", quality=85)
img_buffer.seek(0)
return StreamingResponse(
content=img_buffer,
2022-10-10 20:25:11 +00:00
media_type="image/jpeg",
)