🐛 fix advent_image.py
- `get_average_color` working again - refactor `hide_text` (fail-fast)
This commit is contained in:
parent
5c865b8dbb
commit
6ff5af45d5
1 changed files with 31 additions and 26 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import colorsys
|
import colorsys
|
||||||
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Self, TypeAlias, cast
|
from typing import Self, TypeAlias, cast
|
||||||
|
|
||||||
|
|
@ -14,6 +15,8 @@ _RGB: TypeAlias = tuple[int, int, int]
|
||||||
_XY: TypeAlias = tuple[float, float]
|
_XY: TypeAlias = tuple[float, float]
|
||||||
_Box: TypeAlias = tuple[int, int, int, int]
|
_Box: TypeAlias = tuple[int, int, int, int]
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True, frozen=True)
|
@dataclass(slots=True, frozen=True)
|
||||||
class AdventImage:
|
class AdventImage:
|
||||||
|
|
@ -83,15 +86,14 @@ class AdventImage:
|
||||||
async def get_average_color(
|
async def get_average_color(
|
||||||
self,
|
self,
|
||||||
box: _Box,
|
box: _Box,
|
||||||
) -> tuple[int, int, int]:
|
) -> _RGB:
|
||||||
"""
|
"""
|
||||||
Durchschnittsfarbe eines rechteckigen Ausschnitts in
|
Durchschnittsfarbe eines rechteckigen Ausschnitts in
|
||||||
einem Bild berechnen
|
einem Bild berechnen
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pixel_data = np.asarray(self.img.crop(box))
|
pixel_data = np.asarray(self.img.crop(box))
|
||||||
print(pixel_data)
|
mean_color: np.ndarray = np.mean(pixel_data, axis=(0, 1))
|
||||||
mean_color: np.ndarray = np.mean(pixel_data, axis=0)
|
|
||||||
|
|
||||||
return cast(_RGB, tuple(mean_color.astype(int)))
|
return cast(_RGB, tuple(mean_color.astype(int)))
|
||||||
|
|
||||||
|
|
@ -113,31 +115,34 @@ class AdventImage:
|
||||||
xy=xy, text=text, font=font, anchor=anchor, **text_kwargs
|
xy=xy, text=text, font=font, anchor=anchor, **text_kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
if text_box is not None:
|
if text_box is None:
|
||||||
# Durchschnittsfarbe bestimmen
|
_logger.warning("Konnte Bildbereich nicht finden!")
|
||||||
text_color = await self.get_average_color(
|
return
|
||||||
box=text_box,
|
|
||||||
)
|
|
||||||
|
|
||||||
# etwas heller/dunkler machen
|
# Durchschnittsfarbe bestimmen
|
||||||
tc_h, tc_s, tc_v = colorsys.rgb_to_hsv(*text_color)
|
text_color = await self.get_average_color(
|
||||||
tc_v = int((tc_v - 127) * 0.97) + 127
|
box=text_box,
|
||||||
|
)
|
||||||
|
|
||||||
if tc_v < 127:
|
# etwas heller/dunkler machen
|
||||||
tc_v += 3
|
tc_h, tc_s, tc_v = colorsys.rgb_to_hsv(*text_color)
|
||||||
|
tc_v = int((tc_v - 127) * 0.97) + 127
|
||||||
|
|
||||||
else:
|
if tc_v < 127:
|
||||||
tc_v -= 3
|
tc_v += 3
|
||||||
|
|
||||||
text_color = colorsys.hsv_to_rgb(tc_h, tc_s, tc_v)
|
else:
|
||||||
text_color = tuple(int(val) for val in text_color)
|
tc_v -= 3
|
||||||
|
|
||||||
# Buchstaben verstecken
|
text_color = colorsys.hsv_to_rgb(tc_h, tc_s, tc_v)
|
||||||
ImageDraw.Draw(self.img).text(
|
text_color = tuple(int(val) for val in text_color)
|
||||||
xy=xy,
|
|
||||||
text=text,
|
# Buchstaben verstecken
|
||||||
font=font,
|
ImageDraw.Draw(self.img).text(
|
||||||
fill=cast(_RGB, text_color),
|
xy=xy,
|
||||||
anchor=anchor,
|
text=text,
|
||||||
**text_kwargs,
|
font=font,
|
||||||
)
|
fill=cast(_RGB, text_color),
|
||||||
|
anchor=anchor,
|
||||||
|
**text_kwargs,
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue