knob_os/tools/gen_icons.py

229 lines
7.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
# gen_icons.py —— 生成 nex_buddy 的线性图标位图(LVGL TRUE_COLOR_ALPHA)
#
# 为什么不用 LVGL 自带的 FontAwesome 符号:
# 1. 那套符号在 24px 下笔画偏粗、风格偏"系统",凑不齐统一视觉;
# 2. 时钟/天气这类图标字体里根本没有。
# 这里用 PIL 在 8 倍画布上抗锯齿绘制,再缩到 56px,得到一个统一的线性图标族。
# 图标统一为纯白 + alpha,运行时用 img_recolor 染色,所以一套图能适配任意强调色。
import os
from PIL import Image, ImageDraw
SIZE = 56 # 输出尺寸(屏上 1:1 显示,改大改小都要重跑本脚本)
# 图标绘制坐标按 56×56 网格设计,SIZE<56 会把右下边缘裁掉
SS = 8 # 超采样倍数
STROKE = 2.1 # 基准线宽(输出像素)
OUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'src', 'assets')
def new_canvas():
img = Image.new('L', (SIZE * SS, SIZE * SS), 0)
return img, ImageDraw.Draw(img)
def line(d, pts, w=STROKE):
d.line([(x * SS, y * SS) for x, y in pts], fill=255, width=int(w * SS), joint='curve')
def circle(d, cx, cy, r, w=STROKE):
d.ellipse([(cx - r) * SS, (cy - r) * SS, (cx + r) * SS, (cy + r) * SS],
outline=255, width=int(w * SS))
def disc(d, cx, cy, r):
d.ellipse([(cx - r) * SS, (cy - r) * SS, (cx + r) * SS, (cy + r) * SS], fill=255)
def rrect(d, x0, y0, x1, y1, r, w=STROKE):
d.rounded_rectangle([x0 * SS, y0 * SS, x1 * SS, y1 * SS],
radius=r * SS, outline=255, width=int(w * SS))
def arc(d, cx, cy, r, a0, a1, w=STROKE):
d.arc([(cx - r) * SS, (cy - r) * SS, (cx + r) * SS, (cy + r) * SS],
a0, a1, fill=255, width=int(w * SS))
# ---------------------------------------------------------------- 图标定义
def icon_clock(img, d):
"""表盘 + 时针分针 —— 中心留一个小点,像真表。"""
circle(d, 28, 28, 20.5)
line(d, [(28, 28), (28, 15.5)], 2.5) # 时针
line(d, [(28, 28), (37, 33)], 2.5) # 分针
disc(d, 28, 28, 1.9)
def icon_gallery(img, d):
"""相框 + 太阳 + 远山。"""
rrect(d, 7.5, 12, 48.5, 44, 6.5)
disc(d, 19, 22.5, 3.1)
line(d, [(10.5, 39.5), (21.5, 28.5), (29.5, 36), (36.5, 29), (45.5, 37.5)], 2.4)
def icon_music(img, d):
"""单音符:符头 + 符干 + 符尾。"""
disc(d, 21.5, 39, 6.2)
line(d, [(27.3, 39), (27.3, 14.5)], 2.5)
line(d, [(27.3, 14.5), (39.5, 18.5), (39.5, 26.5)], 2.5)
def icon_astro(img, d):
"""四芒星 + 弯月 + 点星:一眼是"占星"。"""
import math
# 主星:四芒(菱形轮廓,收腰让它更像星光而不是方块)
star = [(28, 6), (33, 23), (50, 28), (33, 33), (28, 50), (23, 33), (6, 28), (23, 23)]
line(d, star + [star[0]], 2.3)
# 右上弯月:外弧 + 内弧拼出月牙
d.arc([(36) * SS, (8) * SS, (52) * SS, (24) * SS], 300, 120, fill=255, width=int(2.2 * SS))
d.arc([(40) * SS, (12) * SS, (52) * SS, (24) * SS], 270, 150, fill=255, width=int(2.2 * SS))
# 左下两颗小星点缀
disc(d, 13, 41, 1.7)
disc(d, 45, 45, 1.5)
def icon_weather(img, d):
"""云转晴:左上太阳从云后探出。
云的轮廓用“剪影 - 腐蚀”自动求边:三个圆 + 底矩形取并集后一次性钩出
外轮廓,交界处不会留出多余的相交线,也不用手调弧线拼接点。
"""
import math
from PIL import ImageChops, ImageFilter
# 云的剪影:左右两个小鼓包 + 顶部大鼓包 + 底部矩形
cloud = Image.new('L', img.size, 0)
cd = ImageDraw.Draw(cloud)
disc(cd, 24.0, 37.2, 6.6)
disc(cd, 32.2, 31.6, 9.8)
disc(cd, 40.6, 37.2, 6.4)
cd.rectangle([17.6 * SS, 37.2 * SS, 47.0 * SS, 43.4 * SS], fill=255)
# 太阳(左上):先画到画布,再被云的剪影遮掉伸进云里的部分
circle(d, 23.5, 20.5, 7.2, 2.4)
for i in range(8):
a = math.radians(i * 45.0)
line(d, [(23.5 + 10.4 * math.cos(a), 20.5 + 10.4 * math.sin(a)),
(23.5 + 14.6 * math.cos(a), 20.5 + 14.6 * math.sin(a))], 2.4)
img.paste(ImageChops.subtract(img, cloud), (0, 0))
# 描边:腐蚀核宽度 (41-1)/2/8 ≈ 2.5px,与基准线宽一致
outline = ImageChops.subtract(cloud, cloud.filter(ImageFilter.MinFilter(41)))
img.paste(ImageChops.lighter(img, outline), (0, 0))
def icon_settings(img, d):
"""齿轮:内圆 + 8 根齿,齿做在圆外,看起来是实心齿而非辐条。"""
import math
for i in range(8):
a = math.radians(i * 45.0 + 22.5)
line(d, [(28 + 15.5 * math.cos(a), 28 + 15.5 * math.sin(a)),
(28 + 22.0 * math.cos(a), 28 + 22.0 * math.sin(a))], 5.4)
circle(d, 28, 28, 16.8, 2.4)
circle(d, 28, 28, 6.6, 2.4)
def icon_chevron_left(img, d):
line(d, [(34, 14), (20, 28), (34, 42)], 2.8)
def icon_user(img, d):
"""人像:圆头 + 半圆肩线 —— 一眼看是"个人资料"。"""
circle(d, 28, 19.5, 9.2, 2.3)
d.arc([(9) * SS, (28) * SS, (47) * SS, (68) * SS], 180, 360,
fill=255, width=int(2.3 * SS))
def icon_check(img, d):
line(d, [(15, 29), (24, 38), (41, 18)], 2.8)
ICONS = [
('clock', icon_clock),
('gallery', icon_gallery),
('music', icon_music),
('weather', icon_weather),
('astro', icon_astro),
('user', icon_user),
('settings', icon_settings),
('back', icon_chevron_left),
('check', icon_check),
]
def emit_c(name, img):
"""输出 LVGL TRUE_COLOR_ALPHA 数组。白色 (0xFFFF) + alpha,运行时 recolor 染色。"""
px = img.load()
out = []
for y in range(SIZE):
for x in range(SIZE):
a = px[x, y]
if a == 0:
out.append('0,0,0')
else:
out.append('255,255,%d' % a)
body = ','.join(out)
# 每行 24 个值换行,避免单行过长
vals = body.split(',')
lines = []
for i in range(0, len(vals), 36):
lines.append(' ' + ','.join(vals[i:i + 36]) + ',')
data = '\n'.join(lines).rstrip(',') + '\n'
return ('static const uint8_t %s_map[] = {\n%s};\n'
'const lv_img_dsc_t %s = {\n'
' .header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA,\n'
' .header.always_zero = 0,\n'
' .header.w = %d,\n'
' .header.h = %d,\n'
' .data_size = %d * %d * 3,\n'
' .data = %s_map,\n'
'};\n' % (name, data, name, SIZE, SIZE, SIZE, SIZE, name))
def main():
os.makedirs(OUT_DIR, exist_ok=True)
src = ['// 由 tools/gen_icons.py 生成,不要手改\n#include "icons.h"\n\n']
for name, fn in ICONS:
img, d = new_canvas()
fn(img, d)
img = img.resize((SIZE, SIZE), Image.LANCZOS)
src.append(emit_c('icon_' + name, img))
with open(os.path.join(OUT_DIR, 'icons.c'), 'w', encoding='utf-8') as f:
f.write('\n'.join(src))
hdr = ['#pragma once',
'// 线性图标资源(LVGL 图片描述符),见 tools/gen_icons.py',
'#include <lvgl.h>',
'',
'#ifdef __cplusplus',
'extern "C" {',
'#endif',
'']
for name, _ in ICONS:
hdr.append('extern const lv_img_dsc_t icon_%s;' % name)
hdr += ['', '#ifdef __cplusplus', '}', '#endif', '']
with open(os.path.join(OUT_DIR, 'icons.h'), 'w', encoding='utf-8') as f:
f.write('\n'.join(hdr) + '\n')
# 附带一张预览图,方便肉眼检查
sheet = Image.new('L', (SIZE * len(ICONS), SIZE), 24)
for i, (name, fn) in enumerate(ICONS):
img, d = new_canvas()
fn(img, d)
img = img.resize((SIZE, SIZE), Image.LANCZOS)
sheet.paste(img, (i * SIZE, 0))
sheet.resize((SIZE * len(ICONS) * 2, SIZE * 2), Image.LANCZOS).save(
os.path.join(OUT_DIR, 'icons_preview.png'))
print('icons: %d generated -> %s' % (len(ICONS), OUT_DIR))
if __name__ == '__main__':
main()