fix typing issues

This commit is contained in:
Jörn-Michael Miehe 2023-09-08 11:13:51 +00:00
parent 69e4590305
commit d1158b1e10
2 changed files with 13 additions and 10 deletions

View file

@ -1,10 +1,13 @@
import colorsys import colorsys
from dataclasses import dataclass from dataclasses import dataclass
from typing import Self 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
_RGB: TypeAlias = tuple[int, int, int]
_XY: TypeAlias = tuple[float, float]
@dataclass @dataclass
class AdventImage: class AdventImage:
@ -44,7 +47,7 @@ class AdventImage:
async def get_text_box( async def get_text_box(
self, self,
xy: tuple[float, float], xy: _XY,
text: str | bytes, text: str | bytes,
font: "ImageFont._Font", font: "ImageFont._Font",
anchor: str | None = "mm", anchor: str | None = "mm",
@ -75,20 +78,20 @@ class AdventImage:
async def get_average_color( async def get_average_color(
self, self,
box: tuple[int, int, int, int], box: tuple[int, int, int, int],
) -> tuple[int, int, int]: ) -> _RGB:
""" """
Durchschnittsfarbe eines rechteckigen Ausschnitts in Durchschnittsfarbe eines rechteckigen Ausschnitts in
einem Bild berechnen einem Bild berechnen
""" """
pixel_data = self.img.crop(box).getdata() pixel_data = self.img.crop(box).getdata()
mean_color: np.ndarray = np.mean(a=pixel_data, axis=0) mean_color: np.ndarray = np.mean(pixel_data, axis=0)
return tuple(mean_color.astype(int).tolist()) return cast(_RGB, tuple(mean_color.astype(int)))
async def hide_text( async def hide_text(
self, self,
xy: tuple[float, float], xy: _XY,
text: str | bytes, text: str | bytes,
font: "ImageFont._Font", font: "ImageFont._Font",
anchor: str | None = "mm", anchor: str | None = "mm",
@ -128,7 +131,7 @@ class AdventImage:
xy=xy, xy=xy,
text=text, text=text,
font=font, font=font,
fill=text_color, fill=cast(_RGB, text_color),
anchor=anchor, anchor=anchor,
**text_kwargs, **text_kwargs,
) )

View file

@ -2,7 +2,7 @@ import itertools
import random import random
import re import re
from io import BytesIO from io import BytesIO
from typing import Any, Self, Sequence from typing import Any, Self, Sequence, cast
from fastapi import Depends from fastapi import Depends
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
@ -10,7 +10,7 @@ from PIL import Image, ImageFont
from ..config import Config, get_config from ..config import Config, get_config
from ..dav_common import dav_file_exists, dav_get_file, dav_list_files from ..dav_common import dav_file_exists, dav_get_file, dav_list_files
from ._image import AdventImage from ._image import _XY, AdventImage
########## ##########
# RANDOM # # RANDOM #
@ -101,7 +101,7 @@ async def get_auto_image(
# Buchstabe verstecken # Buchstabe verstecken
await image.hide_text( await image.hide_text(
xy=tuple(rnd.choices(range(30, 470), k=2)), xy=cast(_XY, tuple(rnd.choices(range(30, 470), k=2))),
text=letter, text=letter,
font=ImageFont.truetype(font, 50), font=ImageFont.truetype(font, 50),
) )