46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
"""把抓取的纯模型截图自动裁切到模型边界,统一为 4:3 浅底画幅,输出到 src/assets/home/。"""
|
|
from PIL import Image
|
|
|
|
IDS = ['A7', 'O6', 'O6_new', 'O6_fingers']
|
|
OUT_W, OUT_H = 1000, 760 # 4:3 略宽,贴合卡片图区
|
|
MARGIN = 0.10 # 模型四周留白比例
|
|
THRESH = 70 # 与背景色差阈值(需高于背景渐变幅度)
|
|
|
|
for pid in IDS:
|
|
im = Image.open(f'scripts/out/thumb-{pid}.png').convert('RGB')
|
|
W, H = im.size
|
|
px = im.load()
|
|
bg = px[4, 4] # 角落取背景色
|
|
|
|
def diff(c):
|
|
return abs(c[0]-bg[0]) + abs(c[1]-bg[1]) + abs(c[2]-bg[2])
|
|
|
|
# 求模型包围盒(隔点采样加速)
|
|
x0, y0, x1, y1 = W, H, 0, 0
|
|
step = 2
|
|
for y in range(0, H, step):
|
|
for x in range(0, W, step):
|
|
if diff(px[x, y]) > THRESH:
|
|
if x < x0: x0 = x
|
|
if x > x1: x1 = x
|
|
if y < y0: y0 = y
|
|
if y > y1: y1 = y
|
|
if x1 <= x0 or y1 <= y0:
|
|
x0, y0, x1, y1 = 0, 0, W, H
|
|
|
|
mw, mh = x1-x0, y1-y0
|
|
mx, my = int(mw*MARGIN), int(mh*MARGIN)
|
|
x0 = max(0, x0-mx); y0 = max(0, y0-my)
|
|
x1 = min(W, x1+mx); y1 = min(H, y1+my)
|
|
crop = im.crop((x0, y0, x1, y1))
|
|
|
|
# 等比缩放后居中贴到统一浅底画幅
|
|
cw, ch = crop.size
|
|
scale = min(OUT_W/cw, OUT_H/ch)
|
|
nw, nh = int(cw*scale), int(ch*scale)
|
|
crop = crop.resize((nw, nh), Image.LANCZOS)
|
|
canvas = Image.new('RGB', (OUT_W, OUT_H), bg)
|
|
canvas.paste(crop, ((OUT_W-nw)//2, (OUT_H-nh)//2))
|
|
canvas.save(f'src/assets/home/{pid}.png', optimize=True)
|
|
print(f'{pid}: bbox=({x0},{y0},{x1},{y1}) bg={bg} -> src/assets/home/{pid}.png')
|