fix Pillow related issues

This commit is contained in:
Jörn-Michael Miehe 2025-11-29 02:39:38 +01:00
parent ec3f846c01
commit 510edf5818
3 changed files with 12 additions and 8 deletions

View file

@ -4,11 +4,13 @@ from typing import Self, TypeAlias, cast
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from PIL.Image import Resampling
from .config import Config
_RGB: TypeAlias = tuple[int, int, int]
_XY: TypeAlias = tuple[float, float]
_Box: TypeAlias = tuple[int, int, int, int]
@dataclass(slots=True, frozen=True)
@ -42,7 +44,7 @@ class AdventImage:
return cls(
img.resize(
size=(cfg.image.size, cfg.image.size),
resample=Image.LANCZOS,
resample=Resampling.LANCZOS,
)
)
@ -50,10 +52,10 @@ class AdventImage:
self,
xy: _XY,
text: str | bytes,
font: "ImageFont._Font",
font: ImageFont.FreeTypeFont,
anchor: str | None = "mm",
**text_kwargs,
) -> "Image._Box | None":
) -> _Box | None:
"""
Koordinaten (links, oben, rechts, unten) des betroffenen
Rechtecks bestimmen, wenn das Bild mit einem Text
@ -78,14 +80,15 @@ class AdventImage:
async def get_average_color(
self,
box: "Image._Box",
box: _Box,
) -> tuple[int, int, int]:
"""
Durchschnittsfarbe eines rechteckigen Ausschnitts in
einem Bild berechnen
"""
pixel_data = self.img.crop(box).getdata()
pixel_data = np.asarray(self.img.crop(box))
print(pixel_data)
mean_color: np.ndarray = np.mean(pixel_data, axis=0)
return cast(_RGB, tuple(mean_color.astype(int)))
@ -94,7 +97,7 @@ class AdventImage:
self,
xy: _XY,
text: str | bytes,
font: "ImageFont._Font",
font: ImageFont.FreeTypeFont,
anchor: str | None = "mm",
**text_kwargs,
) -> None:

View file

@ -138,7 +138,7 @@ class TTFont:
size: int = 50
@property
async def font(self) -> "ImageFont._Font":
async def font(self) -> ImageFont.FreeTypeFont:
return ImageFont.truetype(
font=BytesIO(await WebDAV.read_bytes(self.file_name)),
size=100,

View file

@ -7,6 +7,7 @@ from typing import Any, Awaitable, Callable, Iterable, Self, Sequence, TypeVar
from fastapi.responses import StreamingResponse
from PIL import Image
from PIL.Image import Resampling
from .config import get_config
from .dav.webdav import WebDAV
@ -121,7 +122,7 @@ async def api_return_ico(img: Image.Image) -> StreamingResponse:
# JPEG-Daten in Puffer speichern
img_buffer = BytesIO()
img.resize(size=(256, 256), resample=Image.LANCZOS)
img.resize(size=(256, 256), resample=Resampling.LANCZOS)
img.save(img_buffer, format="ICO")
img_buffer.seek(0)