Datasets:
EntropyDrop commited on
Commit ·
6cf51a3
1
Parent(s): e1a8cb2
real2render
Browse files- DDJ_real2render/create_template.py +149 -0
- DDJ_real2render/real2render.py +83 -0
- DDJ_real2render/template/template1.png +3 -0
- DDJ_real2render/template/template2_ddj.png +3 -0
- DDJ_real2render/template/template3_ddj.png +3 -0
- DDJ_real2render/template/template4_ddj.png +3 -0
- DDJ_real2render/template/template5.png +3 -0
- DDJ_real2render/template/template6.png +3 -0
- DDJ_real2render/template/template7.png +3 -0
- DDJ_real2render/test_input/img1.jpg +3 -0
- DDJ_real2render/test_input/img2.png +3 -0
- DDJ_real2render/test_output/img1_template1.png +3 -0
- DDJ_real2render/test_output/img1_template1_2.png +3 -0
- DDJ_real2render/test_output/img1_template2.png +3 -0
- DDJ_real2render/test_output/img1_template3.png +3 -0
- DDJ_real2render/test_output/img1_template5.png +3 -0
- DDJ_real2render/test_output/img1_template5_2.png +3 -0
- DDJ_real2render/test_output/img1_template7.png +3 -0
- DDJ_real2render/test_output/img2_template1.png +3 -0
- DDJ_real2render/test_output/img2_template2.png +3 -0
- DDJ_real2render/test_output/img2_template6.png +3 -0
- DDJ_real2render/test_output/img2_template7.png +3 -0
DDJ_real2render/create_template.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import argparse
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
def resolve_renderer_path(renderer_path):
|
| 10 |
+
if not renderer_path or not os.path.exists(renderer_path):
|
| 11 |
+
raise FileNotFoundError(f"Renderer directory '{renderer_path}' does not exist.")
|
| 12 |
+
return os.path.abspath(renderer_path)
|
| 13 |
+
|
| 14 |
+
def parse_bg_color(bg_color):
|
| 15 |
+
if bg_color is None:
|
| 16 |
+
return None
|
| 17 |
+
if isinstance(bg_color, str):
|
| 18 |
+
cleaned = bg_color.strip().lower()
|
| 19 |
+
if cleaned in ("none", "transparent", ""):
|
| 20 |
+
return None
|
| 21 |
+
if cleaned.startswith("(") and cleaned.endswith(")"):
|
| 22 |
+
cleaned = cleaned[1:-1]
|
| 23 |
+
if "," in cleaned:
|
| 24 |
+
parts = [int(p.strip()) for p in cleaned.split(",")]
|
| 25 |
+
return tuple(parts)
|
| 26 |
+
return bg_color
|
| 27 |
+
return bg_color
|
| 28 |
+
|
| 29 |
+
def create_template(skin_path, output_path, renderer_path, size="512x1024", bg_color="black"):
|
| 30 |
+
"""
|
| 31 |
+
Renders walk_front_both_layer_ortho and walk_back_both_layer_ortho views for a given skin
|
| 32 |
+
using PyVista, merges them horizontally, adds background color, and saves the result to output_path.
|
| 33 |
+
|
| 34 |
+
:param skin_path: Path to the input skin PNG image.
|
| 35 |
+
:param output_path: Destination path for the merged output template image.
|
| 36 |
+
:param renderer_path: Path to differentiable_minecraft_renderer directory (required).
|
| 37 |
+
:param size: Resolution tuple (W, H) or string (e.g. "512x1024"). Default is "512x1024".
|
| 38 |
+
:param bg_color: Background color for the output template image (default: "black"). Set to None or "transparent" for transparent background.
|
| 39 |
+
:return: Path to generated output file.
|
| 40 |
+
"""
|
| 41 |
+
if not os.path.exists(skin_path):
|
| 42 |
+
raise FileNotFoundError(f"Skin image '{skin_path}' does not exist.")
|
| 43 |
+
|
| 44 |
+
dmr_path = resolve_renderer_path(renderer_path)
|
| 45 |
+
if dmr_path not in sys.path:
|
| 46 |
+
sys.path.insert(0, dmr_path)
|
| 47 |
+
|
| 48 |
+
from mc_skin_utils import mc_render
|
| 49 |
+
from config import get_views, walk_rot, static_rot, walk_offset, static_offset, parse_sizes
|
| 50 |
+
try:
|
| 51 |
+
from merge_views import merge
|
| 52 |
+
except ImportError:
|
| 53 |
+
merge = None
|
| 54 |
+
|
| 55 |
+
if isinstance(size, str):
|
| 56 |
+
parsed = parse_sizes(size)
|
| 57 |
+
target_size = parsed[0]
|
| 58 |
+
elif isinstance(size, (tuple, list)) and len(size) == 2:
|
| 59 |
+
target_size = (int(size[0]), int(size[1]))
|
| 60 |
+
else:
|
| 61 |
+
target_size = (512, 1024)
|
| 62 |
+
|
| 63 |
+
print(f"Loading skin image from: {skin_path}")
|
| 64 |
+
skin_img = Image.open(skin_path).convert("RGBA")
|
| 65 |
+
skin_np = np.array(skin_img)
|
| 66 |
+
|
| 67 |
+
# Clean semi-transparency to match build_target_img preprocessing
|
| 68 |
+
alpha = skin_np[..., 3]
|
| 69 |
+
semi_transparent = (alpha > 0) & (alpha < 255)
|
| 70 |
+
skin_np[semi_transparent, 3] = 255
|
| 71 |
+
clean_skin_img = Image.fromarray(skin_np)
|
| 72 |
+
|
| 73 |
+
views_config = get_views(target_size)
|
| 74 |
+
|
| 75 |
+
view_names = ["walk_front_both_layer_ortho", "walk_back_both_layer_ortho"]
|
| 76 |
+
rendered_images = {}
|
| 77 |
+
|
| 78 |
+
for view_name in view_names:
|
| 79 |
+
if view_name not in views_config:
|
| 80 |
+
raise KeyError(f"View '{view_name}' not found in renderer config.")
|
| 81 |
+
params = views_config[view_name]
|
| 82 |
+
rot_args = walk_rot if params["walk"] else static_rot
|
| 83 |
+
offset_args = walk_offset if params["walk"] else static_offset
|
| 84 |
+
|
| 85 |
+
print(f"Rendering view with PyVista: {view_name} (Size: {params['output_size']})...")
|
| 86 |
+
pv_render = mc_render.render_skin(
|
| 87 |
+
skin=clean_skin_img,
|
| 88 |
+
output_size=params["output_size"],
|
| 89 |
+
cam_front=params["cam_front"],
|
| 90 |
+
zoom=params["zoom"],
|
| 91 |
+
look_at_y=params["look_at_y"],
|
| 92 |
+
use_voxels=False,
|
| 93 |
+
light=False,
|
| 94 |
+
transparent_background=True,
|
| 95 |
+
core_display=params["core_display"],
|
| 96 |
+
decor_display=params["decor_display"],
|
| 97 |
+
rot_args=rot_args,
|
| 98 |
+
offset_args=offset_args,
|
| 99 |
+
ortho=params.get("ortho", False),
|
| 100 |
+
off_screen=True
|
| 101 |
+
)
|
| 102 |
+
rendered_images[view_name] = Image.fromarray(pv_render)
|
| 103 |
+
|
| 104 |
+
front_img = rendered_images["walk_front_both_layer_ortho"]
|
| 105 |
+
back_img = rendered_images["walk_back_both_layer_ortho"]
|
| 106 |
+
|
| 107 |
+
if merge is not None:
|
| 108 |
+
merged_img = merge(front_img, back_img, direction="horizontal")
|
| 109 |
+
else:
|
| 110 |
+
w1, h1 = front_img.size
|
| 111 |
+
w2, h2 = back_img.size
|
| 112 |
+
merged_img = Image.new("RGBA", (w1 + w2, max(h1, h2)))
|
| 113 |
+
merged_img.paste(front_img, (0, 0))
|
| 114 |
+
merged_img.paste(back_img, (w1, 0))
|
| 115 |
+
|
| 116 |
+
parsed_bg = parse_bg_color(bg_color)
|
| 117 |
+
if parsed_bg is not None:
|
| 118 |
+
bg = Image.new("RGBA", merged_img.size, parsed_bg)
|
| 119 |
+
merged_img = Image.alpha_composite(bg, merged_img)
|
| 120 |
+
|
| 121 |
+
out_dir = os.path.dirname(os.path.abspath(output_path))
|
| 122 |
+
if out_dir:
|
| 123 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 124 |
+
|
| 125 |
+
merged_img.save(output_path)
|
| 126 |
+
print(f"Successfully generated merged template: {output_path} (Size: {merged_img.size})")
|
| 127 |
+
return output_path
|
| 128 |
+
|
| 129 |
+
def main():
|
| 130 |
+
parser = argparse.ArgumentParser(
|
| 131 |
+
description="Create merged front & back orthographic reference template image from a Minecraft skin."
|
| 132 |
+
)
|
| 133 |
+
parser.add_argument("--skin", "--skin_path", dest="skin_path", required=True, help="Path to input skin image file.")
|
| 134 |
+
parser.add_argument("--output", "--output_path", dest="output_path", required=True, help="Path to save the merged output template image.")
|
| 135 |
+
parser.add_argument("--renderer_path", "--renderer", dest="renderer_path", required=True, help="Path to differentiable_minecraft_renderer directory.")
|
| 136 |
+
parser.add_argument("--size", "--resolution", default="512x1024", help="Resolution per view (default: 512x1024).")
|
| 137 |
+
parser.add_argument("--bg_color", "--bg-color", "--background", dest="bg_color", default="black", help="Background color for the output template image (default: black). Set to 'none' or 'transparent' for transparency.")
|
| 138 |
+
|
| 139 |
+
args = parser.parse_args()
|
| 140 |
+
create_template(
|
| 141 |
+
skin_path=args.skin_path,
|
| 142 |
+
output_path=args.output_path,
|
| 143 |
+
renderer_path=args.renderer_path,
|
| 144 |
+
size=args.size,
|
| 145 |
+
bg_color=args.bg_color,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
main()
|
DDJ_real2render/real2render.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import argparse
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
sys.path.append(str(Path(__file__).resolve().parent.parent))
|
| 6 |
+
|
| 7 |
+
import banana_pro_image
|
| 8 |
+
|
| 9 |
+
# Default template image path located under inverse_uv/template.png
|
| 10 |
+
DEFAULT_TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'template', 'template5.png')
|
| 11 |
+
|
| 12 |
+
# Default prompt for real-to-render Minecraft character generation
|
| 13 |
+
DEFAULT_PROMPT = """生成[图1]中角色的Minecraft形象
|
| 14 |
+
|
| 15 |
+
- 人物模型必须与minecraft的玩家模型相同,且为包含内层/外层贴图的双图层模型,不能有额外元素。材质不能高于Minecraft所支持的分辨率。模型参考[图2]
|
| 16 |
+
|
| 17 |
+
- 生成的minecraft角色的姿势与[图2]一致,且必须包含正面图与背面图,并排放置。使用纯色背景。
|
| 18 |
+
|
| 19 |
+
- 在上述约束下,尽可能完美、准确的还原[图1]中的角色,包括外貌特征、全身所有服装、各种饰品等(不包括手持物品)"""
|
| 20 |
+
|
| 21 |
+
def real2render(image_path, template_path=None, output_path=None, prompt=None, aspect_ratio="1:1", image_size="1K"):
|
| 22 |
+
"""
|
| 23 |
+
High-level API to generate a Minecraft 3D character render from a real character photo using Nano Banana Pro.
|
| 24 |
+
|
| 25 |
+
:param image_path: Path to the real character input image (Graph 1).
|
| 26 |
+
:param template_path: Path to the template reference image (Graph 2, defaults to inverse_uv/template.png).
|
| 27 |
+
:param output_path: Output PNG path.
|
| 28 |
+
:param prompt: Custom prompt text (defaults to standard Minecraft real-to-render prompt).
|
| 29 |
+
:param aspect_ratio: Image aspect ratio (default: "1:1").
|
| 30 |
+
:param image_size: Image resolution (default: "1K").
|
| 31 |
+
:return: Path to generated image file.
|
| 32 |
+
"""
|
| 33 |
+
if not os.path.exists(image_path):
|
| 34 |
+
raise FileNotFoundError(f"Real character image '{image_path}' does not exist.")
|
| 35 |
+
|
| 36 |
+
template_file = template_path or DEFAULT_TEMPLATE_PATH
|
| 37 |
+
if not os.path.exists(template_file):
|
| 38 |
+
raise FileNotFoundError(f"Template image '{template_file}' does not exist.")
|
| 39 |
+
|
| 40 |
+
prompt_text = prompt or DEFAULT_PROMPT
|
| 41 |
+
|
| 42 |
+
print(f"[*] Real Image (Graph 1): {image_path}")
|
| 43 |
+
print(f"[*] Template Image (Graph 2): {template_file}")
|
| 44 |
+
print(f"[*] Image Size: {image_size}, Aspect Ratio: {aspect_ratio}")
|
| 45 |
+
print(f"[*] Prompt:\n{prompt_text}\n")
|
| 46 |
+
|
| 47 |
+
output_file = banana_pro_image.generate_img2img(
|
| 48 |
+
local_image_paths=[image_path, template_file],
|
| 49 |
+
prompt=prompt_text,
|
| 50 |
+
output_path=output_path,
|
| 51 |
+
aspect_ratio=aspect_ratio,
|
| 52 |
+
image_size=image_size
|
| 53 |
+
)
|
| 54 |
+
return output_file
|
| 55 |
+
|
| 56 |
+
def main():
|
| 57 |
+
parser = argparse.ArgumentParser(description="Convert real character photo to Minecraft render using Nano Banana Pro.")
|
| 58 |
+
parser.add_argument("image", help="Path to local real character photo (Graph 1).")
|
| 59 |
+
parser.add_argument("-t", "--template", default=DEFAULT_TEMPLATE_PATH, help="Path to reference template image (Graph 2).")
|
| 60 |
+
parser.add_argument("-o", "--output", help="Output path for the generated image.")
|
| 61 |
+
parser.add_argument("-p", "--prompt", default=DEFAULT_PROMPT, help="Prompt for image generation.")
|
| 62 |
+
parser.add_argument("-a", "--aspect-ratio", default="1:1",
|
| 63 |
+
choices=["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"],
|
| 64 |
+
help="Aspect ratio (default: 1:1).")
|
| 65 |
+
parser.add_argument("-s", "--image-size", default="1K", choices=["1K", "2K", "4K"], help="Resolution size (default: 1K).")
|
| 66 |
+
args = parser.parse_args()
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
output_file = real2render(
|
| 70 |
+
image_path=args.image,
|
| 71 |
+
template_path=args.template,
|
| 72 |
+
output_path=args.output,
|
| 73 |
+
prompt=args.prompt,
|
| 74 |
+
aspect_ratio=args.aspect_ratio,
|
| 75 |
+
image_size=args.image_size
|
| 76 |
+
)
|
| 77 |
+
print(f"[+] Task completed successfully. Output saved to: {output_file}")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"[!] Execution failed: {e}")
|
| 80 |
+
sys.exit(1)
|
| 81 |
+
|
| 82 |
+
if __name__ == '__main__':
|
| 83 |
+
main()
|
DDJ_real2render/template/template1.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template2_ddj.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template3_ddj.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template4_ddj.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template5.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template6.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template7.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_input/img1.jpg
ADDED
|
Git LFS Details
|
DDJ_real2render/test_input/img2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template1.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template1_2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template3.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template5.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template5_2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img1_template7.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img2_template1.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img2_template2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img2_template6.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img2_template7.png
ADDED
|
Git LFS Details
|