Merge branch 'develop' into feature/refactoring

This commit is contained in:
Jörn-Michael Miehe 2023-09-08 11:14:01 +00:00
commit c8ce12c299
3 changed files with 16 additions and 10 deletions

View file

@ -16,9 +16,11 @@ class Server(BaseModel):
font: str
# Monat, während dem der Kalender läuft
# TODO penner
month: int = 12
# Alle Türen bleiben noch so viele Monate offen
# TODO penner
keep_open: int = 3
@ -27,6 +29,7 @@ class Puzzle(BaseModel):
solution: str
# Länge des Kalenders
# TODO penner
days: int = 24
# Kalenderdefinition

View file

@ -1,10 +1,13 @@
import colorsys
from dataclasses import dataclass
from typing import Self
from typing import Self, TypeAlias, cast
import numpy as np
from PIL import Image, ImageDraw, ImageFont
_RGB: TypeAlias = tuple[int, int, int]
_XY: TypeAlias = tuple[float, float]
@dataclass
class AdventImage:
@ -44,7 +47,7 @@ class AdventImage:
async def get_text_box(
self,
xy: tuple[float, float],
xy: _XY,
text: str | bytes,
font: "ImageFont._Font",
anchor: str | None = "mm",
@ -75,20 +78,20 @@ class AdventImage:
async def get_average_color(
self,
box: tuple[int, int, int, int],
) -> tuple[int, int, int]:
) -> _RGB:
"""
Durchschnittsfarbe eines rechteckigen Ausschnitts in
einem Bild berechnen
"""
pixel_data = self.img.crop(box).getdata()
mean_color: np.ndarray = np.mean(a=pixel_data, axis=0)
mean_color: np.ndarray = np.mean(pixel_data, axis=0)
return tuple(mean_color.astype(int).tolist())
return cast(_RGB, tuple(mean_color.astype(int)))
async def hide_text(
self,
xy: tuple[float, float],
xy: _XY,
text: str | bytes,
font: "ImageFont._Font",
anchor: str | None = "mm",
@ -128,7 +131,7 @@ class AdventImage:
xy=xy,
text=text,
font=font,
fill=text_color,
fill=cast(_RGB, text_color),
anchor=anchor,
**text_kwargs,
)

View file

@ -2,7 +2,7 @@ import itertools
import random
import re
from io import BytesIO
from typing import Any, Self, Sequence
from typing import Any, Self, Sequence, cast
from fastapi import Depends
from fastapi.responses import StreamingResponse
@ -10,7 +10,7 @@ from PIL import Image, ImageFont
from ..config import Config, get_config
from ..dav_common import dav_file_exists, dav_get_file, dav_list_files
from ._image import AdventImage
from ._image import _XY, AdventImage
##########
# RANDOM #
@ -99,7 +99,7 @@ async def get_auto_image(
# Buchstabe verstecken
await image.hide_text(
xy=tuple(rnd.choices(range(30, 470), k=2)),
xy=cast(_XY, tuple(rnd.choices(range(30, 470), k=2))),
text=letter,
font=ImageFont.truetype(font, 50),
)