# 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({
"10": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_subject_FILENAME"
},
"_meta": {
"title": "Load Subject Image"
}
},
"11": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_product_FILENAME"
},
"_meta": {
"title": "Load Product Image"
}
},
"15": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"17",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"17": {
"class_type": "GeminiImage2Node",
"inputs": {
"aspect_ratio": "auto",
"images": [
"18:19",
0
],
"model": "gemini-3-pro-image-preview",
"prompt": "Swap the product the woman is holding in image 1 with the product in image 2. Replace the small amber dropper bottle in her hand with the black faceted stone jar with the chrome lid from image 2, held up beside her cheek the same way, at a believable size for a small skincare jar, with her fingers wrapped naturally around it. Keep her face, smile, hair, grey knit sweater, the kitchen, the rainy loft windows and the warm pendant light exactly as they are, and match the jar's reflections and shadows to the lighting of the room so it looks like a casual product review photo.",
"resolution": "2K",
"response_modalities": "IMAGE+TEXT",
"seed": 467727850770885,
"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)"
}
},
"18:12": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"10",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"18:13": {
"class_type": "ResizeAndPadImage",
"inputs": {
"image": [
"11",
0
],
"interpolation": "area",
"padding_color": "white",
"target_height": [
"18:12",
1
],
"target_width": [
"18:12",
0
]
},
"_meta": {
"title": "Resize And Pad Image"
}
},
"18:19": {
"class_type": "BatchImagesNode",
"inputs": {
"images.image0": [
"10",
0
],
"images.image1": [
"18:13",
0
]
},
"_meta": {
"title": "Batch Images"
}
}
})
workflow.set_input("11", "image", client.assets.from_url("https://comfy.org/catalog-media/7c26aba1-401b-47fb-9d60-0aef1db0c7f5/b3eb27b2-9c68-4759-ae27-90ce58074375"))
workflow.set_input("10", "image", client.assets.from_url("https://comfy.org/catalog-media/e300b50e-505a-4334-b952-a8c2bd2b7abe/292f71b8-d912-4cc3-b207-9493584eb367"))
# 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("15"):
output.to_file(output.name)