Skip to Content
FeaturesModulesLLM IntegrationImage Generation

Image Generation

Generate images using DALL-E, FLUX, and other models with quality presets.

Quick Start

from django_cfg.modules.django_llm.features.image_gen import ImageGenClient client = ImageGenClient() response = client.generate( prompt="A sunset over mountains, photorealistic", model_quality="best", size="1024x1024" ) print(response.first_url) print(f"Cost: ${response.cost_usd:.4f}")

ImageGenClient

Configuration

client = ImageGenClient( api_key="sk-or-v1-...", # Optional, auto-detected )

Basic Generation

response = client.generate( prompt="A futuristic city at night", model_quality="balanced", size="1024x1024", quality="hd", # standard or hd style="vivid" # vivid or natural ) # Access results print(response.first_url) # First image URL print(response.count) # Number of images print(f"Cost: ${response.cost_usd:.4f}")

Quick Generation

# Returns URL directly url = client.generate_quick("A cute cat wearing a hat")

Multiple Images

response = client.generate( prompt="Abstract art", n=4, # Generate 4 images size="1024x1024" ) for img in response.images: print(img.url)

Quality Presets

PresetModelBest For
fastAuto-selectQuick prototypes
balancedDALL-E 3General use
bestFLUX ProHighest quality
# Fast - cheapest response = client.generate( prompt="...", model_quality="fast" ) # Balanced - good quality/cost ratio response = client.generate( prompt="...", model_quality="balanced" ) # Best - highest quality response = client.generate( prompt="...", model_quality="best" )

Sizes

Supported sizes:

  • 256x256 - Small, fast
  • 512x512 - Medium
  • 1024x1024 - Standard (default)
  • 1792x1024 - Wide landscape
  • 1024x1792 - Tall portrait
# Landscape response = client.generate( prompt="Mountain panorama", size="1792x1024" ) # Portrait response = client.generate( prompt="Standing figure", size="1024x1792" )

Quality Options

Quality

  • standard - Default quality
  • hd - Higher detail, more expensive

Style

  • vivid - More dramatic, vibrant (default)
  • natural - More realistic, muted
response = client.generate( prompt="Product photo", quality="hd", style="natural" )

Async Generation

response = await client.agenerate( prompt="Abstract art", size="1024x1024" )

Response Model

from django_cfg.modules.django_llm.features.image_gen import ( ImageGenResponse, GeneratedImage, ) # ImageGenResponse properties response.first_url # First image URL response.images # List[GeneratedImage] response.count # Number of images response.cost_usd # Total cost response.model # Model used response.prompt # Original prompt # GeneratedImage properties img = response.images[0] img.url # Image URL img.b64_json # Base64 data (if requested) img.revised_prompt # Model's revised prompt

Request Model

from django_cfg.modules.django_llm.features.image_gen import ImageGenRequest request = ImageGenRequest( prompt="A sunset", n=2, size="1024x1024", quality="hd", style="vivid" )

Cost Tracking

Costs are automatically calculated:

response = client.generate(prompt="...") print(f"Cost: ${response.cost_usd:.4f}")

Typical costs (varies by provider):

  • DALL-E 3 Standard 1024x1024: ~$0.04
  • DALL-E 3 HD 1024x1024: ~$0.08
  • FLUX Pro: ~$0.05-0.10

Best Practices

Prompt Engineering

# Be specific response = client.generate( prompt="A red sports car on a coastal road at sunset, " "dramatic lighting, photorealistic, 4K quality" ) # Include style hints response = client.generate( prompt="Portrait in the style of Renaissance painting, " "soft lighting, oil on canvas texture" )

Error Handling

try: response = client.generate(prompt="...") except Exception as e: print(f"Generation failed: {e}")

Caching Considerations

Generated images have unique URLs. Consider:

  • Downloading and storing locally
  • Using image cache for responses
  • Implementing your own deduplication

TAGS: image-generation, dall-e, flux, ai-images

Last updated on