Files
WoW-Hybrid-gateway/ops/gnome_skin_palette.py

65 lines
2.7 KiB
Python

"""Transfer the original Gnome skin palette to an HD body atlas."""
import numpy as np
from PIL import Image
LUMA = np.array((0.2126, 0.7152, 0.0722))
def arm_sample(image, hd):
"""Select the upper arm away from the nails, straps and face."""
h, w = image.shape[:2]
x0, x1, y0, y1 = (.025, .095, .10, .44) if hd else (.06, .42, .14, .46)
return image[int(h*y0):int(h*y1), int(w*x0):int(w*x1), :3].reshape(-1, 3)
def _shade_curve(sample):
brightness = sample @ LUMA
bins = np.percentile(brightness, np.linspace(0, 100, 17))
levels, colors = [], []
for low, high in zip(bins[:-1], bins[1:]):
selected = (brightness >= low) & (brightness <= high)
if selected.any():
level = np.median(brightness[selected])
if not levels or level > levels[-1]:
levels.append(level)
colors.append(np.median(sample[selected], axis=0))
return np.array(levels), np.array(colors)
def correct_body(original, hd):
if original.shape != (256, 256, 4) or hd.shape != (512, 1024, 4):
raise ValueError('Expected original and HD Gnome body UV layouts')
levels, native_colors = _shade_curve(arm_sample(hd, True))
_, source_colors = _shade_curve(arm_sample(original, False))
native_rank = np.linspace(0, 1, len(native_colors))
source_rank = np.linspace(0, 1, len(source_colors))
target = np.column_stack([np.interp(native_rank, source_rank, channel)
for channel in source_colors.T])
bounds = np.r_[0, levels, 255]
offsets = np.vstack([np.zeros(3), target - native_colors, np.zeros(3)])
rgb = hd[:, :, :3].astype(float)
luma = rgb @ LUMA
delta = np.stack([np.interp(luma, bounds, channel) for channel in offsets.T], axis=2)
peak = rgb.max(axis=2)
saturation = (peak - rgb.min(axis=2)) / np.maximum(1, peak)
weight = np.clip((.66 - saturation) / .23, 0, 1)
weight *= np.clip((rgb[:, :, 0] - rgb[:, :, 2] + 12) / 22, 0, 1)
# Preserve the eyes and mouth where they occupy a separate body-atlas region.
weight[384:, 768:] = 0
result = hd.copy()
result[:, :, :3] = np.rint(rgb + delta * weight[:, :, None]).clip(0, 255)
return result
if __name__ == '__main__':
import argparse
from pathlib import Path
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
cli.add_argument('hd', type=Path)
cli.add_argument('output_png', type=Path)
args = cli.parse_args()
with Image.open(args.original) as original, Image.open(args.hd) as hd:
image = correct_body(np.asarray(original.convert('RGBA')), np.asarray(hd.convert('RGBA')))
Image.fromarray(image).save(args.output_png, format='PNG')