2022-10-10 20:22:56 +00:00
|
|
|
import colorsys
|
2022-10-10 00:09:09 +00:00
|
|
|
import random
|
2022-10-10 23:46:04 +00:00
|
|
|
import re
|
2022-10-10 00:09:09 +00:00
|
|
|
from datetime import date
|
|
|
|
from io import BytesIO
|
|
|
|
|
2022-10-10 02:12:24 +00:00
|
|
|
import numpy as np
|
2022-10-10 00:09:09 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
2022-10-10 23:46:04 +00:00
|
|
|
from webdav3.client import Client as WebDAVclient
|
|
|
|
|
|
|
|
from ..settings import SETTINGS
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/days", tags=["days"])
|
|
|
|
|
|
|
|
loesungswort = "ABCDEFGHIJKLMNOPQRSTUVWX"
|
|
|
|
|
2022-10-10 23:46:04 +00:00
|
|
|
_WEBDAV_CLIENT = WebDAVclient({
|
|
|
|
"webdav_hostname": SETTINGS.webdav.url,
|
|
|
|
"webdav_login": SETTINGS.webdav.username,
|
|
|
|
"webdav_password": SETTINGS.webdav.password,
|
|
|
|
"disable_check": SETTINGS.webdav.disable_check,
|
|
|
|
})
|
|
|
|
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
2022-10-10 23:46:04 +00:00
|
|
|
_RE_IMAGE_FILE = re.compile(
|
|
|
|
r"\.(gif|jpe?g|tiff?|png|bmp)$",
|
|
|
|
flags=re.IGNORECASE,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def list_images(path: str) -> list[str]:
|
|
|
|
ls = _WEBDAV_CLIENT.list(path)
|
|
|
|
return [
|
|
|
|
path
|
|
|
|
for path in ls
|
|
|
|
if _RE_IMAGE_FILE.search(path)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_file(path: str) -> BytesIO:
|
|
|
|
resource = _WEBDAV_CLIENT.resource(path)
|
|
|
|
buffer = BytesIO()
|
|
|
|
resource.write_to(buffer)
|
|
|
|
buffer.seek(0)
|
|
|
|
|
|
|
|
return buffer
|
|
|
|
|
2022-10-10 00:09:09 +00:00
|
|
|
|
|
|
|
async def load_picture_standard() -> Image.Image:
|
2022-10-10 20:22:56 +00:00
|
|
|
"""
|
|
|
|
Bild laden und einen quadratischen Ausschnitt
|
|
|
|
aus der Mitte nehmen
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Bild laden
|
2022-10-10 23:46:04 +00:00
|
|
|
img_buffer = await get_file("lena2.jpg")
|
|
|
|
img = Image.open(img_buffer)
|
2022-10-10 20:22:56 +00:00
|
|
|
|
|
|
|
# Größen bestimmen
|
2022-10-10 00:09:09 +00:00
|
|
|
width, height = img.size
|
2022-10-10 20:22:56 +00:00
|
|
|
square = min(width, height)
|
2022-10-10 00:09:09 +00:00
|
|
|
|
2022-10-10 20:22:56 +00:00
|
|
|
# Bild zuschneiden und skalieren
|
2022-10-10 00:09:09 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2022-10-10 20:22:56 +00:00
|
|
|
# Farbmodell festlegen
|
2022-10-10 00:09:09 +00:00
|
|
|
return img.convert("RGB")
|
|
|
|
|
|
|
|
|
2022-10-10 18:44:01 +00:00
|
|
|
async def get_text_box(
|
|
|
|
img: Image.Image,
|
|
|
|
xy: tuple[float, float],
|
|
|
|
text: str | bytes,
|
|
|
|
font: "ImageFont._Font",
|
|
|
|
anchor: str | None = "mm",
|
|
|
|
**text_kwargs,
|
|
|
|
) -> tuple[int, int, int, int] | None:
|
2022-10-10 20:22:56 +00:00
|
|
|
"""
|
|
|
|
Koordinaten (links, oben, rechts, unten) des betroffenen
|
|
|
|
Rechtecks bestimmen, wenn das Bild `img` mit einem Text
|
|
|
|
versehen wird
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Neues 1-Bit Bild, gleiche Größe
|
2022-10-10 18:44:01 +00:00
|
|
|
mask = Image.new(mode="1", size=img.size, color=0)
|
|
|
|
|
2022-10-10 20:22:56 +00:00
|
|
|
# Text auf Maske auftragen
|
2022-10-10 18:44:01 +00:00
|
|
|
ImageDraw.Draw(mask).text(
|
|
|
|
xy=xy,
|
|
|
|
text=text,
|
|
|
|
font=font,
|
|
|
|
anchor=anchor,
|
|
|
|
fill=1,
|
|
|
|
**text_kwargs,
|
|
|
|
)
|
|
|
|
|
2022-10-10 20:22:56 +00:00
|
|
|
# betroffenen Pixelbereich bestimmen
|
2022-10-10 18:44:01 +00:00
|
|
|
return mask.getbbox()
|
|
|
|
|
|
|
|
|
|
|
|
async def get_average_color(
|
|
|
|
img: Image.Image,
|
|
|
|
box: tuple[int, int, int, int],
|
|
|
|
) -> tuple[int, int, int]:
|
2022-10-10 20:22:56 +00:00
|
|
|
"""
|
|
|
|
Durchschnittsfarbe eines rechteckigen Ausschnitts in
|
|
|
|
einem Bild `img` berechnen
|
|
|
|
"""
|
|
|
|
|
2022-10-10 18:44:01 +00:00
|
|
|
pixel_data = img.crop(box).getdata()
|
|
|
|
mean_color: np.ndarray = np.mean(pixel_data, axis=0)
|
|
|
|
|
|
|
|
return tuple(mean_color.astype(int).tolist())
|
|
|
|
|
|
|
|
|
2022-10-10 00:09:09 +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,
|
2022-10-10 00:09:09 +00:00
|
|
|
letter: str = Depends(get_letter),
|
|
|
|
img: Image.Image = Depends(load_picture_standard),
|
|
|
|
) -> StreamingResponse:
|
2022-10-10 20:22:56 +00:00
|
|
|
"""
|
|
|
|
Bild für einen Tag erstellen
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Font laden
|
2022-10-10 00:09:09 +00:00
|
|
|
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}")
|
|
|
|
xy = tuple(rnd.choices(range(30, 370), k=2))
|
2022-10-10 20:22:56 +00:00
|
|
|
|
|
|
|
# betroffenen Bildbereich bestimmen
|
2022-10-10 18:44:01 +00:00
|
|
|
text_box = await get_text_box(
|
|
|
|
img=img,
|
|
|
|
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-10 18:44:01 +00:00
|
|
|
text_color = await get_average_color(
|
|
|
|
img=img,
|
|
|
|
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-10 18:44:01 +00:00
|
|
|
ImageDraw.Draw(img).text(
|
|
|
|
xy=xy,
|
|
|
|
text=letter,
|
|
|
|
font=font,
|
|
|
|
anchor="mm",
|
|
|
|
fill=text_color,
|
|
|
|
)
|
2022-10-10 00:09:09 +00:00
|
|
|
|
2022-10-10 20:22:56 +00:00
|
|
|
# Bilddaten in Puffer laden
|
2022-10-10 00:09:09 +00:00
|
|
|
img_buffer = BytesIO()
|
2022-10-10 20:24:37 +00:00
|
|
|
img.save(img_buffer, format="JPEG", quality=85)
|
2022-10-10 00:09:09 +00:00
|
|
|
img_buffer.seek(0)
|
|
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
content=img_buffer,
|
2022-10-10 20:25:11 +00:00
|
|
|
media_type="image/jpeg",
|
2022-10-10 00:09:09 +00:00
|
|
|
)
|