🖼️ DALL-E 3 Mastery
DALL-E 3 là model mạnh nhất của OpenAI cho image generation. Bài này deep dive vào API và techniques.
DALL-E 3 vs DALL-E 2
| Feature | DALL-E 2 | DALL-E 3 |
|---|---|---|
| Text in images | Poor | Excellent |
| Prompt adherence | Good | Very good |
| Quality | Good | Excellent |
| Sizes | 256, 512, 1024 | 1024, 1792x1024 |
| Price | $0.016-0.020 | $0.040-0.120 |
API Basics
Generate Images
Python
1from openai import OpenAI23client = OpenAI()45def generate_dalle3(prompt, size="1024x1024", quality="standard", style="vivid"):6 """7 Generate image with DALL-E 38 9 Args:10 prompt: Image description11 size: 1024x1024, 1792x1024, 1024x179212 quality: standard ($0.04) or hd ($0.08)13 style: vivid (dramatic) or natural (realistic)14 """15 response = client.images.generate(16 model="dall-e-3",17 prompt=prompt,18 size=size,19 quality=quality,20 style=style,21 n=1 # DALL-E 3 only supports n=122 )23 24 return {25 "url": response.data[0].url,26 "revised_prompt": response.data[0].revised_prompt27 }2829# Example30result = generate_dalle3(31 prompt="A modern Vietnamese office with people working on laptops, minimalist design",32 quality="hd",33 style="natural"34)35print(f"Image: {result['url']}")36print(f"Revised: {result['revised_prompt']}")Understanding Revised Prompts
DALL-E 3 tự động expand prompts:
Text
1Input: "a cat"2Revised: "A fluffy orange tabby cat sitting on a windowsill, 3 looking out at a sunny garden, soft natural lighting"Tip
Đọc revised prompt để understand DALL-E interpretation và improve future prompts.
Advanced Prompting
1. Detailed Scene Description
Python
1prompt = """2Scene: A traditional Vietnamese Tết celebration3Setting: Living room with red decorations, mai flowers (yellow apricot blossoms)4People: Three generations of family sitting together5Food: Bánh chưng (square sticky rice cake), mứt (candied fruits)6Style: Warm, nostalgic, photorealistic7Lighting: Soft indoor lighting with golden tones8Composition: Wide shot showing entire room9"""2. Character Consistency
Describe characters in detail:
Python
1character = """2Character: A young Vietnamese woman named Lan3- Age: mid-20s4- Hair: Long black hair in a ponytail5- Outfit: Modern áo dài in light blue6- Expression: Warm, confident smile7- Accessories: Small jade earrings8"""910scene1 = f"{character}\nScene: Standing in a coffee shop, ordering"11scene2 = f"{character}\nScene: Working at a laptop in a co-working space"12scene3 = f"{character}\nScene: Walking through Hội An ancient town"3. Style References
Python
1styles = {2 "studio_ghibli": "in the style of Studio Ghibli anime, soft watercolors, whimsical",3 "cyberpunk": "cyberpunk style, neon lights, futuristic, dark atmosphere",4 "vintage": "vintage photography, 1960s Vietnam, grainy film texture",5 "minimalist": "minimalist illustration, flat design, limited color palette",6 "impressionist": "impressionist painting style, visible brushstrokes, light-filled"7}89base_prompt = "A Vietnamese market scene with vendors selling fruits"1011for style_name, style_desc in styles.items():12 full_prompt = f"{base_prompt}, {style_desc}"13 result = generate_dalle3(full_prompt)Batch Generation
Generate Multiple Variations
Python
1import asyncio2from concurrent.futures import ThreadPoolExecutor34def generate_variations(base_prompt, variations, max_workers=3):5 """Generate multiple images with variations"""6 results = []7 8 with ThreadPoolExecutor(max_workers=max_workers) as executor:9 futures = []10 for i, variation in enumerate(variations):11 full_prompt = f"{base_prompt}. {variation}"12 future = executor.submit(generate_dalle3, full_prompt)13 futures.append((i, variation, future))14 15 for i, variation, future in futures:16 try:17 result = future.result()18 results.append({19 "index": i,20 "variation": variation,21 "url": result["url"],22 "revised_prompt": result["revised_prompt"]23 })24 except Exception as e:25 results.append({26 "index": i,27 "variation": variation,28 "error": str(e)29 })30 31 return results3233# Usage34base = "A cozy reading nook"35variations = [36 "Modern minimalist style",37 "Victorian vintage style",38 "Japanese zen style",39 "Bohemian eclectic style"40]4142results = generate_variations(base, variations)Cost Optimization
Pricing
| Size | Quality | Price |
|---|---|---|
| 1024x1024 | Standard | $0.040 |
| 1024x1024 | HD | $0.080 |
| 1792x1024 | Standard | $0.080 |
| 1792x1024 | HD | $0.120 |
Strategies
Python
1def smart_generate(prompt, need_hd=False, need_wide=False):2 """Cost-effective generation"""3 4 # Start with standard for iterations5 if not need_hd:6 quality = "standard"7 else:8 quality = "hd"9 10 # Only use wide when needed11 if need_wide:12 size = "1792x1024"13 else:14 size = "1024x1024"15 16 return generate_dalle3(prompt, size=size, quality=quality)1718# Iteration workflow19# 1. Generate standard to test prompt20test = smart_generate("my prompt", need_hd=False)2122# 2. If good, generate HD final23final = smart_generate("my prompt", need_hd=True)Error Handling
Python
1from openai import OpenAI, BadRequestError, RateLimitError23def safe_generate(prompt, retries=3):4 """Generate with error handling"""5 6 for attempt in range(retries):7 try:8 return generate_dalle3(prompt)9 10 except BadRequestError as e:11 # Content policy violation12 print(f"Content policy issue: {e}")13 return {"error": "Content policy violation", "prompt": prompt}14 15 except RateLimitError:16 # Rate limited17 wait = 2 ** attempt18 print(f"Rate limited, waiting {wait}s...")19 import time20 time.sleep(wait)21 22 except Exception as e:23 print(f"Error: {e}")24 if attempt == retries - 1:25 return {"error": str(e)}26 27 return {"error": "Max retries exceeded"}Practical Applications
1. Product Mockups
Python
1product_prompt = """2Product photography: A sleek water bottle3- Material: Matte black stainless steel4- Text on bottle: "HYDRATE"5- Background: Clean white studio6- Lighting: Professional product lighting with soft shadows7- Style: Commercial product photo for e-commerce8"""2. Social Media Graphics
Python
1social_prompt = """2Instagram post graphic:3- Theme: "Morning Motivation Monday"4- Background: Gradient sunrise colors (pink to orange)5- Elements: Silhouette of person doing yoga6- Text space: Leave room at bottom for quote7- Style: Modern, clean, Instagram-worthy8- Aspect: Square 1:19"""3. Blog Illustrations
Python
1blog_prompt = """2Blog header illustration for article about AI in education:3- Concept: Students learning with AI assistant4- Style: Friendly flat illustration5- Colors: Soft blues and greens6- Elements: Laptop, robot character, books, lightbulb7- Mood: Optimistic, innovative, approachable8"""Best Practices
DALL-E 3 Tips
- Be descriptive - DALL-E 3 handles long prompts well
- Specify style explicitly
- Include composition details
- Use HD for final outputs only
- Check revised prompts for insights
- Avoid banned content - review policies
Bài tập thực hành
Hands-on Exercise
Create Image Set:
- Chọn một theme (product, brand, story)
- Generate 5-10 images với consistent style
- Experiment với:
- Quality settings
- Style variations
- Composition changes
- Calculate total cost
Target: Cohesive image set cho một project cụ thể
Tiếp theo
Bài tiếp theo: Stable Diffusion - Local image generation với full control.
