# 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({
"130": {
"class_type": "LoadImage",
"inputs": {
"image": "input-aspect-ratio-converter"
},
"_meta": {
"title": "Load Image"
}
},
"144": {
"class_type": "ImageCompositeMasked",
"inputs": {
"destination": [
"224:162",
0
],
"mask": [
"224:168",
0
],
"resize_source": True,
"source": [
"225",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "Image Composite Masked"
}
},
"218": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"144",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"225": {
"class_type": "GeminiNanoBanana2",
"inputs": {
"aspect_ratio": "auto",
"images": [
"224:162",
0
],
"model": "Nano Banana 2 (Gemini 3.1 Flash Image)",
"prompt": "fill white areas. retain composition.",
"resolution": "4K",
"response_modalities": "IMAGE",
"seed": 592182428395941,
"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"
}
},
"226": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"224:162",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"224:152": {
"class_type": "Paste By Mask",
"inputs": {
"image_base": [
"224:230",
0
],
"image_to_paste": [
"224:229",
0
],
"mask": [
"224:229",
0
],
"resize_behavior": "keep_ratio_fit"
},
"_meta": {
"title": "Paste By Mask"
}
},
"224:153": {
"class_type": "ImageToMask",
"inputs": {
"channel": "red",
"image": [
"224:152",
0
]
},
"_meta": {
"title": "Convert Image to Mask"
}
},
"224:158": {
"class_type": "ImageToMask",
"inputs": {
"channel": "red",
"image": [
"224:231",
0
]
},
"_meta": {
"title": "Convert Image to Mask"
}
},
"224:159": {
"class_type": "InvertMask",
"inputs": {
"mask": [
"224:153",
0
]
},
"_meta": {
"title": "Invert Mask"
}
},
"224:162": {
"class_type": "ImageRemoveAlpha+",
"inputs": {
"image": [
"224:165",
0
]
},
"_meta": {
"title": "🔧 Image Remove Alpha"
}
},
"224:163": {
"class_type": "Get Image Size",
"inputs": {
"image": [
"130",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"224:165": {
"class_type": "Paste By Mask",
"inputs": {
"image_base": [
"224:231",
0
],
"image_to_paste": [
"130",
0
],
"mask": [
"224:152",
0
],
"resize_behavior": "keep_ratio_fit"
},
"_meta": {
"title": "Paste By Mask"
}
},
"224:168": {
"class_type": "MaskComposite",
"inputs": {
"destination": [
"224:158",
0
],
"operation": "add",
"source": [
"224:159",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "Combine Masks"
}
},
"224:191": {
"class_type": "FluxResolutionNode",
"inputs": {
"aspect_ratio": "9:16 (Slim Vertical)",
"custom_aspect_ratio": "1:1",
"custom_ratio": False,
"divisible_by": "8",
"megapixel": "2.1"
},
"_meta": {
"title": "Flux Resolution Calc"
}
},
"224:229": {
"class_type": "LayerUtility: ColorImage V2",
"inputs": {
"color": "#ffffff",
"custom_height": [
"224:163",
1
],
"custom_width": [
"224:163",
0
],
"size": "custom"
},
"_meta": {
"title": "LayerUtility: ColorImage V2"
}
},
"224:230": {
"class_type": "LayerUtility: ColorImage V2",
"inputs": {
"color": "#000000",
"custom_height": [
"224:191",
1
],
"custom_width": [
"224:191",
0
],
"size": "custom"
},
"_meta": {
"title": "LayerUtility: ColorImage V2"
}
},
"224:231": {
"class_type": "LayerUtility: ColorImage V2",
"inputs": {
"color": "#ffffff",
"custom_height": [
"224:191",
1
],
"custom_width": [
"224:191",
0
],
"size": "custom"
},
"_meta": {
"title": "LayerUtility: ColorImage V2"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)