Local AI Local AI Advanced

Generate Images from Code

Batch-generate images via API and integrate into apps

66 of 66

Enable ComfyUI server mode

python main.py --listen --port 8188

Queue a prompt from Python

import requests, json

with open("workflow.json") as f:
    prompt = json.load(f)

# Update the positive prompt node (node IDs depend on your workflow)
prompt["3"]["inputs"]["text"] = "a cyberpunk cat wearing goggles"

r = requests.post("http://127.0.0.1:8188/prompt", json={"prompt": prompt})
print(r.json())

Wait for completion

ComfyUI is asynchronous. Poll the history endpoint:

import time

prompt_id = r.json()["prompt_id"]
while True:
    history = requests.get(f"http://127.0.0.1:8188/history/{prompt_id}").json()
    if history:
        break
    time.sleep(1)

Batch generation

Loop over a list of prompts and queue them:

prompts = ["a red car", "a blue car", "a green car"]
for p in prompts:
    prompt["3"]["inputs"]["text"] = p
    requests.post("http://127.0.0.1:8188/prompt", json={"prompt": prompt})

Use cases

  • Generate app assets and icons
  • Create synthetic training datasets
  • Produce thumbnails and marketing images
  • Build internal image-generation tools

Tip

Save your ComfyUI workflow JSON with the API format option so node IDs are stable.

Working out which model to run this on? See The Codex. Packaging it as a reusable skill? See The Armory.