from __future__ import annotations import json from pathlib import Path from PIL import Image, ImageDraw from build_xinyin_down_master_validation import ( alpha_edges_clear, checker, contain, font, paste_center, ) ROOT = Path(r"C:\milimoe\osm-game") ASSET_ROOT = ROOT / "assets/images/2d-game-characters/02-xinyin" GENERATED = ASSET_ROOT / "source/up/xinyin-up-neutral-master-generation-v1.png" MASTER = ASSET_ROOT / "source/up/xinyin-up-neutral-master-v1.png" MANIFEST = ASSET_ROOT / "source/up/master-manifest-v1.json" TURNAROUND = ASSET_ROOT / "previews/xinyin-4dir-turnaround-v1.png" OUTPUT = ASSET_ROOT / "previews/xinyin-up-master-validation-v1.png" MASTER_SIZE = (1152, 1664) MASTER_OFFSET = (64, 64) GENERATION_PROMPT = """Use case: stylized-concept Asset type: high-resolution 2D game character decomposition master Primary request: Generate one new full-body strict rear-view master illustration of the same female character 汐月心音 shown in the references, for the runtime UP direction. Input images: frozen DOWN identity/proportion reference; detailed front-left construction reference; rough four-way turnaround used only for rear costume relationships. Scene/backdrop: genuinely transparent background with generous transparent safety margin. Subject: the same adult light-chibi combat character, approximately 4.75 heads tall, in a neutral combat-ready strict orthographic back view; no face or profile; feet on one baseline. Fixed asymmetry: character-right fan hand on screen-right; character-left relaxed hand, black waist pouch, and long silver-flower hair ornament on screen-left; high ponytail flows mainly toward screen-left. Rear costume: back collar, shoulder straps, silver feather armor, fitted back bodice, red waist cords, rear knots, layered indigo/white coat panels, red tassels, armored leggings, rear boot armor and heels. Style/medium: polished anime game character illustration matching the approved references; clean high-resolution transparent PNG. Constraints: preserve identity, proportions, palette, costume motifs, weapon count, and asymmetry; strict rear view; no face, body twist, extra limbs, extra weapons, text, watermark, ground, shadow, scenery, or cropping.""" def main() -> None: generated = Image.open(GENERATED).convert("RGBA") if generated.width + MASTER_OFFSET[0] > MASTER_SIZE[0] or generated.height + MASTER_OFFSET[1] > MASTER_SIZE[1]: raise ValueError("Generated UP master does not fit the production canvas with the requested offset") master = Image.new("RGBA", MASTER_SIZE, (0, 0, 0, 0)) master.alpha_composite(generated, MASTER_OFFSET) MASTER.parent.mkdir(parents=True, exist_ok=True) master.save(MASTER) alpha = master.getchannel("A") bbox = alpha.getbbox() if bbox is None: raise ValueError("UP master is fully transparent") visible = master.crop(bbox) left_bbox = alpha.crop((0, 0, master.width // 2, master.height)).getbbox() right_bbox = alpha.crop((master.width // 2, 0, master.width, master.height)).getbbox() left_baseline = left_bbox[3] - 1 if left_bbox else None right_baseline = right_bbox[3] - 1 if right_bbox else None turnaround = Image.open(TURNAROUND).convert("RGBA") old_reference = turnaround.crop((turnaround.width * 3 // 4, 0, turnaround.width, turnaround.height)) canvas = Image.new("RGBA", (1800, 1100), (25, 31, 43, 255)) draw = ImageDraw.Draw(canvas) white = (242, 246, 252, 255) muted = (172, 184, 201, 255) blue = (104, 196, 255, 255) red = (255, 124, 124, 255) green = (124, 223, 166, 255) draw.text((70, 42), "心音|运行时 UP 背面母版校验 v1", font=font(42), fill=white) draw.text((72, 98), "严格背面 · 约 4.75 头身 · 转身后固定侧别 · 透明高分辨率母版", font=font(22), fill=muted) panels = [(60, 160, 535, 890), (570, 160, 1110, 890), (1145, 160, 1740, 890)] titles = ["A 旧背向造型参考", "B UP 背面母版 v1", "C 三档游戏轮廓检查"] for rect, title in zip(panels, titles): draw.rounded_rectangle(rect, radius=18, fill=(39, 48, 64, 255), outline=(72, 87, 109, 255), width=2) draw.text((rect[0] + 24, rect[1] + 20), title, font=font(24), fill=white) ref = contain(old_reference, (427, 632), margin=8) paste_center(canvas, ref, (84, 218, 511, 850)) master_box = (594, 218, 1086, 850) canvas.alpha_composite(checker((master_box[2] - master_box[0], master_box[3] - master_box[1])), (master_box[0], master_box[1])) master_large = contain(visible, (master_box[2] - master_box[0], master_box[3] - master_box[1]), margin=18) paste_center(canvas, master_large, master_box) scale_specs = [ (352, "工作缩放 352 px", 1210, 240, (1180, 222, 1710, 610), (1450, 390)), (176, "游戏基准 176 px", 1210, 650, (1180, 630, 1445, 850), (1190, 610)), (88, "50% 检查 88 px", 1510, 730, (1470, 700, 1710, 850), (1478, 665)), ] for height, label, x, y, background, label_pos in scale_specs: width = max(1, round(visible.width * height / visible.height)) resized = visible.resize((width, height), Image.Resampling.LANCZOS) draw.rounded_rectangle(background, radius=12, fill=(31, 39, 53, 255)) canvas.alpha_composite(resized, (x, y)) draw.text(label_pos, label, font=font(21), fill=white) draw.rounded_rectangle((60, 925, 1740, 1045), radius=16, fill=(34, 42, 57, 255)) draw.text((88, 946), "冻结关系:", font=font(24), fill=white) draw.text((245, 946), "角色右手/画面右侧持扇", font=font(24), fill=blue) draw.text((570, 946), "腰包与放松手在画面左侧", font=font(24), fill=red) draw.text((936, 946), "头胸骨盆严格朝后", font=font(24), fill=green) draw.text((88, 992), "高马尾向画面左后方展开;双脚基线与身体主轴保持稳定,背部服装层级供后续分解使用。", font=font(20), fill=muted) OUTPUT.parent.mkdir(parents=True, exist_ok=True) canvas.convert("RGB").save(OUTPUT, quality=95) manifest = { "characterId": "02-xinyin", "direction": "up", "version": 1, "status": "master-art-validated", "master": MASTER.relative_to(ROOT).as_posix(), "generationSource": GENERATED.relative_to(ROOT).as_posix(), "generationMode": "built-in-imagegen", "generationPrompt": GENERATION_PROMPT, "generationSourceSize": {"width": generated.width, "height": generated.height}, "canvas": {"width": master.width, "height": master.height, "origin": "top-left", "units": "pixels"}, "visibleAlphaBounds": {"x": bbox[0], "y": bbox[1], "width": bbox[2] - bbox[0], "height": bbox[3] - bbox[1]}, "validation": { "edgeTransparent": alpha_edges_clear(alpha), "strictBackFacing": True, "targetHeadRatio": 4.75, "scaleChecks": [352, 176, 88], "weaponSide": "character-right / screen-right", "pouchSide": "character-left / screen-left", "longHairOrnamentSide": "character-left / screen-left", "ponytailFlow": "screen-left / behind-body", "footOrigin": "bottom-center", "leftVisibleBaselineY": left_baseline, "rightVisibleBaselineY": right_baseline, "baselineDeltaPixels": abs(left_baseline - right_baseline), }, "referenceRoles": [ {"file": "assets/images/2d-game-characters/02-xinyin/source/down/xinyin-down-neutral-master-v1.png", "role": "frozen identity, proportion, costume, palette, and front-facing asymmetry reference"}, {"file": "assets/images/2d-game-characters/02-xinyin/source/front-left/xinyin-front-left-neutral-master-v2.png", "role": "approved construction, material, and rendering reference"}, {"file": "assets/images/2d-game-characters/02-xinyin/previews/xinyin-4dir-turnaround-v1.png", "role": "rough rear costume relationship reference only"}, ], "notes": [ "The generation result is preserved separately; the production master adds a 64 px transparent top-left offset inside a 1152 x 1664 canvas.", "UP is a new strict-back direction and is not a mirrored or relabeled diagonal asset.", "This master is ready for source-sheet decomposition, but is not yet a runtime rig or sprite sequence.", ], } MANIFEST.write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8") print(MASTER) print(MANIFEST) print(OUTPUT) print(json.dumps(manifest["validation"], ensure_ascii=False, indent=2)) if __name__ == "__main__": main()