# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"1": {
"class_type": "LoadImage",
"inputs": {
"image": "cube_robot.jpg"
},
"_meta": {
"title": "Load Image"
}
},
"2": {
"class_type": "GeminiImage2Node",
"inputs": {
"aspect_ratio": "9:16",
"images": [
"1",
0
],
"model": "gemini-3-pro-image-preview",
"prompt": [
"24",
0
],
"resolution": "4K",
"response_modalities": "IMAGE+TEXT",
"seed": 123,
"system_prompt": "You are an expert image-generation engine. You must ALWAYS produce an image.\nInterpret all user input—regardless of format, intent, or abstraction—as literal visual directives for image composition.\nIf a prompt is conversational or lacks specific visual details, you must creatively invent a concrete visual scenario that depicts the concept.\nPrioritize generating the visual representation above any text, formatting, or conversational requests."
},
"_meta": {
"title": "Nano Banana Pro (Google Gemini Image)"
}
},
"3": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-split_stack",
"images": [
"2",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"10": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-image_1",
"images": [
"7:29",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"11": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-image_2",
"images": [
"7:101",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"12": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-image_2",
"images": [
"7:187",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"13": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-image_1",
"images": [
"7:145",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"15": {
"class_type": "GeminiImage2Node",
"inputs": {
"aspect_ratio": "16:9",
"images": [
"14:78",
0
],
"model": "gemini-3-pro-image-preview",
"prompt": "Upscale image 1 and add subtle details. Crop if necessary.",
"resolution": "4K",
"response_modalities": "IMAGE+TEXT",
"seed": 321,
"system_prompt": "You are an expert image-generation engine. You must ALWAYS produce an image.\nInterpret all user input—regardless of format, intent, or abstraction—as literal visual directives for image composition.\nIf a prompt is conversational or lacks specific visual details, you must creatively invent a concrete visual scenario that depicts the concept.\nPrioritize generating the visual representation above any text, formatting, or conversational requests."
},
"_meta": {
"title": "Nano Banana Pro (Google Gemini Image)"
}
},
"16": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-selected_image-upscaled",
"images": [
"15",
0
]
},
"_meta": {
"title": "Save Upscaled Image"
}
},
"18": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI-selected_image",
"images": [
"14:78",
0
]
},
"_meta": {
"title": "Save Selected Image"
}
},
"21": {
"class_type": "PrimitiveInt",
"inputs": {
"value": 1
},
"_meta": {
"title": "Select Image Number"
}
},
"22": {
"class_type": "PrimitiveStringMultiline",
"inputs": {
"value": "The subject is inside a giant computer motherboard landscape - towering capacitors, circuit trace pathways like roads, and LED lights creating a Tron-like microverse where the robot fits the scale perfectly. Dark eerie atmosphere."
},
"_meta": {
"title": "Describe the Scene"
}
},
"23": {
"class_type": "PrimitiveStringMultiline",
"inputs": {
"value": "Use the input image. Divide the frame in 4 different stacked 16:9 frames.\nFor each frame choose the angle and type of shot (close up, extreme close up, dutch, over-shoulder, wide shot, Cowboy shot, full shot, etc).\n\nEnsure that subject is present in each frame and the environment is consistent between frames:\n{scene}"
},
"_meta": {
"title": "Prompt Template"
}
},
"24": {
"class_type": "RegexReplace",
"inputs": {
"case_insensitive": True,
"count": 0,
"dotall": False,
"multiline": False,
"regex_pattern": "{scene}",
"replace": [
"22",
0
],
"string": [
"23",
0
]
},
"_meta": {
"title": "Replace Text (Regex)"
}
},
"14:78": {
"class_type": "ImageFromBatch",
"inputs": {
"batch_index": [
"14:89",
0
],
"image": [
"7:333",
0
],
"length": 1
},
"_meta": {
"title": "Get Image from Batch"
}
},
"14:83": {
"class_type": "ResizeAndPadImage",
"inputs": {
"image": [
"1",
0
],
"interpolation": "area",
"padding_color": "white",
"target_height": [
"14:86",
1
],
"target_width": [
"14:86",
0
]
},
"_meta": {
"title": "Resize And Pad Image"
}
},
"14:86": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"14:78",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"14:87": {
"class_type": "ImageBatch",
"inputs": {
"image1": [
"14:78",
0
],
"image2": [
"14:83",
0
]
},
"_meta": {
"title": "Batch Images (DEPRECATED)"
}
},
"14:89": {
"class_type": "SimpleMath+",
"inputs": {
"a": [
"21",
0
],
"b": 0,
"value": "a - 1"
},
"_meta": {
"title": "Image # as index"
}
},
"7:101": {
"class_type": "ImageCrop",
"inputs": {
"height": [
"7:66",
0
],
"image": [
"2",
0
],
"width": [
"7:65",
0
],
"x": 0,
"y": [
"7:63",
0
]
},
"_meta": {
"title": "2"
}
},
"7:109": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"7:101",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"7:136": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"7:145",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"7:144": {
"class_type": "SimpleMath+",
"inputs": {
"a": [
"7:66",
0
],
"b": 0,
"value": "2*a"
},
"_meta": {
"title": "Row 3 - Y val"
}
},
"7:145": {
"class_type": "ImageCrop",
"inputs": {
"height": [
"7:66",
0
],
"image": [
"2",
0
],
"width": [
"7:65",
0
],
"x": 0,
"y": [
"7:144",
0
]
},
"_meta": {
"title": "3"
}
},
"7:164": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"7:187",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"7:172": {
"class_type": "SimpleMath+",
"inputs": {
"a": [
"7:66",
0
],
"b": 0,
"value": "3*a"
},
"_meta": {
"title": "Row 4 - Y val"
}
},
"7:18": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"2",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"7:187": {
"class_type": "ImageCrop",
"inputs": {
"height": [
"7:66",
0
],
"image": [
"2",
0
],
"width": [
"7:65",
0
],
"x": 0,
"y": [
"7:172",
0
]
},
"_meta": {
"title": "4"
}
},
"7:29": {
"class_type": "ImageCrop",
"inputs": {
"height": [
"7:66",
0
],
"image": [
"2",
0
],
"width": [
"7:65",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "1"
}
},
"7:32": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"7:29",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"7:333": {
"class_type": "ImageBatchMulti",
"inputs": {
"image_1": [
"7:29",
0
],
"image_2": [
"7:101",
0
],
"image_3": [
"7:145",
0
],
"image_4": [
"7:187",
0
],
"inputcount": 4
},
"_meta": {
"title": "Image Batch Multi"
}
},
"7:353": {
"class_type": "SimpleMath+",
"inputs": {
"a": 0,
"b": 0,
"value": "(2*a) + (3*b)"
},
"_meta": {
"title": "Row 3 - Y val"
}
},
"7:63": {
"class_type": "SimpleMath+",
"inputs": {
"a": [
"7:66",
0
],
"b": 0,
"value": "a"
},
"_meta": {
"title": "Row 2 - Y val"
}
},
"7:65": {
"class_type": "SimpleMath+",
"inputs": {
"a": [
"7:18",
0
],
"b": 0,
"value": "a"
},
"_meta": {
"title": "WIDTH"
}
},
"7:66": {
"class_type": "SimpleMath+",
"inputs": {
"a": [
"7:18",
1
],
"b": 0,
"value": "a / 4"
},
"_meta": {
"title": "HEIGHT"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)