# 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({
"125": {
"class_type": "LoadImage",
"inputs": {
"image": "2682eac315523a6fa6f71f7ec8f6b972752551dd7d782629083f65db3be35da0.png"
},
"_meta": {
"title": "Load Image"
}
},
"131": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"143:134",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"143:128": {
"class_type": "GeminiNanoBanana2",
"inputs": {
"aspect_ratio": "16:9",
"images": [
"125",
0
],
"model": "Nano Banana 2 (Gemini 3.1 Flash Image)",
"prompt": [
"143:2",
0
],
"resolution": "2K",
"response_modalities": "IMAGE",
"seed": 443144447094291,
"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.",
"thinking_level": "HIGH"
},
"_meta": {
"title": "Nano Banana 2"
}
},
"143:130": {
"class_type": "GeminiImage2Node",
"inputs": {
"aspect_ratio": "16:9",
"images": [
"125",
0
],
"model": "gemini-3-pro-image-preview",
"prompt": [
"143:2",
0
],
"resolution": "2K",
"response_modalities": "IMAGE+TEXT",
"seed": 314507571392615,
"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)"
}
},
"143:134": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"143:130",
0
],
"on_true": [
"143:128",
0
],
"switch": [
"143:136",
0
]
},
"_meta": {
"title": "If/Else Switch"
}
},
"143:135": {
"class_type": "CustomCombo",
"inputs": {
"choice": "Nano Banana 2"
},
"_meta": {
"title": "Custom Combo"
}
},
"143:136": {
"class_type": "StringCompare",
"inputs": {
"case_sensitive": False,
"mode": "Starts With",
"string_a": [
"143:135",
0
],
"string_b": "nano banana 2"
},
"_meta": {
"title": "Compare Text"
}
},
"143:2": {
"class_type": "PrimitiveStringMultiline",
"inputs": {
"value": "Role: Act as an expert art director and compositing specialist. Your objective is to adapt the provided image to a new aspect ratio by expanding the background and rearranging its core elements. Never stretch, squish, or letterbox the image.\n\n1. Canvas Expansion & Outpainting\n\nSeamless Extension: Expand the canvas to the target dimensions using generative outpainting. Seamlessly continue the existing environment (sky, textures, walls, floors) while perfectly matching the original lighting, grain, and perspective.\n\nStrict Limitations: Do not shrink the original image onto a blurred or colored background. Do not introduce new focal objects, characters, or decorative borders. Only continue existing environmental elements.\n\n2. Element Redistribution\n\nTreat as Layers: Separate the main components (typography, logos, hero imagery, credits) into distinct, movable elements.\n\nSmart Rearrangement: Reposition these elements to suit the new aspect ratio using professional layout principles (e.g., shifting a vertical stack into a horizontal, side-by-side composition). Use the newly generated space intentionally to avoid awkward negative space.\n\nPreserve Proportions: Maintain the exact original aspect ratio of every individual element. Keep all font styles, weights, and text styling identical. Scale elements uniformly only if required for layout balance.\n\n3. Visual Integrity & Polish\n\nMaintain Hierarchy: Preserve the original visual dominance of the headline, hero image, and logo placement.\n\nNative Feel: The final composition must look cohesive, breathable, and deliberately designed from scratch for this specific format."
},
"_meta": {
"title": "Prompt"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)