refactor(报告): 图片改为独立JPG文件引用,不再base64内嵌
- 测试结果图(258张)和原图(3张)以JPG存入 static/bench/{matrix,hairstyle,orig}/
- 报告HTML改为 <img src> 引用相对路径,体积从 ~45MB 降至 ~30KB
- 报告和图片均部署在 static/ 下,可通过URL直接访问
This commit is contained in:
@@ -14,19 +14,16 @@ RESULTS = OUT / "results.json"
|
||||
HTML = OUT / "report.html"
|
||||
|
||||
|
||||
def img_b64(path, max_w=300, quality=70):
|
||||
"""读图片,缩放到 max_w 宽、JPEG 降质后转 base64 data URI(压缩体积便于入 git)。"""
|
||||
if not path or not os.path.isfile(path):
|
||||
def img_src(path):
|
||||
"""把绝对路径转成报告里的相对 URL(报告在 static/,图片在 static/bench/)。"""
|
||||
if not path:
|
||||
return None
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
im = Image.open(path).convert("RGB")
|
||||
if im.width > max_w:
|
||||
nh = round(im.height * max_w / im.width)
|
||||
im = im.resize((max_w, nh), Image.LANCZOS)
|
||||
buf = BytesIO()
|
||||
im.save(buf, format="JPEG", quality=quality, optimize=True)
|
||||
return f"data:image/jpeg;base64,{base64.b64encode(buf.getvalue()).decode()}"
|
||||
p = str(path)
|
||||
if "benchmark_out/hairstyle/" in p:
|
||||
return "bench/hairstyle/" + os.path.basename(p)
|
||||
if "benchmark_out/matrix/" in p:
|
||||
return "bench/matrix/" + os.path.basename(p)
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
@@ -63,16 +60,17 @@ def main():
|
||||
# 发型+图标签
|
||||
label = f'<div class="row-label">{r["img"]}<br><b>{r["hair_name"]}</b></div>'
|
||||
# 原图
|
||||
orig = img_b64(r["img_path"])
|
||||
ORIG_SRC = {"asdf": "bench/orig/asdf.jpg", "qwer": "bench/orig/qwer.jpg", "girl5": "bench/orig/girl5.jpg"}
|
||||
orig = ORIG_SRC.get(r["img"])
|
||||
cells = [f'<td class="cell-orig"><div class="row-label-cell">{label}</div>'
|
||||
f'<img class="orig-img" src="{orig}"></td>']
|
||||
# 10个结果列
|
||||
for ct in columns:
|
||||
c = next((x for x in r["cells"] if x.get("title") == ct), {})
|
||||
uri = img_b64(c.get("grown_path")) if not c.get("error") else None
|
||||
if uri:
|
||||
src = img_src(c.get("grown_path")) if not c.get("error") else None
|
||||
if src:
|
||||
cells.append(
|
||||
f'<td class="cell-result"><img class="result-img" src="{uri}" loading="lazy">'
|
||||
f'<td class="cell-result"><img class="result-img" src="{src}" loading="lazy">'
|
||||
f'<div class="cell-time">{c["elapsed"]:.1f}s</div></td>')
|
||||
else:
|
||||
cells.append(f'<td class="cell-result"><div class="na">⚠</div></td>')
|
||||
|
||||
Reference in New Issue
Block a user