# -*- coding: utf-8 -*- """ 从反白 logo 提取左侧图形生成 favicon(180x180 PNG): 深色圆角底(与站内卡片底色一致)+ 图形居中占 72% 标签栏明暗模式均以深色块呈现,反白图形恒清晰。 用法: uv run --with pillow python3 tools/make_favicon.py """ from PIL import Image, ImageDraw SRC = 'assets/img/logo-light.png' OUT = 'assets/img/favicon.png' SIZE = 180 PAD_GRAPHIC = 0.72 # 图形占画布比例 # logo 左侧图形实测内容段(x 投影分析):x[47:224] y[44:260],取 bbox 略收边 CROP = (47, 44, 224, 260) BG = (7, 11, 22) # 站内 --bg-1 RADIUS = 38 graphic = Image.open(SRC).convert('RGBA').crop(CROP) canvas = Image.new('RGBA', (SIZE, SIZE), (0, 0, 0, 0)) # 圆角深色底 mask = Image.new('L', (SIZE, SIZE), 0) ImageDraw.Draw(mask).rounded_rectangle((0, 0, SIZE, SIZE), RADIUS, fill=255) canvas.paste(Image.new('RGBA', (SIZE, SIZE), BG + (255,)), (0, 0), mask) # 图形等比缩放到 72% 高度内居中 target = round(SIZE * PAD_GRAPHIC) scale = min(target / graphic.width, target / graphic.height) graphic = graphic.resize((round(graphic.width * scale), round(graphic.height * scale)), Image.LANCZOS) canvas.alpha_composite(graphic, ((SIZE - graphic.width) // 2, (SIZE - graphic.height) // 2)) # 自检:中心应有不透明图形像素,四角透明 px = canvas.load() assert px[SIZE // 2, SIZE // 2][3] > 0, '中心无图形' assert px[2, 2][3] == 0 and px[SIZE - 3, SIZE - 3][3] == 0, '圆角未生效' canvas.save(OUT, 'PNG', optimize=True) print(f'完成: {OUT} {SIZE}x{SIZE},图形 {graphic.size}')