# 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({
"12": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"13": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_2_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"14": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_3_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"15": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_4_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"16": {
"class_type": "ImageStitch",
"inputs": {
"direction": "right",
"image1": [
"12",
0
],
"image2": [
"13",
0
],
"match_image_size": True,
"spacing_color": "white",
"spacing_width": 0
},
"_meta": {
"title": "Stitch Images"
}
},
"17": {
"class_type": "ImageStitch",
"inputs": {
"direction": "right",
"image1": [
"14",
0
],
"image2": [
"15",
0
],
"match_image_size": True,
"spacing_color": "white",
"spacing_width": 0
},
"_meta": {
"title": "Stitch Images"
}
},
"18": {
"class_type": "ImageStitch",
"inputs": {
"direction": "down",
"image1": [
"16",
0
],
"image2": [
"17",
0
],
"match_image_size": True,
"spacing_color": "white",
"spacing_width": 0
},
"_meta": {
"title": "Stitch Images"
}
},
"19": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"21",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"21": {
"class_type": "ResizeImageMaskNode",
"inputs": {
"input": [
"18",
0
],
"resize_type": "scale dimensions",
"resize_type.crop": "center",
"resize_type.height": 2048,
"resize_type.width": 2048,
"scale_method": "lanczos"
},
"_meta": {
"title": "Resize Image/Mask"
}
}
})
# Replace each example.com URL with a public URL of your file.
workflow.set_input("12", "image", client.assets.from_url("https://example.com/replace-with-a-url/image"))
workflow.set_input("13", "image", client.assets.from_url("https://example.com/replace-with-a-url/image_2"))
workflow.set_input("14", "image", client.assets.from_url("https://example.com/replace-with-a-url/image_3"))
workflow.set_input("15", "image", client.assets.from_url("https://example.com/replace-with-a-url/image_4"))
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)