# 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": "LongCat-Image-Edit",
"images": [
"22:8",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"13": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"17": {
"class_type": "ImageCompare",
"inputs": {
"compare_view": {
"__value__": [
"",
""
]
},
"image_a": [
"13",
0
],
"image_b": [
"22:8",
0
]
},
"_meta": {
"title": "Compare Images"
}
},
"22:1": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "longcat_image_edit_bf16.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"22:11": {
"class_type": "FluxGuidance",
"inputs": {
"conditioning": [
"22:4",
0
],
"guidance": 4.5
},
"_meta": {
"title": "FluxGuidance"
}
},
"22:12": {
"class_type": "FluxGuidance",
"inputs": {
"conditioning": [
"22:5",
0
],
"guidance": 4.5
},
"_meta": {
"title": "FluxGuidance"
}
},
"22:14": {
"class_type": "ImageScaleToTotalPixels",
"inputs": {
"image": [
"13",
0
],
"megapixels": 1,
"resolution_steps": 16,
"upscale_method": "lanczos"
},
"_meta": {
"title": "Scale Image to Total Pixels"
}
},
"22:15": {
"class_type": "FluxKontextMultiReferenceLatentMethod",
"inputs": {
"conditioning": [
"22:11",
0
],
"reference_latents_method": "index"
},
"_meta": {
"title": "Edit Model Reference Method"
}
},
"22:16": {
"class_type": "FluxKontextMultiReferenceLatentMethod",
"inputs": {
"conditioning": [
"22:12",
0
],
"reference_latents_method": "index"
},
"_meta": {
"title": "Edit Model Reference Method"
}
},
"22:2": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
"device": "default",
"type": "longcat_image"
},
"_meta": {
"title": "Load CLIP"
}
},
"22:3": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "ae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"22:4": {
"class_type": "TextEncodeQwenImageEdit",
"inputs": {
"clip": [
"22:2",
0
],
"image": [
"22:14",
0
],
"prompt": "Change the scene to a stormy late evening just after heavy rain. The sky turns dark slate grey with a narrow band of orange sunset glow low on the horizon, and the cobblestone quay is wet and glossy, reflecting warm light from every window and from old iron lanterns that are now lit along the waterfront. Puddles gather between the stones, the canvas awnings are darkened by rain, and the turquoise water becomes deep teal and slightly choppy, with the small wooden boat rocking at its mooring. Keep the buildings, shutters, plant pots, the quay wall and the camera viewpoint exactly the same; only the weather, time of day and lighting change.",
"vae": [
"22:3",
0
]
},
"_meta": {
"title": "Text Encode Qwen Image Edit"
}
},
"22:5": {
"class_type": "TextEncodeQwenImageEdit",
"inputs": {
"clip": [
"22:2",
0
],
"image": [
"22:14",
0
],
"prompt": "",
"vae": [
"22:3",
0
]
},
"_meta": {
"title": "Text Encode Qwen Image Edit"
}
},
"22:6": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"22:14",
0
],
"vae": [
"22:3",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"22:7": {
"class_type": "KSampler",
"inputs": {
"cfg": 4.5,
"denoise": 1,
"latent_image": [
"22:6",
0
],
"model": [
"22:1",
0
],
"negative": [
"22:16",
0
],
"positive": [
"22:15",
0
],
"sampler_name": "euler",
"scheduler": "simple",
"seed": 43,
"steps": 50
},
"_meta": {
"title": "KSampler"
}
},
"22:8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"22:7",
0
],
"vae": [
"22:3",
0
]
},
"_meta": {
"title": "VAE Decode"
}
}
})
workflow.set_input("13", "image", client.assets.from_url("https://comfy.org/catalog-media/ef174218-4940-469c-89a0-587539944cad/3f7cee46-6364-4c96-a07c-443bb8f618f9"))
# 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)