diff --git a/api/advent22_api/core/advent_image.py b/api/advent22_api/core/advent_image.py index 8bc5fc4..437bf17 100644 --- a/api/advent22_api/core/advent_image.py +++ b/api/advent22_api/core/advent_image.py @@ -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: diff --git a/api/advent22_api/core/depends.py b/api/advent22_api/core/depends.py index 7566644..f07f582 100644 --- a/api/advent22_api/core/depends.py +++ b/api/advent22_api/core/depends.py @@ -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, diff --git a/api/advent22_api/core/helpers.py b/api/advent22_api/core/helpers.py index 78675d1..10ecd2f 100644 --- a/api/advent22_api/core/helpers.py +++ b/api/advent22_api/core/helpers.py @@ -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)