File size: 5,364 Bytes
6cf51a3
 
 
 
 
 
 
 
 
 
 
 
 
 
0d6b007
6cf51a3
0d6b007
5e51992
 
6cf51a3
1c65989
6cf51a3
64c7374
 
 
 
 
 
 
 
 
 
6cf51a3
 
 
 
 
2d86a45
64c7374
6cf51a3
 
 
 
 
 
 
 
 
 
 
 
 
2d86a45
 
 
64c7374
 
 
 
2d86a45
6cf51a3
 
 
 
2d86a45
 
64c7374
 
6cf51a3
 
 
2d86a45
 
 
64c7374
 
2d86a45
6cf51a3
2d86a45
6cf51a3
 
 
 
 
 
 
 
 
 
 
2d86a45
64c7374
6cf51a3
 
0d6b007
6cf51a3
60df411
 
6cf51a3
 
 
 
 
 
2d86a45
64c7374
6cf51a3
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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的玩家模型相同,且为包含内层/外层贴图的双图层模型,不能有额外元素。texture不能高于Minecraft所支持的分辨率,参考[图2][图3][图4]。

- 生成的minecraft角色的尺寸、朝向、姿势必须与[图2][图3][图4]完全一致。

- 使用容易区分前景的纯色背景。

- 在上述约束下,尽可能完美、准确的还原[图1]中的角色,包括外貌特征、全身所有服装、各种饰品等(不包括手持物品和披风)"""

def real2render(
    image_path,
    template_path=None,
    template2_path=None,
    output_path=None,
    prompt=None,
    aspect_ratio="1:1",
    image_size="1K",
    template3_path=None,
):
    """
    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 template2_path: Path to the second template reference image (Graph 3, defaults to None).
    :param template3_path: Path to the third template reference image (Graph 4, defaults to None).
    :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.")
        
    template2_file = template2_path
    if template2_file and not os.path.exists(template2_file):
        raise FileNotFoundError(f"Template 2 image '{template2_file}' does not exist.")

    template3_file = template3_path
    if template3_file and not os.path.exists(template3_file):
        raise FileNotFoundError(f"Template 3 image '{template3_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}")
    if template2_file:
        print(f"[*] Template 2 Image (Graph 3): {template2_file}")
    if template3_file:
        print(f"[*] Template 3 Image (Graph 4): {template3_file}")
    print(f"[*] Image Size: {image_size}, Aspect Ratio: {aspect_ratio}")
    print(f"[*] Prompt:\n{prompt_text}\n")
    
    local_image_paths = [image_path, template_file]
    if template2_file:
        local_image_paths.append(template2_file)
    if template3_file:
        local_image_paths.append(template3_file)
        
    output_file = banana_pro_image.generate_img2img(
        local_image_paths=local_image_paths,
        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("-t2", "--template2", default=None, help="Path to second reference template image (Graph 3).")
    parser.add_argument("-t3", "--template3", default=None, help="Path to third reference template image (Graph 4).")
    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.")
    parser.add_argument("-s", "--image-size", default="2K", choices=["1K", "2K", "4K"], help="Resolution size (default: 2K).")
    args = parser.parse_args()

    try:
        output_file = real2render(
            image_path=args.image,
            template_path=args.template,
            template2_path=args.template2,
            template3_path=args.template3,
            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()