Files
live-hub-py/web/extension/huya-exchange-helper/generate_icons.py
T

80 lines
2.7 KiB
Python

"""生成虎牙兑换助手扩展图标:橙色渐变圆角底 + 白色礼盒。
128/48 用完整图案;16px 细节会糊,改用去掉蝴蝶结的加粗简化版。
用法: uv run python web/extension/huya-exchange-helper/generate_icons.py
"""
from PIL import Image, ImageDraw
S = 1024 # 超采样画布,最后缩到目标尺寸
RADIUS = 200
TOP = (255, 156, 71) # 渐变顶部
BOTTOM = (240, 84, 0) # 渐变底部
BOX = (255, 255, 255) # 礼盒
RIBBON = (234, 74, 8) # 丝带(比背景深,压在白色上可见)
def gradient_rounded(size: int, radius: int) -> Image.Image:
grad = Image.new("RGBA", (size, size))
g = ImageDraw.Draw(grad)
for y in range(size):
t = y / (size - 1)
color = tuple(round(TOP[i] + (BOTTOM[i] - TOP[i]) * t) for i in range(3)) + (
255,
)
g.line([(0, y), (size, y)], fill=color)
mask = Image.new("L", (size, size), 0)
ImageDraw.Draw(mask).rounded_rectangle([0, 0, size, size], radius=radius, fill=255)
out = Image.new("RGBA", (size, size), (0, 0, 0, 0))
out.paste(grad, (0, 0), mask)
return out
def draw_gift(img: Image.Image) -> None:
d = ImageDraw.Draw(img)
cx = S // 2
# 蝴蝶结:左右两个椭圆
d.ellipse([348, 235, 512, 365], fill=BOX)
d.ellipse([512, 235, 676, 365], fill=BOX)
# 盒盖(比盒身宽)
d.rounded_rectangle([cx - 235, 375, cx + 235, 485], radius=30, fill=BOX)
# 盒身
d.rounded_rectangle([cx - 200, 509, cx + 200, 789], radius=36, fill=BOX)
# 竖向丝带贯穿盖与盒身
d.rectangle([cx - 44, 375, cx + 44, 789], fill=RIBBON)
# 结口圆点,压在蝴蝶结与盒盖交界
d.ellipse([cx - 46, 330, cx + 46, 422], fill=RIBBON)
def draw_gift_simple(img: Image.Image) -> None:
"""小尺寸版:去掉蝴蝶结,盒体加粗。"""
d = ImageDraw.Draw(img)
cx = img.width // 2
scale = img.width / 512
def rect(x1, y1, x2, y2, radius):
d.rounded_rectangle(
[x1 * scale, y1 * scale, x2 * scale, y2 * scale],
radius=radius * scale,
fill=BOX,
)
rect(cx / scale - 144, 96, cx / scale + 144, 206, 24) # 盖
rect(cx / scale - 112, 230, cx / scale + 112, 420, 28) # 盒身
d.rectangle(
[cx - 34 * scale, 96 * scale, cx + 34 * scale, 420 * scale], fill=RIBBON
)
full = gradient_rounded(S, RADIUS)
draw_gift(full)
base = "web/extension/huya-exchange-helper/"
for size in (128, 48):
full.resize((size, size), Image.LANCZOS).save(f"{base}icon{size}.png")
simple = gradient_rounded(512, 100)
draw_gift_simple(simple)
simple.resize((16, 16), Image.LANCZOS).save(f"{base}icon16.png")
print("icons regenerated")