Files
d2r-mobile/dm/ios/tools/gen_trove_art.py
T
2026-10-12 16:51:08 +08:00

104 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# 珍宝匣(储藏箱里宝石 / 符文 / 材料三页合成的一页):格位几何 + 底图。
# 底图不重画:角色页那块连续石面整块校色到高级仓库石面,石槽 / 合成格台座 / 石板横带 / 底带从原版三张高级仓库底图逐像素搬(石槽缩到 SCALE),外框是原版宝石页的边框。
# 几何(slots)给布局用(wide_stash.py):物品码 → 槽控件在底图里的左上角与大小(像素,1 像素 = 2 布局单位)。原版按物品码认格位,换位置不换码。
# 底图由 gen_widestash_art.py 调 build() 生成,顶替原版宝石页那张(构建产物,不进仓库)。
# Trove (the gems / runes / materials pages of the stash merged into one page): slot geometry + background.
# The background is not redrawn: the continuous stone face of the character page is color-matched as a whole to the advanced stash stone face; stone slots / crafting slot pedestal / stone slab band / bottom band are copied pixel by pixel from the three original advanced stash backgrounds (stone slots scaled to SCALE); the outer frame is the original gems page border.
# The geometry (slots) is for the layout (wide_stash.py): item code → top-left and size of the slot widget in the background (pixels, 1 pixel = 2 layout units). The game identifies slots by item code; positions change, codes do not.
# The background is generated by gen_widestash_art.py calling build() and replaces the original gems page image (a build product, not in the repo).
import numpy as np
from PIL import Image, ImageDraw
X0, X1 = 58, 594 # 原版高级仓库底图(652 宽)有内容的列范围,两侧是透明边 | column range with content in the original advanced stash background (652 wide); both sides are transparent margins
W, H = 732, 649 # 珍宝匣底图:原内容 536 宽 + 196(与加宽后的个人页同宽,切页时外框不跳) | Trove background: original content width 536 + 196 (same width as the widened personal tab, so the frame does not jump when switching tabs)
EXW = W - (X1 - X0); HALF = EXW // 2
FR, FRR = 20, 26 # 外框厚:左 20、右 26(右框内沿多一道暗线) | outer frame thickness: left 20, right 26 (the right frame has an extra dark line on its inner edge)
SCALE = 0.85 # 石槽与槽控件的缩放(原版槽芯 45 像素、槽控件 49 像素) | scale of stone slots and slot widgets (original slot core 45 px, slot widget 49 px)
TOP = dict(dx=43, dy=43, y=28, gap=18) # 上排:宝石 7 列 + 石面间隔 + 符文 7 列,各 5 行 | top row: 7 gem columns + stone face gap + 7 rune columns, 5 rows each
SHIFT = 36 # 合成格台座与底带中台比居中偏左多少:右边要放下材料的 5 列 | how far left of center the crafting slot pedestal and the bottom band's middle block sit: the right side must fit the 5 materials columns
CONS = dict(dx=44, y=278) # 材料槽格距与第一行(双高格)上沿 | materials slot pitch and top edge of the first row (double-height cells)
YM, YS, YB = 283, 462, 507 # 宝石页格子区下沿 / 材料页石板横带上沿 / 底带上沿 | bottom edge of the gems page grid area / top edge of the materials page stone slab band / top edge of the bottom band
GEM_COLS = 'w g r y v b'.split(); GEM_ROWS = 'gc gf gs gl gp'.split() # 列 = 颜色,行 = 品级;下面两个例外码 | column = color, row = grade; two exception codes below
GEM_ODD = {'glv': 'gzv'}; SKULL = 'skc skf sku skl skz'.split()
MAT_L = ['pk1', 'pk2', 'pk3', 'dhn', 'bey', 'mbr', 'rvs', 'rvl'] # 左 3 列:双高 3 / 3 / 2 | left 3 columns: double-height 3 / 3 / 2
MAT_R = ['ua1', 'ua2', 'ua3', 'ua4', 'ua5', 'xa1', 'xa2', 'xa3', 'xa4', 'xa5', 'toa', 'tes', 'ceh', 'bet', 'fed'] # 右 5 列:双高 5 / 5 / 5 | right 5 columns: double-height 5 / 5 / 5
PED_X = 205 - X0 + HALF - SHIFT # 台座左沿(原版材料页台座 x 205–439) | left edge of the pedestal (original materials page pedestal x 205–439)
def geometry():
"""返回 {物品码: (槽心 x, 槽心 y, 占几格高)},坐标是珍宝匣底图里的像素。 | Returns {item code: (slot center x, slot center y, height in cells)}, coordinates in pixels within the Trove background."""
dm = 45 * SCALE; tw = 13 * TOP['dx'] + dm + TOP['gap']; x0 = FR + (W - FR - FRR - tw) / 2; g = {} # 在左右框之间居中(右框比左框厚 6) | centered between the left and right frames (the right frame is 6 thicker than the left)
for r in range(5):
for k in range(14):
cx, cy = x0 + k * TOP['dx'] + dm / 2 + (TOP['gap'] if k >= 7 else 0), TOP['y'] + r * TOP['dy'] + dm / 2
if k < 6: c = GEM_ROWS[r] + GEM_COLS[k]; g[GEM_ODD.get(c, c)] = (cx, cy, 1)
elif k == 6: g[SKULL[r]] = (cx, cy, 1)
elif r < 4: g['r%02d' % (r * 7 + k - 6)] = (cx, cy, 1)
elif k - 7 < 5: g['r%02d' % (29 + k - 7)] = (cx + TOP['dx'], cy, 1) # 符文 33 个:前 4 行各 7,末行 5 个居中 | 33 runes: 7 in each of the first 4 rows, 5 centered in the last row
def group(codes, counts, gx):
i = 0
for row, n in enumerate(counts):
for k in range(n):
cx = gx + k * CONS['dx'] + dm / 2
cy = CONS['y'] + 95 * SCALE / 2 if row == 0 else CONS['y'] + 95 * SCALE + 6 + (row - 1) * CONS['dx'] + dm / 2
g[codes[i]] = (cx, cy, 2 if row == 0 else 1); i += 1
pl, pr = PED_X, PED_X + 234
group(MAT_L, (3, 3, 2), FR + (pl - FR - (2 * CONS['dx'] + dm)) / 2); group(MAT_R, (5, 5, 5), pr + (W - FRR - pr - (4 * CONS['dx'] + dm)) / 2)
return g
def slots():
"""布局用:{物品码: (x, y, 宽, 高)},槽控件矩形,单位 = 布局单位,原点 = 原版 652 宽底图的左上角(原版槽控件 98 × 98 / 98 × 196)。 | For the layout: {item code: (x, y, width, height)}, slot widget rects, in layout units, origin = top-left of the original 652-wide background (original slot widgets are 98 × 98 / 98 × 196)."""
out = {}
for code, (cx, cy, hgt) in geometry().items():
w = round(98 * SCALE); h = w if hgt == 1 else round(196 * SCALE); wp, hp = 49 * SCALE, 49 * SCALE * hgt
out[code] = (round((cx - wp / 2 - (HALF - X0)) * 2), round((cy - hp / 2) * 2) + 102, w, h) # 珍宝匣底图贴在加宽画布的 x = X0,原版 652 图贴在 x = HALF:横坐标差 HALF − X0 | the Trove background is pasted at x = X0 on the widened canvas, the original 652 image at x = HALF: x offset HALF − X0
return out
def _stats(im, boxes):
a = np.concatenate([np.asarray(im.crop(b).convert('RGB'), dtype=np.float64).reshape(-1, 3) for b in boxes]); return a.mean(0), a.std(0)
def _stone(csh, w, h, target, ox=90, oy=190):
src = np.asarray(csh.crop((ox, oy, ox + w, oy + h)).convert('RGB'), dtype=np.float64); m, s = src.reshape(-1, 3).mean(0), src.reshape(-1, 3).std(0)
return Image.fromarray(np.clip((src - m) / s * target[1] + target[0], 0, 255).astype(np.uint8)).convert('RGBA')
def _feather(sz, n=2):
w, h = sz; a = np.ones((h, w))
for i in range(n): v = (i + 1) / (n + 1); a[i, :] = np.minimum(a[i, :], v); a[h - 1 - i, :] = np.minimum(a[h - 1 - i, :], v); a[:, i] = np.minimum(a[:, i], v); a[:, w - 1 - i] = np.minimum(a[:, w - 1 - i], v)
return Image.fromarray((a * 255).astype(np.uint8))
def _slot(src, x, y, w=45, h=45, pad=4): # 槽芯外 2 像素是边框线,再外 2 像素羽化进石面(边框线不参与羽化) | 2 px outside the slot core is the border line, 2 more px outside feather into the stone face (the border line is not feathered)
t = src.crop((x - pad, y - pad, x + w + pad, y + h + pad)); return t.resize((round(t.width * SCALE), round(t.height * SCALE)), Image.LANCZOS)
def _paste(dst, t, cx, cy): dst.paste(t, (round(cx - t.width / 2), round(cy - t.height / 2)), _feather(t.size))
def _mirror_fill(dst, strip, x, y, w):
k = 0; px = x
while px < x + w:
p = strip if k % 2 == 0 else strip.transpose(Image.FLIP_LEFT_RIGHT); p = p.crop((0, 0, min(p.width, x + w - px), p.height)); dst.paste(p, (px, y)); px += p.width; k += 1
def build(gems, runes, cons, csh):
"""四张原版图(宝石页、符文页、材料页底图,角色页底图;RGBA)→ 珍宝匣底图(W × H,RGBA)。 | Four original images (gems page, runes page, materials page backgrounds, character page background; RGBA) → Trove background (W × H, RGBA)."""
assert gems.size == runes.size == cons.size == (652, 649) and csh.size == (1369, 706), '原版底图尺寸不是原样(原版资源变了?)'
out = Image.new('RGBA', (W, H)); g = gems.crop((X0, 0, X1, H)); c = cons.crop((X0, 0, X1, H)); gw = g.width
tgt = _stats(gems, [(165, 31, 178, 276), (226, 31, 239, 276), (287, 31, 300, 276)]) # 目标石面:宝石页槽与槽之间的三条竖缝 | target stone face: the three vertical gaps between slots on the gems page
out.paste(g.crop((0, 0, FR, H)), (0, 0)); out.paste(g.crop((gw - FRR, 0, gw, H)), (W - FRR, 0))
st = _stone(csh, W - 2 * FR + 6, H, tgt); out.paste(st.crop((0, 0, st.width, YS - 14)), (FR - 3, 14))
out.paste(g.crop((0, 0, FR - 3, YS)), (0, 0)); out.paste(g.crop((gw - FRR + 3, 0, gw, YS)), (W - FRR + 3, 0)); _mirror_fill(out, g.crop((FR, 0, gw - FRR, 15)), FR, 0, W - FR - FRR)
# 下半照原版材料页:石板横带、底带各自「左块贴左框、右块贴右框、中块放在居中偏左 SHIFT」,中块两侧空出来的按各段材质补
# The lower half follows the original materials page: for the stone slab band and the bottom band each, "left block against the left frame, right block against the right frame, middle block SHIFT left of center"; the gaps beside the middle block are filled with each segment's own texture
def widen(y0, y1, xl, xr, mode):
band = c.crop((0, y0, gw, y1)); h = y1 - y0; nx = xl + HALF - SHIFT; left, mid, right = band.crop((0, 0, xl, h)), band.crop((xl, 0, xr, h)), band.crop((xr, 0, gw, h))
def fill(x, w, edge, flip):
if w <= 0: return
if mode == 'tile': # 细密刻纹的石板:取紧邻的一段原样接着铺(不镜像) | finely engraved stone slab: continue with an adjacent segment as is (not mirrored)
src = edge.crop((edge.width - 70, 0, edge.width - 6, h)) if not flip else edge.crop((6, 0, 70, h)); blk = Image.new('RGBA', (w, h)); px = 0
while px < w: blk.paste(src, (px if not flip else w - px - src.width, 0)); px += src.width
else: # 暗石面:角色页石面校到底带最下面那条暗石(横贯全宽、没有花饰)的均值与起伏;金线那几行横向接过去 | dark stone face: the character page stone face matched to the mean and variation of the bottom band's lowest dark stone strip (full width, no ornament); the gold-line rows are continued across
tg = _stats(band, [(30, h - 16, gw - 30, h - 4)]); blk = _stone(csh, w, h, tg, ox=300 + x, oy=420); col = np.asarray(edge.convert('RGB'))[:, -3 if not flip else 2].astype(int); d = ImageDraw.Draw(blk)
for yy in range(h):
r_, g_, b_ = col[yy]
if r_ - b_ > 22 and r_ > 60: d.line([(0, yy), (w, yy)], fill=(int(r_), int(g_), int(b_), 255))
out.paste(blk, (x, y0))
fill(xl, nx - xl, left, False); fill(nx + (xr - xl), W - (gw - xr) - nx - (xr - xl), right, True)
out.paste(left, (0, y0)); out.paste(mid, (nx, y0)); out.paste(right, (W - (gw - xr), y0))
ped = cons.crop((205, 264, 439, YS + 3)); out.paste(ped, (PED_X, 264), _feather(ped.size, 1)) # 台座先贴,石板横带后贴盖住它的下沿(接缝落在横带自己的上边线上) | paste the pedestal first, then the stone slab band over its lower edge (the seam falls on the band's own top line)
widen(YS, YB, 205 - X0, 439 - X0, 'tile'); widen(YB, H, 226 - X0, 421 - X0, 'dark')
gs, rs, tall, one = _slot(gems, 117, 31), _slot(runes, 93, 51), _slot(cons, 87, 49, 45, 95), _slot(cons, 87, 154)
for code, (cx, cy, hgt) in geometry().items():
_paste(out, tall if hgt == 2 else gs if code[0] in 'gs' else rs if code[0] == 'r' and code[1:].isdigit() else one, cx, cy)
return out