#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 形状渲染器 """ from typing import Tuple, Union, List from PIL import Image, ImageDraw class ShapeRenderer: """形状渲染器""" @staticmethod def draw_rounded_rect( draw: ImageDraw.ImageDraw, bbox: Tuple[int, int, int, int], radius: int, fill: Union[str, Tuple] = None, outline: Union[str, Tuple] = None, width: int = 1 ): """绘制圆角矩形""" draw.rounded_rectangle(bbox, radius=radius, fill=fill, outline=outline, width=width) @staticmethod def draw_line( draw: ImageDraw.ImageDraw, start: Tuple[int, int], end: Tuple[int, int], fill: Union[str, Tuple], width: int = 1 ): """绘制线条""" draw.line([start, end], fill=fill, width=width) @staticmethod def draw_ellipse( draw: ImageDraw.ImageDraw, bbox: Tuple[int, int, int, int], fill: Union[str, Tuple] = None, outline: Union[str, Tuple] = None ): """绘制椭圆/圆形""" draw.ellipse(bbox, fill=fill, outline=outline) @staticmethod def draw_capsule( draw: ImageDraw.ImageDraw, pos: Tuple[int, int], text_width: int, height: int, fill: Union[str, Tuple], padding: int = 10 ) -> Tuple[int, int, int, int]: """ 绘制胶囊形状 (用于标签背景) Returns: bbox (x1, y1, x2, y2) """ x, y = pos bbox = (x, y, x + text_width + padding * 2, y + height) radius = height // 2 draw.rounded_rectangle(bbox, radius=radius, fill=fill) return bbox @staticmethod def draw_decorator_line( draw: ImageDraw.ImageDraw, pos: Tuple[int, int], width: int, fill: Union[str, Tuple], thickness: int = 4 ): """绘制装饰线""" x, y = pos draw.rounded_rectangle( [x, y, x + width, y + thickness], radius=thickness // 2, fill=fill )