Programmatic Image Generation Made Easy: A Comprehensive API-Ready Workflow

CN
ComfyUI.org
2025-06-11 10:55:23

1. Workflow Overview

mbru25vyjcip05bfc1图片压缩f0cdfea911bf2ac4733c901ea36e92f9bcde88af60fb192ea557a1a93c603748.png

This JSON file defines a standard API-ready Stable Diffusion workflow for programmatic image generation. It produces 512x512 images from text prompts with built-in output saving functionality.

2. Core Components

Node

Type

Key Function

CheckpointLoaderSimple

Model Loader

Loads AWPainting_v1.3 (artistic style model)

EmptyLatentImage

Latent Space

Creates 512x512 blank latent image

CLIPTextEncode (x2)

Text Encoder

Processes positive/negative prompts

KSampler

Sampler

Controls generation (20 steps, Euler)

VAEDecode

Decoder

Converts latent to pixel image

SaveImage

Output

Saves final image

3. Critical Parameters

  • Resolution: 512x512

  • Prompts:

    • Positive: "beautiful scenery nature glass bottle landscape, purple galaxy bottle"

    • Negative: "text, watermark"

  • Sampling: Euler method, 20 steps, CFG=8

  • Seed: 802908303372950

4. API Integration Features

  1. Structured Input:(Json)

{
  "prompt": "beautiful scenery...",
  "negative_prompt": "text, watermark",
  "width": 512,
  "height": 512,
  "steps": 20,
  "cfg_scale": 8
}
  1. Extensibility:

  • Model path can be modified (Node 4 widgets_values)

  • Dynamic seed adjustment (Node 3 widgets_values[0])

  1. Output Handling:

  • Auto-saves to ComfyUI output directory

  • Returns base64 or file path

5. Recommended API Wrapper(Python)

def generate_image(prompt, negative_prompt="", size=(512,512)):
    payload = {
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "width": size[0],
        "height": size[1]
    }
    response = requests.post("http://comfyui-server/api/v1/generate", 
                           json=payload)
    return response.json()["output_url"]

6. Performance Optimization

  1. For high-frequency calls:

  • Preload models (keep CheckpointLoaderSimple resident)

  • Implement batch processing

  1. Parameter tuning:

  • Reduce steps to 15 for faster generation

  • Use "dpmpp_2m" sampler for quality/speed balance

7. Technical Notes

  • Dependencies: Requires ComfyUI server with AWPainting model

  • Throughput: ~2.5 sec/image on RTX 3090 (20 steps)

  • Scalability: Stateless design supports horizontal scaling

This workflow is API-optimized with:

  • Minimal node complexity

  • Clear parameter mapping

  • No external dependencies

  • Deterministic output control

For production deployment, consider adding:

  • Request queuing system

  • Async response handling

  • Dynamic model switching