import random from datetime import date from io import BytesIO import numpy as np from fastapi import APIRouter, Depends from fastapi.responses import StreamingResponse from PIL import Image, ImageDraw, ImageFont router = APIRouter(prefix="/days", tags=["days"]) loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX" async def shuffle(string: str) -> str: rnd = random.Random(loesungswort) return "".join(rnd.sample(string, len(string))) @router.on_event("startup") async def narf() -> None: print(loesungswort) print(await shuffle(loesungswort)) @router.get("/letter/{index}") async def get_letter( index: int, ) -> str: return (await shuffle(loesungswort))[index] @router.get("/date") def get_date() -> int: return date.today().day @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", ) async def load_picture_standard() -> Image.Image: img = Image.open("hand.png") width, height = img.size square = min(img.size) img = img.crop(box=( int((width - square)/2), int((height - square)/2), int((width + square)/2), int((height + square)/2), )) img = img.resize( size=(400, 400), resample=Image.ANTIALIAS, ) return img.convert("RGB") @router.get( "/picture/{index}", response_class=StreamingResponse, ) async def get_picture_for_day( letter: str = Depends(get_letter), img: Image.Image = Depends(load_picture_standard), ) -> StreamingResponse: font = ImageFont.truetype("Lena.ttf", 50) txt_layer = Image.new(mode="RGB", size=(400, 400), color=(0, 0, 0)) txt_color = (255, 255, 255) ImageDraw.Draw(txt_layer).text( xy=(300, 300), text=letter, font=font, anchor="mm", fill=txt_color, ) if not (bbox := txt_layer.getbbox()) is None: mean = np.mean(img.crop(bbox).getdata(), axis=0) txt_color = tuple(int(val) for val in mean) ImageDraw.Draw(img).text( xy=(300, 300), text=letter, font=font, anchor="mm", fill=txt_color, ) img_buffer = BytesIO() img.save(img_buffer, format="PNG", quality=85) img_buffer.seek(0) return StreamingResponse( content=img_buffer, media_type="image/png", )