jinye_huang dcfd820ca4 feat(poster_v2): 智能海报生成引擎 v1.0
- 新增 PosterSmartEngine,AI 生成文案 + 海报渲染
- 5 种布局支持文本换行和自适应字体
- 修复按钮/标签颜色显示问题
- 优化渐变遮罩和内容区域计算
- Prompt 优化:标题格式为产品名+描述
2025-12-10 15:04:59 +08:00

83 lines
2.1 KiB
Python

#!/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
)