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
| Preset | Model | Best For |
|---|---|---|
fast | Auto-select | Quick prototypes |
balanced | DALL-E 3 | General use |
best | FLUX Pro | Highest 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, fast512x512- Medium1024x1024- Standard (default)1792x1024- Wide landscape1024x1792- 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 qualityhd- 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 promptRequest 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
Related
TAGS: image-generation, dall-e, flux, ai-images
Last updated on