# 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({
"9": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "Nano_Banana2",
"images": [
"23",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"16": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"23": {
"class_type": "GeminiNanoBanana2",
"inputs": {
"aspect_ratio": "16:9",
"images": [
"16",
0
],
"model": "Nano Banana 2 (Gemini 3.1 Flash Image)",
"prompt": "Edit the image: replace the man's grey three-piece suit with a rust-orange corduroy chore jacket over a cream ribbed turtleneck, loose olive cargo trousers and chunky white leather sneakers. Give him a short, neatly trimmed beard and round wire-rimmed glasses, and add a small brown leather crossbody bag resting on his left hip. Keep his face, pose, position, scale and the camera angle exactly the same. Keep the Art Deco hall, the checkered terrazzo floor, the staircase and the soft morning light unchanged, and let the new clothes catch that same warm light with natural folds and shadows.",
"resolution": "2K",
"response_modalities": "IMAGE",
"seed": 42,
"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": "MINIMAL"
},
"_meta": {
"title": "Nano Banana 2"
}
}
})
workflow.set_input("16", "image", client.assets.from_url("https://comfy.org/catalog-media/1cb5997d-c88d-4813-9be0-ee17d4755edd/1fc6a629-6271-4038-bf60-78ffc5667316"))
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)
for output in job.get_outputs("9"):
output.to_file(output.name)