restructure Pillow imports

This commit is contained in:
Jörn-Michael Miehe 2025-11-30 02:38:03 +01:00
parent 510edf5818
commit af08917155
4 changed files with 23 additions and 19 deletions

View file

@ -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:

View file

@ -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
"""

View file

@ -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
"""

View file

@ -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