This commit is contained in:
penner 2023-10-31 20:40:07 +00:00
parent 337d8d34ff
commit 84c5467edb
3 changed files with 8 additions and 7 deletions

View file

@ -5,6 +5,8 @@ from typing import Self, TypeAlias, cast
import numpy as np import numpy as np
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from .config import Config
_RGB: TypeAlias = tuple[int, int, int] _RGB: TypeAlias = tuple[int, int, int]
_XY: TypeAlias = tuple[float, float] _XY: TypeAlias = tuple[float, float]
@ -14,7 +16,7 @@ class AdventImage:
img: Image.Image img: Image.Image
@classmethod @classmethod
async def from_img(cls, img: Image.Image) -> Self: async def from_img(cls, img: Image.Image, cfg: Config) -> Self:
""" """
Einen quadratischen Ausschnitt aus der Mitte des Bilds nehmen Einen quadratischen Ausschnitt aus der Mitte des Bilds nehmen
""" """
@ -39,7 +41,7 @@ class AdventImage:
# skalieren # skalieren
return cls( return cls(
img.resize( img.resize(
size=(1000, 1000), size=(cfg.image.size, cfg.image.size),
resample=Image.LANCZOS, resample=Image.LANCZOS,
) )
) )

View file

@ -97,11 +97,9 @@ class TTFont(BaseModel):
class Image(BaseModel): class Image(BaseModel):
# Quadrat, Seitenlänge in px # Quadrat, Seitenlänge in px
# TODO penner
size: int = 1000 size: int = 1000
# Rand in px, wo keine Buchstaben untergebracht werden # Rand in px, wo keine Buchstaben untergebracht werden
# TODO penner
border: int = 60 border: int = 60
# Schriftarten # Schriftarten

View file

@ -109,7 +109,7 @@ async def gen_day_auto_image(
# Datei existiert garantiert! # Datei existiert garantiert!
img = await load_image(auto_image_names[day]) img = await load_image(auto_image_names[day])
image = await AdventImage.from_img(img) image = await AdventImage.from_img(img, cfg)
rnd = await Random.get(day) rnd = await Random.get(day)
@ -120,8 +120,9 @@ async def gen_day_auto_image(
# Buchstaben verstecken # Buchstaben verstecken
for letter in day_parts[day]: for letter in day_parts[day]:
xy_range = range(cfg.image.border, (cfg.image.size - cfg.image.border))
await image.hide_text( await image.hide_text(
xy=cast(_XY, tuple(rnd.choices(range(60, 940), k=2))), xy=cast(_XY, tuple(rnd.choices(xy_range, k=2))),
text=letter, text=letter,
font=font, font=font,
) )
@ -148,7 +149,7 @@ async def get_day_image(
img = await load_image(f"images_manual/{day}.jpg") img = await load_image(f"images_manual/{day}.jpg")
# Als AdventImage verarbeiten # Als AdventImage verarbeiten
image = await AdventImage.from_img(img) image = await AdventImage.from_img(img, cfg)
return image.img return image.img
except RuntimeError: except RuntimeError: