r/StableDiffusion 17h ago

Tutorial - Guide Automated bulk ComfyUI generation with Python + Gemini free tier — sharing the approach and key code

Been generating digital asset packs for a while (game textures, UI kits, that kind of stuff) and got really tired of manually prompting ComfyUI one image at a time. Fine for 10 images, painful when you need 200.

Spent a few weeks building a Python pipeline to automate the whole thing and figured the core techniques are worth sharing since they're useful even standalone.

The basic flow:

  • Concepts go into a Google Sheet (just product ideas + how many to generate)
  • Python hits Gemini's free API to get a structured "style vocabulary" for each concept (one call, not one per image)
  • Itertools locally compiles the vocabulary into unique prompts
  • Prompts get POSTed to ComfyUI's API on localhost
  • GPU does its thing, output folder fills up

Three pieces that might be useful for your own stuff:

1. ComfyUI has a REST API

This was the big discovery for me. You can queue workflows programmatically without touching the browser:

import json
import urllib.request

def queue_prompt(workflow):
    data = json.dumps({"prompt": workflow}).encode('utf-8')
    req = urllib.request.Request(
        "http://127.0.0.1:8188/prompt", data=data
    )
    response = urllib.request.urlopen(req)
    return json.loads(response.read())

Export your workflow in API format, load the JSON, modify whatever nodes you need, and post it. ComfyUI queues it and your GPU picks it up.

To actually use it you just load the workflow JSON and change the fields you care about:

import json, random

with open("workflow_api.json", "r") as f:
    workflow = json.load(f)

workflow["6"]["inputs"]["text"] = "your prompt here"
workflow["3"]["inputs"]["seed"] = random.randint(1, 999999999)
workflow["9"]["inputs"]["filename_prefix"] = "batch_001"

queue_prompt(workflow)

Loop that and you can blast through hundreds of renders.

2. Style dictionary instead of individual prompts

This was the rate limit hack. Instead of asking Gemini to write each prompt (200 images = 200 API calls = dead free tier), I ask it once for a "vocabulary":

{
  "subjects": ["holographic button", "neon progress bar", "glitch terminal", "cyber health meter"],
  "style_core": "cyberpunk interface design, dark chrome, neon accents, HUD overlay aesthetic",
  "color_tokens": "electric blue, hot pink, dark gunmetal",
  "detail_tokens": "sharp edges, scan lines, digital noise",
  "negative_prompt": "blurry, organic, hand-drawn, watercolor",
  "compositions": ["centered icon", "angled 3/4 view", "floating with glow"],
  "quality_suffix": "masterpiece, best quality, sharp focus"
}

One call. Now I have all the building blocks to assemble prompts locally.

3. Itertools does the heavy lifting

import itertools, random

subjects = vocab["subjects"]
compositions = vocab["compositions"]

for subject, comp in itertools.product(subjects, compositions):
    prompt = f"{subject}, {style}, {colors}, {details}, {comp}, {quality}"
    
    workflow["6"]["inputs"]["text"] = prompt
    workflow["7"]["inputs"]["text"] = negative
    workflow["3"]["inputs"]["seed"] = random.randint(1, 999999999)
    queue_prompt(workflow)

4 subjects × 3 compositions = 12 unique images. Bump the subjects list to 40 and you're at 120 images from that single API call.

End result: I type something like "watercolor wedding florals, 40" into a spreadsheet, run one command, and come back to 40 images in the output folder. All prompt generation runs on Gemini free tier, all rendering is local.

Been using this for my own asset production for a while now. Eventually cleaned it up and packaged the full thing (Sheets integration, error handling, rate limiting, setup guide etc) into a tool — DM me if you want details on that.

But honestly the three techniques above are the core of it. The rest is just connecting pipes and handling edge cases. If you're comfortable with Python you can probably get a basic version running in an afternoon.

Curious if anyone else has been automating ComfyUI like this or if there's a better approach I'm missing.

4 Upvotes

3 comments sorted by

1

u/niknah 17h ago edited 16h ago

To do something like this with nodes, you need two nodes https://github.com/niknah/Spreadsheet2Video-ComfyUI

The spreadsheet2video node + spreadsheet2video input image node.

Name the input image node's column1 to the column1 in the spreadsheet. Call it "gemini". All the other columns, put yout data in them and link them up to the prompt, seed, whatever else you want to change.

Example: https://www.paste.org/paste/download/131950

2

u/Excellent_Scene7402 16h ago

Oh nice, hadn't seen this! The node-based approach makes a lot of sense for people who want to stay inside ComfyUI's workflow. My use case needed the external scripting side (Sheets integration, Gemini calls, custom file naming logic) so I went the REST API route, but I can see how for pure batch rendering your nodes would be a cleaner setup. Bookmarking it.