# -*- coding: utf-8 -*- # view/assets/nine_slice_scaler.py from __future__ import annotations from PIL import Image, ImageChops, ImageDraw, ImageTk BOX_SOURCE_CORNER_RATIO = { "box_big": 0.16, "box_gold_small": 0.24, "box_gold_double": 0.16, "box_purple": 0.22, "dropdown_box": 0.085, } BOX_TARGET_CORNER_SPEC = { "box_big": (0.14, 18, 46), "box_gold_small": (0.22, 14, 34), "box_gold_double": (0.18, 12, 26), "box_purple": (0.18, 14, 32), "dropdown_box": (0.10, 10, 18), } def clean_box_matte_fast(pil_img: Image.Image | None, hollow_center: bool = True) -> Image.Image | None: if pil_img is None: return None rgba = pil_img.convert("RGBA") w, h = rgba.size if w < 8 or h < 8: return rgba r, g, b, a = rgba.split() luma = ImageChops.lighter(ImageChops.lighter(r, g), b) seed_img = luma.point(lambda p: 0 if p < 18 else 255) seeds = [(0, 0), (w - 1, 0), (w - 1, h - 1), (0, h - 1)] if hollow_center: seeds.append((w // 2, h // 2)) changed = False for sx, sy in seeds: if rgba.getpixel((sx, sy))[3] > 240 and seed_img.getpixel((sx, sy)) == 0: ImageDraw.floodfill(seed_img, (sx, sy), 128) changed = True if changed: ext_alpha = seed_img.point(lambda p: 0 if p == 128 else 255) rgba.putalpha(ImageChops.darker(a, ext_alpha)) return rgba class NineSliceScaler: """Skaliert und cached 9-Slice-Zierrahmen für Boxen und Dropdowns.""" @staticmethod def get_9slice_pil( raw_boxes: dict[str, Image.Image | None], pil_cache: dict, box_key: str, target_size: tuple[int, int], corner_ratio: float | None = None, border_scale: float = 1.0, ) -> Image.Image | None: src = raw_boxes.get(box_key) tw, th = max(16, int(target_size[0])), max(16, int(target_size[1])) if src is None: return None src_ratio = corner_ratio if corner_ratio is not None else BOX_SOURCE_CORNER_RATIO.get(box_key, 0.16) pil_key = ("9slice_pil", box_key, (tw, th), src_ratio, round(border_scale, 3)) if pil_key in pil_cache: return pil_cache[pil_key] sw, sh = src.size sc = max(12, int(round(min(sw, sh) * src_ratio))) rel_target, min_dc, max_dc = BOX_TARGET_CORNER_SPEC.get(box_key, (0.14, 12, 36)) dc = max(min_dc, min(max_dc, int(round(min(tw, th) * rel_target * border_scale)))) dc = min(dc, sc, tw // 2 - 1, th // 2 - 1) if dc < 6: out = src.resize((tw, th), Image.Resampling.BILINEAR) pil_cache[pil_key] = out return out out = Image.new("RGBA", (tw, th), (0, 0, 0, 0)) out.paste(src.crop((0, 0, sc, sc)).resize((dc, dc), Image.Resampling.LANCZOS), (0, 0)) out.paste(src.crop((sw - sc, 0, sw, sc)).resize((dc, dc), Image.Resampling.LANCZOS), (tw - dc, 0)) out.paste(src.crop((0, sh - sc, sc, sh)).resize((dc, dc), Image.Resampling.LANCZOS), (0, th - dc)) out.paste(src.crop((sw - sc, sh - sc, sw, sh)).resize((dc, dc), Image.Resampling.LANCZOS), (tw - dc, th - dc)) mid_w, mid_h = tw - 2 * dc, th - 2 * dc if mid_w > 0: out.paste(src.crop((sc, 0, sw - sc, sc)).resize((mid_w, dc), Image.Resampling.BILINEAR), (dc, 0)) out.paste(src.crop((sc, sh - sc, sw - sc, sh)).resize((mid_w, dc), Image.Resampling.BILINEAR), (dc, th - dc)) if mid_h > 0: out.paste(src.crop((0, sc, sc, sh - sc)).resize((dc, mid_h), Image.Resampling.BILINEAR), (0, dc)) out.paste(src.crop((sw - sc, sc, sw, sh - sc)).resize((dc, mid_h), Image.Resampling.BILINEAR), (tw - dc, dc)) if mid_w > 0 and mid_h > 0 and box_key == "dropdown_box": out.paste(src.crop((sc, sc, sw - sc, sh - sc)).resize((mid_w, mid_h), Image.Resampling.BILINEAR), (dc, dc)) pil_cache[pil_key] = out return out @classmethod def get_box_photo( cls, raw_boxes: dict[str, Image.Image | None], pil_cache: dict, tk_cache: dict, box_key: str, size: tuple[int, int], corner_ratio: float | None = None, border_scale: float = 1.0, ) -> ImageTk.PhotoImage | None: tk_key = ("9slice_tk", box_key, size, corner_ratio, round(border_scale, 3)) if tk_key not in tk_cache: pil_box = cls.get_9slice_pil(raw_boxes, pil_cache, box_key, size, corner_ratio, border_scale) if pil_box is not None: tk_cache[tk_key] = ImageTk.PhotoImage(pil_box) return tk_cache.get(tk_key)