# -*- coding: utf-8 -*- """生成报告用的两张示意图(PIL 绘制,无外部依赖)。 采用 800×N 的画布 + 3 列布局:图片按 16cm 宽插入 A4 后,正文文字约 6pt, 可直接打印阅读(1600 宽的画布只有 3pt,太小)。 1) flow.png —— 五段链路:采集 → 加工 → 调度 → 验收 → 输出 2) chain.png —— 需求传导链:考试密度 → 错题 → 老师 → 家长 → 孩子 → 闲置 用法:python report/make_diagram.py """ from pathlib import Path from PIL import Image, ImageDraw, ImageFont HERE = Path(__file__).parent FONT_PATH = r"C:\Windows\Fonts\msyh.ttc" FONT_BOLD = r"C:\Windows\Fonts\msyhbd.ttc" ACCENT = (179, 39, 45) INK = (26, 26, 26) MUTED = (110, 110, 110) FILL = (251, 244, 243) FILL_ALT = (247, 247, 247) EDGE = (227, 200, 198) RED_FILL = (253, 235, 234) SCALE = 4 # 超采样倍数(680 逻辑宽 → 2720px) W = 680 COLS = 3 CARD_W = 197 GAP = 24 MARGIN = 18 ROW_H = 150 ROW_GAP = 44 def font(size, bold=False): return ImageFont.truetype(FONT_BOLD if bold else FONT_PATH, int(size * SCALE)) def fit(text, max_w, start=13, bold=False, min_size=7): s = start while s >= min_size: f = font(s, bold) if f.getlength(text) <= max_w: return f s -= 0.25 return font(min_size, bold) def text_center(d, box, text, size, bold=False, color=INK): x0, y0, x1, y1 = box f = fit(text, (x1 - x0) * 0.92, size, bold) w = f.getlength(text) h = f.size d.text(((x0 + x1 - w) / 2, (y0 + y1 - h) / 2 - h * 0.08), text, font=f, fill=color) def card(d, box, title, lines, fill=FILL, edge=EDGE, title_color=ACCENT): x0, y0, x1, y1 = box d.rounded_rectangle(box, radius=8 * SCALE, fill=fill, outline=edge, width=2 * SCALE) d.rectangle((x0, y0 + 8 * SCALE, x0 + 4 * SCALE, y1 - 8 * SCALE), fill=title_color) text_center(d, (x0 + 9 * SCALE, y0 + 7 * SCALE, x1 - 7 * SCALE, y0 + 31 * SCALE), title, 12.5, True, title_color) cy = y0 + 36 * SCALE for ln in lines: f = fit(ln, (x1 - x0) * 0.86, 10, False, 7) d.text((x0 + 12 * SCALE, cy), ln, font=f, fill=MUTED if ln.startswith("·") else INK) cy += 19 * SCALE def arrow(d, p0, p1, color=ACCENT, width=2): import math d.line([p0, p1], fill=color, width=width * SCALE) (x0, y0), (x1, y1) = p0, p1 ang = math.atan2(y1 - y0, x1 - x0) L = 8 * SCALE for s in (+0.45, -0.45): d.line( [(x1, y1), (x1 - L * math.cos(ang - s), y1 - L * math.sin(ang - s))], fill=color, width=width * SCALE, ) def wrap_arrow(d, from_box, to_box, lane_y): """从上一行末尾卡片底部,绕到下一行首卡片顶部。""" x_from = (from_box[0] + from_box[2]) / 2 x_to = (to_box[0] + to_box[2]) / 2 d.line([(x_from, from_box[3] + 2 * SCALE), (x_from, lane_y)], fill=ACCENT, width=2 * SCALE) d.line([(x_from, lane_y), (x_to, lane_y)], fill=ACCENT, width=2 * SCALE) arrow(d, (x_to, lane_y), (x_to, to_box[1] - 2 * SCALE)) def grid(d, items, y0=20, cols=COLS): boxes = [] for i, (title, lines) in enumerate(items): col, row = i % cols, i // cols x = MARGIN + col * (CARD_W + GAP) y = y0 + row * (ROW_H + ROW_GAP) box = (x * SCALE, y * SCALE, (x + CARD_W) * SCALE, (y + ROW_H) * SCALE) fill, edge, tc = FILL if i % 2 == 0 else FILL_ALT, EDGE, ACCENT if title.startswith("闲置") or title.startswith("⑥"): fill, edge, tc = RED_FILL, EDGE, ACCENT card(d, box, title, lines, fill=fill, edge=edge, title_color=tc) boxes.append(box) return boxes def link_row(d, boxes, row, cols=COLS): ay = (boxes[row * cols][1] + boxes[row * cols][3]) / 2 for i in range(cols - 1): a, b = boxes[row * cols + i], boxes[row * cols + i + 1] arrow(d, (a[2] + 2 * SCALE, ay), (b[0] - 2 * SCALE, ay)) def make_flow(): H = 20 + 2 * ROW_H + ROW_GAP + 46 im = Image.new("RGB", (W * SCALE, H * SCALE), "white") d = ImageDraw.Draw(im) items = [ ("① 采集 · 手机", ["拍整卷 / 一页", "自动切题成卡片", "家长勾选真错题"]), ("② 加工 · 云端", ["错因归类(概念/计算/审题)", "AI 讲解 + 知识点打标", "生成变式题"]), ("③ 调度 · 云端", ["按遗忘曲线排期", "每天推 3–5 道", "到期提醒(订阅消息)"]), ("④ 验收 · 手机", ["重做 → 拍照判分", "连对两次才算过关", "错则顺延、缩短间隔"]), ("⑤ 输出 · 电脑", ["A4 排版:题图不分离", "预留答题区", "PDF 导出 / 打印"]), ("断点在哪里", ["任何一环换设备,用户就掉队", "最常见:采集在手机、", "· 加工却在电脑上"]), ] boxes = grid(d, items) link_row(d, boxes, 0) link_row(d, boxes, 1) wrap_arrow(d, boxes[2], boxes[3], (boxes[2][3] + ROW_GAP / 2 * SCALE)) note = "这条链一旦有一环落在「另一个设备」上,用户就会掉队——采集在手机、加工在电脑,是最常见的断点。" f = fit(note, (W - 2 * MARGIN) * SCALE, 11, False, 8) d.text((MARGIN * SCALE, (H - 34) * SCALE), note, font=f, fill=MUTED) out = HERE / "flow.png" im.save(out) print("已生成", out, im.size) def make_chain(): H = 20 + 2 * ROW_H + ROW_GAP + 76 im = Image.new("RGB", (W * SCALE, H * SCALE), "white") d = ImageDraw.Draw(im) items = [ ("考试密度", ["周测、月考", "每科每周 10–30 道错题"]), ("错题产生", ["错题 = 最高信号", "的知识缺口数据"]), ("老师要求", ["「回去整理错题本」", "变成一项作业"]), ("家长买工具", ["辅导不了,但能买", "打印机 / 会员 / App"]), ("孩子用三天", ["采集太麻烦", "没提醒,没验收"]), ("闲置", ["占内存,不再打开"]), ] boxes = grid(d, items) link_row(d, boxes, 0) link_row(d, boxes, 1) wrap_arrow(d, boxes[2], boxes[3], (boxes[2][3] + ROW_GAP / 2 * SCALE)) # 回环:闲置 → 家长买工具(从第 2 行末卡片再绕回同行中间卡片上方) y_lane = (boxes[5][3] + 24 * SCALE) x_from = (boxes[5][0] + boxes[5][2]) / 2 x_to = (boxes[3][0] + boxes[3][2]) / 2 d.line([(x_from, boxes[5][3] + 2 * SCALE), (x_from, y_lane)], fill=ACCENT, width=2 * SCALE) d.line([(x_from, y_lane), (x_to, y_lane)], fill=ACCENT, width=2 * SCALE) arrow(d, (x_to, y_lane), (x_to, boxes[3][3] + 2 * SCALE)) label = "下学期重新买一次" f = fit(label, 200 * SCALE, 10, True, 8) tw = f.getlength(label) mid = (x_from + x_to) / 2 d.rectangle((mid - tw / 2 - 5 * SCALE, y_lane - 12 * SCALE, mid + tw / 2 + 5 * SCALE, y_lane + 4 * SCALE), fill="white") d.text((mid - tw / 2, y_lane - 10 * SCALE), label, font=f, fill=ACCENT) note = "终点不是「产品不好用」,是「产品没进入日常」——真正要解决的是第 4 到第 5 环之间那一段。" f = fit(note, (W - 2 * MARGIN) * SCALE, 11, False, 8) d.text((MARGIN * SCALE, (H - 34) * SCALE), note, font=f, fill=MUTED) out = HERE / "chain.png" im.save(out) print("已生成", out, im.size) if __name__ == "__main__": make_flow() make_chain()