Sking / gpt_skin2real.py
EntropyDrop
feat: update skins
ac4ade7
Raw
History Blame
4.95 kB
import os
import sys
import uuid
import time
import argparse
import numpy as np
from PIL import Image
import mc_render
import gpt_image
from build_target_img import check_skin, ensure_valid_skin
from alice_to_steve import alice_to_steve
from mc_voxel_texture_resolver import resolve_voxel_consistency
def main():
parser = argparse.ArgumentParser(description="Render Minecraft skin to 3D views, upload to S3, and call img2img model.")
parser.add_argument("skin", help="Path to local Minecraft skin image file.")
parser.add_argument("-o", "--output", help="Output path for the generated image. Defaults to 'result_<timestamp>.png'.")
parser.add_argument("-p", "--prompt", default="生成ta的全身真人照片,再生成ta的背面放在图片右边。请准确还原人物的特征细节,注意真人照片里面通常不包含像素风格的装饰。", help="Prompt for the img2img model.")
parser.add_argument("-r", "--render-only", action="store_true", help="Only render the 3D front and back views locally, without calling the S3/img2img pipeline.")
args = parser.parse_args()
# 1. Validate skin path
if not os.path.exists(args.skin):
print(f"Error: Skin file '{args.skin}' does not exist.")
sys.exit(1)
# 2. Process/Validate the skin image (Alex/Steve conversion, validity, consistency)
try:
print(f"[*] Processing skin file: {args.skin}")
skin_image = Image.open(args.skin).convert('RGBA')
valid, is_alex = check_skin(skin_image)
if not valid:
print(f"Error: Skin '{args.skin}' fails validation.")
sys.exit(1)
if is_alex:
print("[*] Detected Alex skin. Converting to Steve...")
skin_image = alice_to_steve(skin_image)
skin_image = ensure_valid_skin(skin_image)
skin_image = resolve_voxel_consistency(skin_image)
except Exception as e:
print(f"[!] Skin processing failed: {e}")
sys.exit(1)
# 3. Generate front and back 3D renders using mc_render
pos_args2 = {
'head': (0, 28, 0),
'body': (0, 18, 0),
'right_arm': (-6, 18, 0),
'left_arm': (6, 18, 0),
'right_leg': (-2, 6, 0),
'left_leg': (2, 6, 0),
}
# Determine output filenames for renders
if args.render_only:
if args.output:
base, ext = os.path.splitext(args.output)
temp_front_path = f"{base}_front{ext or '.png'}"
temp_back_path = f"{base}_back{ext or '.png'}"
else:
temp_front_path = "render_front.png"
temp_back_path = "render_back.png"
else:
# Generate unique filenames for temp renders
unique_id = uuid.uuid4()
temp_front_path = f"temp_front_{unique_id}.png"
temp_back_path = f"temp_back_{unique_id}.png"
try:
print("[*] Rendering skin 3D front view...")
mc_render.render_skin(
skin=np.array(skin_image),
output_size=(768, 768),
cam_front=(0.25, 0.25, 0.25),
use_voxels=False,
ortho=False,
save_path=temp_front_path,
transparent_background=True,
zoom=0.3,
look_at_y=18,
pos_args=pos_args2,
off_screen=True
)
print("[*] Rendering skin 3D back view...")
mc_render.render_skin(
skin=np.array(skin_image),
output_size=(768, 768),
cam_front=(-0.25, 0.25, -0.25),
use_voxels=False,
ortho=False,
save_path=temp_back_path,
transparent_background=True,
zoom=0.3,
look_at_y=18,
pos_args=pos_args2,
off_screen=True
)
if args.render_only:
print(f"[+] Render-only mode active. Views saved to:\n - {temp_front_path}\n - {temp_back_path}")
return
# 4. Invoke gpt_image.generate_img2img
print("[*] Invoking image-to-image pipeline via gpt_image...")
output_file = gpt_image.generate_img2img(
local_image_paths=[temp_front_path, temp_back_path],
prompt=args.prompt,
output_path=args.output
)
print(f"[+] Pipeline completed successfully. Output saved to: {output_file}")
except Exception as e:
print(f"[!] Execution failed: {e}")
sys.exit(1)
finally:
# Clean up local temporary files if not in render-only mode
if not args.render_only:
for temp_file in [temp_front_path, temp_back_path]:
if os.path.exists(temp_file):
print(f"[*] Removing local temporary file: {temp_file}")
try:
os.remove(temp_file)
except Exception as e:
print(f"[!] Failed to remove local file {temp_file}: {e}")
if __name__ == '__main__':
main()