Lý thuyết
35 phút
Bài 2/3

DALL-E 3 Mastery

Master DALL-E 3 API - từ basics đến advanced techniques

🖼️ 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

FeatureDALL-E 2DALL-E 3
Text in imagesPoorExcellent
Prompt adherenceGoodVery good
QualityGoodExcellent
Sizes256, 512, 10241024, 1792x1024
Price$0.016-0.020$0.040-0.120

API Basics

Generate Images

Python
1from openai import OpenAI
2
3client = OpenAI()
4
5def generate_dalle3(prompt, size="1024x1024", quality="standard", style="vivid"):
6 """
7 Generate image with DALL-E 3
8
9 Args:
10 prompt: Image description
11 size: 1024x1024, 1792x1024, 1024x1792
12 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=1
22 )
23
24 return {
25 "url": response.data[0].url,
26 "revised_prompt": response.data[0].revised_prompt
27 }
28
29# Example
30result = 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 celebration
3Setting: Living room with red decorations, mai flowers (yellow apricot blossoms)
4People: Three generations of family sitting together
5Food: Bánh chưng (square sticky rice cake), mt (candied fruits)
6Style: Warm, nostalgic, photorealistic
7Lighting: Soft indoor lighting with golden tones
8Composition: Wide shot showing entire room
9"""

2. Character Consistency

Describe characters in detail:

Python
1character = """
2Character: A young Vietnamese woman named Lan
3- Age: mid-20s
4- Hair: Long black hair in a ponytail
5- Outfit: Modern áo dài in light blue
6- Expression: Warm, confident smile
7- Accessories: Small jade earrings
8"""
9
10scene1 = 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}
8
9base_prompt = "A Vietnamese market scene with vendors selling fruits"
10
11for 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 asyncio
2from concurrent.futures import ThreadPoolExecutor
3
4def 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 results
32
33# Usage
34base = "A cozy reading nook"
35variations = [
36 "Modern minimalist style",
37 "Victorian vintage style",
38 "Japanese zen style",
39 "Bohemian eclectic style"
40]
41
42results = generate_variations(base, variations)

Cost Optimization

Pricing

SizeQualityPrice
1024x1024Standard$0.040
1024x1024HD$0.080
1792x1024Standard$0.080
1792x1024HD$0.120

Strategies

Python
1def smart_generate(prompt, need_hd=False, need_wide=False):
2 """Cost-effective generation"""
3
4 # Start with standard for iterations
5 if not need_hd:
6 quality = "standard"
7 else:
8 quality = "hd"
9
10 # Only use wide when needed
11 if need_wide:
12 size = "1792x1024"
13 else:
14 size = "1024x1024"
15
16 return generate_dalle3(prompt, size=size, quality=quality)
17
18# Iteration workflow
19# 1. Generate standard to test prompt
20test = smart_generate("my prompt", need_hd=False)
21
22# 2. If good, generate HD final
23final = smart_generate("my prompt", need_hd=True)

Error Handling

Python
1from openai import OpenAI, BadRequestError, RateLimitError
2
3def 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 violation
12 print(f"Content policy issue: {e}")
13 return {"error": "Content policy violation", "prompt": prompt}
14
15 except RateLimitError:
16 # Rate limited
17 wait = 2 ** attempt
18 print(f"Rate limited, waiting {wait}s...")
19 import time
20 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 bottle
3- Material: Matte black stainless steel
4- Text on bottle: "HYDRATE"
5- Background: Clean white studio
6- Lighting: Professional product lighting with soft shadows
7- Style: Commercial product photo for e-commerce
8"""

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 yoga
6- Text space: Leave room at bottom for quote
7- Style: Modern, clean, Instagram-worthy
8- Aspect: Square 1:1
9"""

3. Blog Illustrations

Python
1blog_prompt = """
2Blog header illustration for article about AI in education:
3- Concept: Students learning with AI assistant
4- Style: Friendly flat illustration
5- Colors: Soft blues and greens
6- Elements: Laptop, robot character, books, lightbulb
7- Mood: Optimistic, innovative, approachable
8"""

Best Practices

DALL-E 3 Tips
  1. Be descriptive - DALL-E 3 handles long prompts well
  2. Specify style explicitly
  3. Include composition details
  4. Use HD for final outputs only
  5. Check revised prompts for insights
  6. Avoid banned content - review policies

Bài tập thực hành

Hands-on Exercise

Create Image Set:

  1. Chọn một theme (product, brand, story)
  2. Generate 5-10 images với consistent style
  3. Experiment với:
    • Quality settings
    • Style variations
    • Composition changes
  4. 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.