"""Mock 图像后端:Pillow 生成占位图。 无 OpenAI key 时也能端到端演示产品流水线(选品 → 底图 → "印花图" → "模特合成" 产物齐全)。 占位图 = 参考图尺寸 + 文字标注(提示词摘要),明确标识 [MOCK] 避免误用。 """ from pathlib import Path from PIL import Image, ImageDraw from .base import ImageBackend class MockImageBackend(ImageBackend): name = "mock" def __init__(self): self._cfg: dict = {} def bind_config(self, cfg: dict): self._cfg = cfg or {} def print(self, prompt: str, base_image: str, out_path: str, negative: str = "", extra_images=None, size: str = "") -> str: size_px = (1024, 1024) try: with Image.open(base_image) as im: size_px = im.size except Exception: pass if size: try: w, h = (int(x) for x in str(size).lower().split("x")) size_px = (w, h) except Exception: pass img = Image.new("RGB", size_px, (240, 240, 248)) d = ImageDraw.Draw(img) d.rectangle([0, 0, size_px[0] - 1, size_px[1] - 1], outline=(180, 180, 200)) d.text((24, 24), f"[MOCK] {Path(out_path).name}", fill=(50, 50, 80)) d.text((24, 56), "(未配置 OpenAI key,占位图演示流程)", fill=(120, 120, 150)) d.text((24, 96), (prompt or "")[:160], fill=(90, 90, 120)) if extra_images: d.text((24, 136), f"参考图 {len(list(extra_images))} 张: " + ", ".join(Path(p).name[:24] for p in extra_images), fill=(90, 90, 120)) Path(out_path).parent.mkdir(parents=True, exist_ok=True) img.save(out_path) return out_path def generate(self, prompt: str, out_path: str, negative: str = "", size: str = "") -> str: """纯文生图(mock):白底 + 文字标注,模拟纯印花设计稿。""" size_px = (1024, 1024) if size: try: w, h = (int(x) for x in str(size).lower().split("x")) size_px = (w, h) except Exception: pass img = Image.new("RGB", size_px, (252, 252, 252)) d = ImageDraw.Draw(img) d.rectangle([0, 0, size_px[0] - 1, size_px[1] - 1], outline=(200, 200, 210)) d.text((24, 24), f"[MOCK DESIGN] {Path(out_path).name}", fill=(50, 50, 80)) d.text((24, 56), "(纯印花设计稿占位,白底)", fill=(120, 120, 150)) d.text((24, 96), (prompt or "")[:160], fill=(90, 90, 120)) Path(out_path).parent.mkdir(parents=True, exist_ok=True) img.save(out_path) return out_path