diff --git a/api/advent22_api/core/advent_image.py b/api/advent22_api/core/advent_image.py index 437bf17..13d6dc2 100644 --- a/api/advent22_api/core/advent_image.py +++ b/api/advent22_api/core/advent_image.py @@ -3,8 +3,10 @@ from dataclasses import dataclass from typing import Self, TypeAlias, cast import numpy as np -from PIL import Image, ImageDraw, ImageFont -from PIL.Image import Resampling +from PIL import Image as PILImage +from PIL import ImageDraw +from PIL.Image import Image, Resampling +from PIL.ImageFont import FreeTypeFont from .config import Config @@ -15,10 +17,10 @@ _Box: TypeAlias = tuple[int, int, int, int] @dataclass(slots=True, frozen=True) class AdventImage: - img: Image.Image + img: Image @classmethod - async def from_img(cls, img: Image.Image, cfg: Config) -> Self: + async def from_img(cls, img: Image, cfg: Config) -> Self: """ Einen quadratischen Ausschnitt aus der Mitte des Bilds nehmen """ @@ -52,7 +54,7 @@ class AdventImage: self, xy: _XY, text: str | bytes, - font: ImageFont.FreeTypeFont, + font: FreeTypeFont, anchor: str | None = "mm", **text_kwargs, ) -> _Box | None: @@ -63,7 +65,7 @@ class AdventImage: """ # Neues 1-Bit Bild, gleiche Größe - mask = Image.new(mode="1", size=self.img.size, color=0) + mask = PILImage.new(mode="1", size=self.img.size) # Text auf Maske auftragen ImageDraw.Draw(mask).text( @@ -97,7 +99,7 @@ class AdventImage: self, xy: _XY, text: str | bytes, - font: ImageFont.FreeTypeFont, + font: 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 f07f582..a9423a2 100644 --- a/api/advent22_api/core/depends.py +++ b/api/advent22_api/core/depends.py @@ -5,7 +5,9 @@ from io import BytesIO from typing import cast from fastapi import Depends -from PIL import Image, ImageFont +from PIL import ImageFont +from PIL.Image import Image +from PIL.ImageFont import FreeTypeFont from .advent_image import _XY, AdventImage from .calendar_config import CalendarConfig, get_calendar_config @@ -138,7 +140,7 @@ class TTFont: size: int = 50 @property - async def font(self) -> ImageFont.FreeTypeFont: + async def font(self) -> FreeTypeFont: return ImageFont.truetype( font=BytesIO(await WebDAV.read_bytes(self.file_name)), size=100, @@ -169,7 +171,7 @@ async def gen_day_auto_image( auto_image_names: dict[int, str], day_parts: dict[int, str], ttfonts: list[TTFont], -) -> Image.Image: +) -> Image: """ Automatisch generiertes Bild erstellen """ @@ -200,7 +202,7 @@ async def get_day_image( auto_image_names: dict[int, str] = Depends(get_all_auto_image_names), day_parts: dict[int, str] = Depends(get_all_parts), ttfonts: list[TTFont] = Depends(get_all_ttfonts), -) -> Image.Image | None: +) -> Image | None: """ Bild für einen Tag abrufen """ diff --git a/api/advent22_api/core/helpers.py b/api/advent22_api/core/helpers.py index 10ecd2f..b75e31c 100644 --- a/api/advent22_api/core/helpers.py +++ b/api/advent22_api/core/helpers.py @@ -6,8 +6,8 @@ from io import BytesIO 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 PIL import Image as PILImage +from PIL.Image import Image, Resampling from .config import get_config from .dav.webdav import WebDAV @@ -104,7 +104,7 @@ list_images_manual = list_helper("/images_manual", RE_IMG) list_fonts = list_helper("/files", RE_TTF) -async def load_image(file_name: str) -> Image.Image: +async def load_image(file_name: str) -> Image: """ Versuche, Bild aus Datei zu laden """ @@ -112,10 +112,10 @@ async def load_image(file_name: str) -> Image.Image: if not await WebDAV.exists(file_name): raise RuntimeError(f"DAV-File {file_name} does not exist!") - return Image.open(BytesIO(await WebDAV.read_bytes(file_name))) + return PILImage.open(BytesIO(await WebDAV.read_bytes(file_name))) -async def api_return_ico(img: Image.Image) -> StreamingResponse: +async def api_return_ico(img: Image) -> StreamingResponse: """ ICO-Bild mit API zurückgeben """ @@ -133,7 +133,7 @@ async def api_return_ico(img: Image.Image) -> StreamingResponse: ) -async def api_return_jpeg(img: Image.Image) -> StreamingResponse: +async def api_return_jpeg(img: Image) -> StreamingResponse: """ JPEG-Bild mit API zurückgeben """ diff --git a/api/advent22_api/routers/user.py b/api/advent22_api/routers/user.py index 65ca8d4..48c0e54 100644 --- a/api/advent22_api/routers/user.py +++ b/api/advent22_api/routers/user.py @@ -2,7 +2,7 @@ from datetime import datetime from fastapi import APIRouter, Depends, HTTPException, status from fastapi.responses import StreamingResponse -from PIL import Image +from PIL.Image import Image from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config from ..core.config import Config, Site, get_config @@ -75,7 +75,7 @@ async def get_doors( async def get_image_for_day( user_can_view: bool = Depends(user_can_view_day), is_admin: bool = Depends(user_is_admin), - image: Image.Image | None = Depends(get_day_image), + image: Image | None = Depends(get_day_image), ) -> StreamingResponse: """ Bild für einen Tag erstellen