import os import sys import argparse from pathlib import Path sys.path.append(str(Path(__file__).resolve().parent.parent)) import banana_pro_image # Default template image path located under inverse_uv/template.png DEFAULT_TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'template', 'template5.png') # Default prompt for real-to-render Minecraft character generation DEFAULT_PROMPT = """生成[图1]中角色的Minecraft形象 - 人物模型必须与minecraft的玩家模型相同,且为包含内层/外层贴图的双图层模型,不能有额外元素。材质不能高于Minecraft所支持的分辨率。模型参考[图2] - 生成的minecraft角色的姿势与[图2]完全一致(不是笔直站立),且必须包含正面图与背面图,并排放置。 - 使用容易区分前景的纯色背景。 - 在上述约束下,尽可能完美、准确的还原[图1]中的角色,包括外貌特征、全身所有服装、各种饰品等(不包括手持物品)""" def real2render(image_path, template_path=None, output_path=None, prompt=None, aspect_ratio="1:1", image_size="1K"): """ High-level API to generate a Minecraft 3D character render from a real character photo using Nano Banana Pro. :param image_path: Path to the real character input image (Graph 1). :param template_path: Path to the template reference image (Graph 2, defaults to inverse_uv/template.png). :param output_path: Output PNG path. :param prompt: Custom prompt text (defaults to standard Minecraft real-to-render prompt). :param aspect_ratio: Image aspect ratio (default: "1:1"). :param image_size: Image resolution (default: "1K"). :return: Path to generated image file. """ if not os.path.exists(image_path): raise FileNotFoundError(f"Real character image '{image_path}' does not exist.") template_file = template_path or DEFAULT_TEMPLATE_PATH if not os.path.exists(template_file): raise FileNotFoundError(f"Template image '{template_file}' does not exist.") prompt_text = prompt or DEFAULT_PROMPT print(f"[*] Real Image (Graph 1): {image_path}") print(f"[*] Template Image (Graph 2): {template_file}") print(f"[*] Image Size: {image_size}, Aspect Ratio: {aspect_ratio}") print(f"[*] Prompt:\n{prompt_text}\n") output_file = banana_pro_image.generate_img2img( local_image_paths=[image_path, template_file], prompt=prompt_text, output_path=output_path, aspect_ratio=aspect_ratio, image_size=image_size ) return output_file def main(): parser = argparse.ArgumentParser(description="Convert real character photo to Minecraft render using Nano Banana Pro.") parser.add_argument("image", help="Path to local real character photo (Graph 1).") parser.add_argument("-t", "--template", default=DEFAULT_TEMPLATE_PATH, help="Path to reference template image (Graph 2).") parser.add_argument("-o", "--output", help="Output path for the generated image.") parser.add_argument("-p", "--prompt", default=DEFAULT_PROMPT, help="Prompt for image generation.") parser.add_argument("-a", "--aspect-ratio", default="1:1", choices=["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"], help="Aspect ratio (default: 1:1).") parser.add_argument("-s", "--image-size", default="1K", choices=["1K", "2K", "4K"], help="Resolution size (default: 1K).") args = parser.parse_args() try: output_file = real2render( image_path=args.image, template_path=args.template, output_path=args.output, prompt=args.prompt, aspect_ratio=args.aspect_ratio, image_size=args.image_size ) print(f"[+] Task completed successfully. Output saved to: {output_file}") except Exception as e: print(f"[!] Execution failed: {e}") sys.exit(1) if __name__ == '__main__': main()