← Back to blog

Generate Images with ComfyUI Using gen.py: Free Python Script for AI Agents

September 3, 2026 · Eric · 8 min read

If you run ComfyUI on your own machine, you know the rhythm: build or paste a workflow graph, wire the sampler, pick a checkpoint, queue it, wait, and then hunt through the output folder. It is powerful — and it is exactly the kind of repetitive, deterministic work that an AI agent should be doing for you. The problem is that a coding agent usually tries to "reason" its way through ComfyUI's REST API every single time, drafting a fresh node graph in JSON and burning thousands of tokens on something that never changes.

gen.py is a one-file Python Blueprint from Agent Media Tools that collapses that whole flow into a single command: you pass a prompt, it builds the standard txt2img workflow, queues it on your ComfyUI server, waits for completion, and downloads the finished image into a local output folder. It is a free download — no account, no hosted GPU, no per-image credits — and this post walks through what the script actually does, how to grab it, and how to call it from curl, Python, or an agent loop.

What gen.py actually does

The script (v1.2.1 at the time of writing, about 13 KB and 400 lines) is a thin client for ComfyUI's standard HTTP API. Reading through it, the flow is:

  1. Auto-detect the server. It pings GET /system_stats on the primary URL (default http://127.0.0.1:8188), then falls back to http://localhost:8188 if that fails.
  2. Build the workflow. It assembles a fixed txt2img graph with standard node IDs: CheckpointLoaderSimple → two CLIPTextEncode nodes (positive and negative) → EmptyLatentImage → KSampler → VAEDecode → SaveImage.
  3. Queue and poll. It POSTs the graph to /prompt with a fresh client_id, then polls GET /history/{prompt_id} once per second until the job finishes (300-second timeout).
  4. Download the result. It reads the output image list from the history response and fetches each file through GET /view?filename=...&subfolder=...&type=output, saving PNGs to ./outputs/.

Defaults are tuned for speed rather than perfection: 30 sampling steps, CFG 7.0, 1024×1024, Euler sampler with the normal scheduler. The default checkpoint name is animeModel.safetensors, so on first run you will almost certainly want --model to point at a checkpoint that actually exists on your server. A sensible negative prompt ships as the default, and every run prints progress to stderr while writing a structured JSON result to stdout — which is what makes the script easy for an agent to consume.

Step 1: Download the script (free, no signup)

Blueprint downloads are served from a public endpoint that requires no account and no API key. One command grabs the file:

curl -fL -o gen.py "https://agentmediatools.com/api/shop/download/gen.py"
python3 gen.py --help

The equivalent Python download looks like this:

import requests

url = "https://agentmediatools.com/api/shop/download/gen.py"
r = requests.get(url, timeout=30)
r.raise_for_status()
open("gen.py", "wb").write(r.content)
print(f"Saved gen.py ({len(r.content):,} bytes)")

Agents that want to discover what is available before downloading can hit the public catalog — this is the same endpoint behind the MCP list_shop_items tool:

curl -s "https://agentmediatools.com/api/shop/items" \
  | python3 -c "import json,sys; d=json.load(sys.stdin); print([s['id'] for s in d['scripts']])"

Each entry in the response includes an id (for example gen), a filename, a description, dependency hints, and a download_url. The API explicitly notes that Blueprint downloads require no account or purchase — gen.py, gen-sd.py, web-scrape.py, deliver.py, and the rest of the library are all free to fetch this way.

Step 2: Point it at your ComfyUI

The script assumes ComfyUI is already running somewhere you control — it is a local workflow tool, not a hosted render farm. If your instance is not on the default localhost port, set the URL explicitly:

export COMFY_URL="http://192.168.1.50:8188"
python3 gen.py --prompt "neon city street at night, rain, cinematic" \
  --model "dreamshaperXL"

Everything is configurable through flags or environment variables:

Step 3: Generate, one line at a time or in a batch

Basic single image:

python3 gen.py --prompt "1girl, blue hair, sunset, masterpiece" \
  --width 1024 --height 1024 --steps 30

Four variations of the same idea with a fixed seed so you can compare:

python3 gen.py --prompt "mossy forest, god rays" --count 4 --seed 12345

For pipelines that need many different prompts, read them from stdin, one per line:

printf '%s\n' \
  "cyberpunk alley, neon signs" \
  "abandoned space station, volumetric light" \
  "tiny dragon on a desk, macro" | python3 gen.py --prompts-from-stdin

Because each run returns JSON on stdout, the script slots into automation without scraping logs:

{"results": [{"success": true, "outputs": ["outputs/gen_20260903_142301.png"], "seed": 842913, "model": "dreamshaperXL"}]}

Why this beats making your agent "figure out" ComfyUI

There are two ways an agent can talk to ComfyUI. The expensive way is to have the model improvise a workflow JSON on every call — teaching it the node schema, babysitting malformed graphs, and paying for thousands of tokens of trial and error. The cheap way is to give the agent one deterministic command that already encodes the correct workflow. The script's own header estimates this at 3,000–5,000 tokens saved per image versus having an LLM plan the workflow by hand, which lines up with what we see in practice: the workflow never changes, so the tokens should not either.

That makes gen.py a natural fit for agent tool use. An agent can download it once, discover it later through list_shop_items if it forgets, and then treat python3 gen.py --prompt "..." as a black-box tool that returns structured output — no SDK to install, no API key to rotate, no rate limit to hit on your own GPU.

Honest boundaries

A few things worth knowing before you adopt it:

If you already run ComfyUI, the fastest way to test the idea is also the simplest: download gen.py, point it at your server, and generate an image from your terminal in the next sixty seconds. Once it works once, hand the same command to your agent and let it start producing.

Try it in the Toolbox

Browse the whole Blueprint library — image generators, scrapers, context tools, and delivery scripts — and download any of them in one click.

Open Blueprints