# 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({
"282": {
"class_type": "ImageListToImageBatch",
"inputs": {
"images": [
"347",
0
]
},
"_meta": {
"title": "Image List to Image Batch"
}
},
"287": {
"class_type": "ImageBatchRepeatInterleaving",
"inputs": {
"images": [
"282",
0
],
"repeats": [
"321",
0
]
},
"_meta": {
"title": "ImageBatchRepeatInterleaving"
}
},
"296": {
"class_type": "RebatchImages",
"inputs": {
"batch_size": 1,
"images": [
"414:415",
0
]
},
"_meta": {
"title": "Rebatch Images"
}
},
"310": {
"class_type": "LoadImage",
"inputs": {
"image": "4aec5ec351a7828d3d2a2b9e79535c3e97ab346d2f1877de80313d955e470d5f.png"
},
"_meta": {
"title": "INPUT STYLE REFERENCE"
}
},
"321": {
"class_type": "PrimitiveInt",
"inputs": {
"value": 5
},
"_meta": {
"title": "SELECT EVERY NTH FRAME"
}
},
"331": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"401",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"337": {
"class_type": "CreateVideo",
"inputs": {
"bit_depth": "auto",
"codec": "none",
"color_space": "sRGB",
"fps": [
"410",
2
],
"images": [
"407",
0
]
},
"_meta": {
"title": "Create Video"
}
},
"338": {
"class_type": "SaveVideo",
"inputs": {
"filename_prefix": "video/CollageImage",
"format": "mp4",
"format.codec": "h264",
"video": [
"337",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"340": {
"class_type": "VHS_SelectEveryNthImage",
"inputs": {
"images": [
"410",
0
],
"select_every_nth": [
"321",
0
],
"skip_first_images": 0
},
"_meta": {
"title": "Select Every Nth Image 🎥🅥🅗🅢"
}
},
"347": {
"class_type": "ResizeImageMaskNode",
"inputs": {
"input": [
"401",
0
],
"resize_type": "scale to multiple",
"resize_type.multiple": 2,
"scale_method": "lanczos"
},
"_meta": {
"title": "Resize Image/Mask"
}
},
"401": {
"class_type": "OpenAIGPTImageNodeV2",
"inputs": {
"model": "gpt-image-2",
"model.background": "auto",
"model.custom_height": [
"405",
1
],
"model.custom_width": [
"405",
0
],
"model.images.image_1": [
"296",
0
],
"model.images.image_2": [
"310",
0
],
"model.quality": "low",
"model.size": "Custom",
"n": 1,
"prompt": "Stylize the first image in the style of the second image.",
"seed": 79065436
},
"_meta": {
"title": "OpenAI GPT Image 2.5"
}
},
"405": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"423",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"407": {
"class_type": "ImageRemoveAlpha+",
"inputs": {
"image": [
"287",
0
]
},
"_meta": {
"title": "🔧 Image Remove Alpha"
}
},
"409": {
"class_type": "LoadVideo",
"inputs": {
"file": "97528b37a516964dc2c1b9843ce2e0e049bcbddfdeefe985dfe8b2c197b18679.mp4"
},
"_meta": {
"title": "INPUT VIDEO"
}
},
"410": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"413",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"413": {
"class_type": "Video Slice",
"inputs": {
"duration": 0.5,
"start_time": 0.5,
"strict_duration": False,
"video": [
"409",
0
]
},
"_meta": {
"title": "TRIM VIDEO"
}
},
"423": {
"class_type": "ResizeImageMaskNode",
"inputs": {
"input": [
"340",
0
],
"resize_type": "scale to multiple",
"resize_type.multiple": 16,
"scale_method": "lanczos"
},
"_meta": {
"title": "Resize Image/Mask"
}
},
"414:13": {
"class_type": "RemoveBackground",
"inputs": {
"bg_removal_model": [
"414:14",
0
],
"image": [
"423",
0
]
},
"_meta": {
"title": "Remove Background"
}
},
"414:14": {
"class_type": "LoadBackgroundRemovalModel",
"inputs": {
"bg_removal_name": "birefnet.safetensors"
},
"_meta": {
"title": "Load Background Removal Model"
}
},
"414:15": {
"class_type": "InvertMask",
"inputs": {
"mask": [
"414:13",
0
]
},
"_meta": {
"title": "Invert Mask"
}
},
"414:415": {
"class_type": "ImageCompositeMasked",
"inputs": {
"destination": [
"423",
0
],
"mask": [
"414:15",
0
],
"resize_source": True,
"source": [
"414:418",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "Image Composite Masked"
}
},
"414:418": {
"class_type": "ImageConstant",
"inputs": {
"batch_size": 1,
"blue": 0,
"green": 0,
"height": 512,
"red": 0,
"width": 512
},
"_meta": {
"title": "Image Constant Color (RGB)"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)