244 lines
8.4 KiB
Python
244 lines
8.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""运营向微信道具图标:200×200 PNG,深色精品档,价签清晰。"""
|
||
from __future__ import annotations
|
||
|
||
import math
|
||
from pathlib import Path
|
||
|
||
from PIL import Image, ImageDraw, ImageFilter, ImageFont
|
||
|
||
OUT = Path(__file__).resolve().parents[2] / "admin" / "public" / "props"
|
||
SIZE = 512 # 先画高清再缩到 200
|
||
FINAL = 200
|
||
|
||
|
||
def font(size: int, bold: bool = True) -> ImageFont.FreeTypeFont:
|
||
path = r"C:\Windows\Fonts\msyhbd.ttc" if bold else r"C:\Windows\Fonts\msyh.ttc"
|
||
try:
|
||
return ImageFont.truetype(path, size, index=0)
|
||
except OSError:
|
||
return ImageFont.truetype(r"C:\Windows\Fonts\arialbd.ttf", size)
|
||
|
||
|
||
def round_rect(draw: ImageDraw.ImageDraw, box, radius: int, fill) -> None:
|
||
draw.rounded_rectangle(box, radius=radius, fill=fill)
|
||
|
||
|
||
def radial_glow(base: Image.Image, color: tuple[int, int, int], cx: int, cy: int, r: int, alpha: int = 120) -> None:
|
||
overlay = Image.new("RGBA", base.size, (0, 0, 0, 0))
|
||
px = overlay.load()
|
||
w, h = base.size
|
||
for y in range(h):
|
||
for x in range(w):
|
||
d = math.hypot(x - cx, y - cy) / r
|
||
if d >= 1:
|
||
continue
|
||
a = int(alpha * (1 - d) ** 2)
|
||
px[x, y] = (*color, a)
|
||
base.alpha_composite(overlay)
|
||
|
||
|
||
def draw_open_book(draw: ImageDraw.ImageDraw, cx: int, cy: int, scale: float, cover, page, glow) -> None:
|
||
w = int(150 * scale)
|
||
h = int(110 * scale)
|
||
# glow blob
|
||
draw.ellipse([cx - w // 2 - 20, cy - h // 2 - 10, cx + w // 2 + 20, cy + h // 2 + 30], fill=glow)
|
||
# left cover
|
||
draw.polygon(
|
||
[
|
||
(cx - w // 2, cy - h // 2 + 8),
|
||
(cx - 6, cy - h // 2),
|
||
(cx - 6, cy + h // 2),
|
||
(cx - w // 2, cy + h // 2 - 8),
|
||
],
|
||
fill=cover,
|
||
)
|
||
# right cover
|
||
draw.polygon(
|
||
[
|
||
(cx + 6, cy - h // 2),
|
||
(cx + w // 2, cy - h // 2 + 8),
|
||
(cx + w // 2, cy + h // 2 - 8),
|
||
(cx + 6, cy + h // 2),
|
||
],
|
||
fill=cover,
|
||
)
|
||
# pages
|
||
draw.polygon(
|
||
[
|
||
(cx - w // 2 + 10, cy - h // 2 + 14),
|
||
(cx - 8, cy - h // 2 + 6),
|
||
(cx - 8, cy + h // 2 - 10),
|
||
(cx - w // 2 + 10, cy + h // 2 - 16),
|
||
],
|
||
fill=page,
|
||
)
|
||
draw.polygon(
|
||
[
|
||
(cx + 8, cy - h // 2 + 6),
|
||
(cx + w // 2 - 10, cy - h // 2 + 14),
|
||
(cx + w // 2 - 10, cy + h // 2 - 16),
|
||
(cx + 8, cy + h // 2 - 10),
|
||
],
|
||
fill=page,
|
||
)
|
||
# spine light
|
||
draw.line([(cx, cy - h // 2 + 4), (cx, cy + h // 2 - 4)], fill=(255, 255, 255, 90), width=3)
|
||
|
||
|
||
def price_badge(img: Image.Image, text: str, accent: tuple[int, int, int]) -> None:
|
||
draw = ImageDraw.Draw(img)
|
||
f = font(56, bold=True)
|
||
tw, th = draw.textbbox((0, 0), text, font=f)[2:]
|
||
bw, bh = tw + 72, th + 28
|
||
bx = (SIZE - bw) // 2
|
||
by = SIZE - 118
|
||
# soft shadow
|
||
shadow = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
||
sd = ImageDraw.Draw(shadow)
|
||
round_rect(sd, [bx + 4, by + 6, bx + bw + 4, by + bh + 6], 28, (0, 0, 0, 110))
|
||
img.alpha_composite(shadow.filter(ImageFilter.GaussianBlur(6)))
|
||
draw = ImageDraw.Draw(img)
|
||
# plaque
|
||
round_rect(draw, [bx, by, bx + bw, by + bh], 28, (245, 236, 214, 255))
|
||
# accent tips
|
||
draw.ellipse([bx - 6, by + bh // 2 - 8, bx + 10, by + bh // 2 + 8], fill=accent)
|
||
draw.ellipse([bx + bw - 10, by + bh // 2 - 8, bx + bw + 6, by + bh // 2 + 8], fill=accent)
|
||
draw.text((bx + (bw - tw) // 2, by + (bh - th) // 2 - 4), text, font=f, fill=(28, 24, 20, 255))
|
||
|
||
|
||
def frame(img: Image.Image, color: tuple[int, int, int, int], width: int = 10) -> None:
|
||
draw = ImageDraw.Draw(img)
|
||
draw.rounded_rectangle([6, 6, SIZE - 7, SIZE - 7], radius=96, outline=color, width=width)
|
||
|
||
|
||
def make_icon(
|
||
filename: str,
|
||
price: str,
|
||
bg_top: tuple[int, int, int],
|
||
bg_bot: tuple[int, int, int],
|
||
glow: tuple[int, int, int],
|
||
cover: tuple[int, int, int],
|
||
accent: tuple[int, int, int],
|
||
motif: str = "book",
|
||
) -> Path:
|
||
img = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
|
||
# rounded background gradient
|
||
canvas = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
|
||
for y in range(SIZE):
|
||
t = y / (SIZE - 1)
|
||
r = int(bg_top[0] * (1 - t) + bg_bot[0] * t)
|
||
g = int(bg_top[1] * (1 - t) + bg_bot[1] * t)
|
||
b = int(bg_top[2] * (1 - t) + bg_bot[2] * t)
|
||
ImageDraw.Draw(canvas).line([(0, y), (SIZE, y)], fill=(r, g, b, 255))
|
||
mask = Image.new("L", (SIZE, SIZE), 0)
|
||
ImageDraw.Draw(mask).rounded_rectangle([0, 0, SIZE - 1, SIZE - 1], radius=108, fill=255)
|
||
bg = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
|
||
bg.paste(canvas, mask=mask)
|
||
img.alpha_composite(bg)
|
||
|
||
radial_glow(img, glow, SIZE // 2, SIZE // 2 - 40, 220, 140)
|
||
draw = ImageDraw.Draw(img)
|
||
|
||
if motif == "book":
|
||
draw_open_book(draw, SIZE // 2, SIZE // 2 - 36, 1.35, (*cover, 255), (248, 244, 235, 255), (*glow, 55))
|
||
# tiny sparkles
|
||
for sx, sy in [(120, 140), (390, 160), (150, 300), (370, 290)]:
|
||
draw.ellipse([sx, sy, sx + 6, sy + 6], fill=(255, 255, 255, 160))
|
||
elif motif == "note":
|
||
# music note / short video
|
||
cx, cy = SIZE // 2, SIZE // 2 - 40
|
||
draw.ellipse([cx - 70, cy - 20, cx + 10, cy + 50], fill=(20, 20, 24, 255), outline=(255, 255, 255, 230), width=8)
|
||
draw.rectangle([cx + 2, cy - 110, cx + 18, cy + 20], fill=(255, 255, 255, 230))
|
||
draw.polygon([(cx + 18, cy - 110), (cx + 70, cy - 90), (cx + 18, cy - 70)], fill=(255, 80, 120, 255))
|
||
radial_glow(img, (255, 80, 140), cx, cy, 160, 90)
|
||
elif motif == "play":
|
||
cx, cy = SIZE // 2, SIZE // 2 - 40
|
||
draw.rounded_rectangle([cx - 90, cy - 60, cx + 90, cy + 60], radius=28, outline=(255, 60, 70, 255), width=14)
|
||
draw.polygon([(cx - 28, cy - 40), (cx - 28, cy + 40), (cx + 48, cy)], fill=(255, 60, 70, 255))
|
||
radial_glow(img, (255, 60, 70), cx, cy, 150, 100)
|
||
elif motif == "crown":
|
||
draw_open_book(draw, SIZE // 2, SIZE // 2 - 10, 1.2, (*cover, 255), (252, 248, 235, 255), (*glow, 50))
|
||
# crown
|
||
cx, cy = SIZE // 2, 150
|
||
pts = [(cx - 70, cy + 30), (cx - 55, cy - 25), (cx - 25, cy + 10), (cx, cy - 40), (cx + 25, cy + 10), (cx + 55, cy - 25), (cx + 70, cy + 30)]
|
||
draw.polygon(pts, fill=(232, 184, 74, 255))
|
||
draw.ellipse([cx - 10, cy - 52, cx + 10, cy - 32], fill=(255, 220, 120, 255))
|
||
radial_glow(img, (255, 200, 80), cx, cy, 120, 110)
|
||
elif motif == "wrench":
|
||
cx, cy = SIZE // 2, SIZE // 2 - 30
|
||
draw.rounded_rectangle([cx - 18, cy - 90, cx + 18, cy + 70], radius=10, fill=(80, 200, 120, 255))
|
||
draw.ellipse([cx - 55, cy - 55, cx + 55, cy + 55], outline=(80, 200, 120, 255), width=18)
|
||
draw.ellipse([cx - 22, cy - 22, cx + 22, cy + 22], fill=(12, 28, 18, 255))
|
||
|
||
price_badge(img, price, accent)
|
||
frame(img, (*accent, 200), 8)
|
||
|
||
out = img.resize((FINAL, FINAL), Image.Resampling.LANCZOS)
|
||
OUT.mkdir(parents=True, exist_ok=True)
|
||
path = OUT / filename
|
||
out.save(path, "PNG", optimize=True)
|
||
print("wrote", path, path.stat().st_size)
|
||
return path
|
||
|
||
|
||
def main() -> None:
|
||
# 入门 ¥29 — 青绿漫游
|
||
make_icon(
|
||
"nomad_lite.png",
|
||
"29",
|
||
(8, 28, 36),
|
||
(18, 70, 72),
|
||
(255, 170, 80),
|
||
(20, 90, 88),
|
||
(245, 158, 11),
|
||
"book",
|
||
)
|
||
# 进阶 ¥298 — 紫金
|
||
make_icon(
|
||
"nomad_plus.png",
|
||
"298",
|
||
(18, 8, 36),
|
||
(58, 28, 92),
|
||
(180, 120, 255),
|
||
(40, 20, 70),
|
||
(232, 184, 74),
|
||
"book",
|
||
)
|
||
# 精英 ¥588 — 深海蓝金
|
||
make_icon(
|
||
"nomad_pro.png",
|
||
"588",
|
||
(6, 14, 36),
|
||
(20, 48, 90),
|
||
(255, 180, 70),
|
||
(16, 40, 78),
|
||
(245, 158, 11),
|
||
"book",
|
||
)
|
||
# 联调占位 ¥1 — 绿,明确非卖品观感
|
||
make_icon(
|
||
"pay_test.png",
|
||
"1",
|
||
(10, 28, 18),
|
||
(20, 60, 36),
|
||
(80, 220, 140),
|
||
(30, 90, 50),
|
||
(80, 200, 120),
|
||
"wrench",
|
||
)
|
||
# 兼容旧文件名(已上传过丑图的 id)也覆盖一份精品图,方便微信侧改图
|
||
for src, dst in [
|
||
("nomad_lite.png", "29.png"),
|
||
("nomad_plus.png", "298.png"),
|
||
("nomad_pro.png", "588.png"),
|
||
("pay_test.png", "test.png"),
|
||
]:
|
||
data = (OUT / src).read_bytes()
|
||
(OUT / dst).write_bytes(data)
|
||
print("mirror", dst)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|