Datasets:
Download DDJ_real2render/real2render.py from ramuuuo/Sking: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/datasets/ramuuuo/Sking/resolve/main/DDJ_real2render/real2render.py
- Command line
-
hf download hf://datasets/ramuuuo/Sking/DDJ_real2render/real2render.py
-
curl -L -o real2render.py https://huggingface.co/datasets/ramuuuo/Sking/resolve/main/DDJ_real2render/real2render.py
5.11 kB
| 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') | |
| def _build_template_refs(count): | |
| """Build the [图2][图3]...[图N+1] reference string for the prompt.""" | |
| return "".join(f"[图{i}]" for i in range(2, count + 2)) | |
| # Default prompt for real-to-render Minecraft character generation. | |
| # {template_refs} is replaced at runtime based on the number of templates. | |
| DEFAULT_PROMPT = """把[图1]中角色生成为参考图片的风格:{template_refs} | |
| 1. 包含内层/外层贴图的双图层模型,不能有额外元素。texture不能高于Minecraft所支持的分辨率,参考{template_refs}。 | |
| 2. 生成角色的尺寸、朝向、姿势必须与{template_refs}完全一致,轮廓与内层或外层皮肤完全贴合,不能用超出内外层皮肤的任何元素表达角色的特征,无光影特效。 | |
| 3. 使用容易区分前景的纯色背景。 | |
| 4. 准确的还原包括外貌特征、全身所有服装、各种饰品等(不包括手持物品和披风)""" | |
| def real2render( | |
| image_path, | |
| template_paths=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_paths: List of paths to template reference images (Graph 2, Graph 3, ...). | |
| Defaults to [DEFAULT_TEMPLATE_PATH] if None or empty. | |
| :param output_path: Output PNG path. | |
| :param prompt: Custom prompt text. Use {template_refs} to insert the reference image tags. | |
| Defaults to the 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.") | |
| if not template_paths: | |
| template_paths = [DEFAULT_TEMPLATE_PATH] | |
| for i, tp in enumerate(template_paths): | |
| if not os.path.exists(tp): | |
| raise FileNotFoundError(f"Template image {i + 2} '{tp}' does not exist.") | |
| template_refs = _build_template_refs(len(template_paths)) | |
| prompt_text = (prompt or DEFAULT_PROMPT).format(template_refs=template_refs) | |
| print(f"[*] Real Image (Graph 1): {image_path}") | |
| for i, tp in enumerate(template_paths): | |
| print(f"[*] Template Image (Graph {i + 2}): {tp}") | |
| print(f"[*] Image Size: {image_size}, Aspect Ratio: {aspect_ratio}") | |
| print(f"[*] Prompt:\n{prompt_text}\n") | |
| local_image_paths = [image_path] + list(template_paths) | |
| 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", | |
| action="append", | |
| default=None, | |
| help="Path to a reference template image. May be specified multiple times " | |
| "(e.g. -t t1.png -t t2.png -t t3.png). " | |
| "Defaults to the built-in template if omitted." | |
| ) | |
| parser.add_argument("-o", "--output", help="Output path for the generated image.") | |
| parser.add_argument("-p", "--prompt", default=None, | |
| help="Path to a prompt text file. Use {template_refs} as placeholder for reference image tags in the file.") | |
| 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: | |
| prompt_content = None | |
| if args.prompt: | |
| with open(args.prompt, "r", encoding="utf-8") as f: | |
| prompt_content = f.read() | |
| output_file = real2render( | |
| image_path=args.image, | |
| template_paths=args.template, | |
| output_path=args.output, | |
| prompt=prompt_content, | |
| 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() | |