45 lines
2.9 KiB
Python
45 lines
2.9 KiB
Python
"""#157 owner 2026-11-27: High Elf skin 10 (grey-green) SD faces from Octo are pale and do not match the body.
|
|
Repaint each pale face onto the skin-10 tone: per-channel mean/std transfer in RGB from the matching good skin-10 face
|
|
(same sex, same target: lower 4 / upper 5), opaque pixels only; writes the same palettised BLP2 format as the source."""
|
|
import io, json, struct, sys
|
|
import numpy as np
|
|
from PIL import Image, ImageCms
|
|
from pathlib import Path
|
|
H = Path(__file__).resolve().parent; sys.path.insert(0, str(H)); import getdb2
|
|
srgb = ImageCms.createProfile('sRGB'); lab = ImageCms.createProfile('LAB')
|
|
to_lab = ImageCms.buildTransformFromOpenProfiles(srgb, lab, 'RGB', 'LAB'); to_rgb = ImageCms.buildTransformFromOpenProfiles(lab, srgb, 'LAB', 'RGB')
|
|
# bad FDID -> reference FDID (female ref = face 0 skin 10, male ref = face 1628 skin 10)
|
|
JOBS = {**{f: (230362 if f % 2 == 0 else 230471) for f in range(8840118, 8840132)},
|
|
**{f: (230589 if f % 2 == 0 else 230698) for f in range(8840102, 8840116)}}
|
|
def rgba(fd): return Image.open(io.BytesIO(getdb2.get(fd))).convert('RGBA')
|
|
def stats(img):
|
|
a = np.asarray(img.convert('RGB')).astype(float)
|
|
m = np.asarray(img)[..., 3] > 0
|
|
return a, m, a[m].mean(0), a[m].std(0) + 1e-6
|
|
def write_pal_blp(img, path):
|
|
rgb = img.convert('RGB'); q = rgb.quantize(256, method=Image.Quantize.MEDIANCUT); pal = q.getpalette()[:768]
|
|
pal += [0] * (768 - len(pal)); levels = []; w, h = img.size; cur = rgb
|
|
while True:
|
|
lv = cur.resize((w, h), Image.LANCZOS).quantize(palette=q, dither=Image.Dither.NONE); levels.append(bytes(lv.tobytes()))
|
|
if (w == 1 and h == 1) or len(levels) == 16: break
|
|
w, h = max(1, w // 2), max(1, h // 2)
|
|
offs, sizes, at = [], [], 148 + 1024
|
|
for l in levels: offs.append(at); sizes.append(len(l)); at += len(l)
|
|
offs += [0] * (16 - len(offs)); sizes += [0] * (16 - len(sizes))
|
|
palb = b''.join(bytes([pal[i*3+2], pal[i*3+1], pal[i*3], 0]) for i in range(256))
|
|
hdr = b'BLP2' + struct.pack('<I', 1) + bytes([1, 0, 8, 1]) + struct.pack('<II', *img.size) + struct.pack('<16I', *offs) + struct.pack('<16I', *sizes)
|
|
path.write_bytes(hdr + palb + b''.join(levels))
|
|
out = {}
|
|
for bad, ref in JOBS.items():
|
|
X = rgba(bad); R = rgba(ref)
|
|
xa, xm, xmu, xsd = stats(X); _, _, rmu, rsd = stats(R)
|
|
y = (xa - xmu) / xsd * rsd + rmu
|
|
y = np.clip(y, 0, 255).astype('uint8')
|
|
rgb = Image.fromarray(y, 'RGB')
|
|
p = H / 'tex' / f'{bad}.blp'; write_pal_blp(rgb, p)
|
|
back = Image.open(p).convert('RGB'); m = np.asarray(back).reshape(-1, 3).mean(0)
|
|
out[bad] = dict(ref=ref, mean_after=[round(v) for v in m], file=str(p))
|
|
strip = Image.new('RGB', (X.width * 3, X.height)); strip.paste(X.convert('RGB'), (0, 0)); strip.paste(back, (X.width, 0)); strip.paste(R.convert('RGB').resize(X.size), (X.width * 2, 0))
|
|
strip.save(H / 'preview' / f'{bad}.png')
|
|
(H / 'recolor.json').write_text(json.dumps(out, indent=1) + '\n'); print(json.dumps(out)[:600])
|