簡介
DALL-E 3 是 OpenAI 最強大的圖像生成模型 - 與 ChatGPT 深度集成,很好地理解自然提示,並具有可用於生產的 API。本文指導如何將 DALL-E 3 API 整合到實際應用程式中。
1.OpenAI 圖片 API
from openai import OpenAI
client = OpenAI()
# Text-to-Image
response = client.images.generate(
model="dall-e-3",
prompt="A white siamese cat sitting on a windowsill, watercolor painting",
size="1024x1024", # 1024x1024, 1024x1792, 1792x1024
quality="standard", # standard, hd
style="vivid", # vivid, natural
n=1,
)
image_url = response.data[0].url
revised_prompt = response.data[0].revised_prompt # DALL-E rewrites prompt
尺寸和定價
| 尺寸 | 品質 | 價格 |
|---|---|---|
| 1024×1024 | 標準 | ~$0.04 |
| 1024×1024 | 高清 | ~$0.08 |
| 1024×1792 | 標準 | ~$0.08 |
| 1792×1024 | 標準 | ~$0.08 |
2. 影像編輯 (DALL-E 2)
# Edit specific region of image
response = client.images.edit(
model="dall-e-2",
image=open("original.png", "rb"),
mask=open("mask.png", "rb"), # transparent area = edit zone
prompt="a golden retriever sitting naturally",
size="1024x1024",
n=1,
)
影像變化
# Generate variations of existing image
response = client.images.create_variation(
model="dall-e-2",
image=open("cat.png", "rb"),
size="1024x1024",
n=4, # 4 variations
)
3.下載並儲存映像
import httpx
from pathlib import Path
async def generate_and_save(prompt, output_path, client):
"""Generate image and save to disk"""
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="hd",
response_format="b64_json", # get base64 instead of URL
)
# From base64
import base64
image_data = base64.b64decode(response.data[0].b64_json)
Path(output_path).write_bytes(image_data)
return response.data[0].revised_prompt
# Or download from URL
def download_image(url, output_path):
response = httpx.get(url)
Path(output_path).write_bytes(response.content)
4. 提示 DALL-E 3 的最佳實踐
# DALL-E 3 hiểu ngôn ngữ tự nhiên tốt hơn SD
# Viết mô tả chi tiết như đang nói chuyện
# ❌ SD-style prompt (keyword spam)
bad_prompt = "cat, digital art, 4k, trending on artstation, highly detailed"
# ✅ DALL-E 3 prompt (natural description)
good_prompt = """
A fluffy white cat sitting on a velvet cushion in a cozy library.
The cat is wearing tiny round glasses and looking at an open book.
Warm golden afternoon light streams through tall windows.
Style: detailed watercolor illustration with soft colors.
"""
# DALL-E 3 tự động rewrite prompt → check revised_prompt
print(f"Original: {prompt}")
print(f"Revised: {response.data[0].revised_prompt}")
5. 整合模式-FastAPI 伺服器
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import OpenAI
import base64
app = FastAPI()
client = OpenAI()
class ImageRequest(BaseModel):
prompt: str
size: str = "1024x1024"
quality: str = "standard"
style: str = "vivid"
class ImageResponse(BaseModel):
image_url: str
revised_prompt: str
@app.post("/generate", response_model=ImageResponse)
async def generate_image(req: ImageRequest):
try:
response = client.images.generate(
model="dall-e-3",
prompt=req.prompt,
size=req.size,
quality=req.quality,
style=req.style,
)
return ImageResponse(
image_url=response.data[0].url,
revised_prompt=response.data[0].revised_prompt,
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
6. 速率限制與成本最佳化
import asyncio
from collections import deque
import time
class RateLimiter:
"""Simple rate limiter for API calls"""
def __init__(self, max_calls=5, period=60):
self.max_calls = max_calls
self.period = period
self.calls = deque()
async def acquire(self):
now = time.time()
while self.calls and self.calls[0] < now - self.period:
self.calls.popleft()
if len(self.calls) >= self.max_calls:
sleep_time = self.calls[0] + self.period - now
await asyncio.sleep(sleep_time)
self.calls.append(time.time())
# Usage
limiter = RateLimiter(max_calls=5, period=60)
async def safe_generate(prompt):
await limiter.acquire()
return client.images.generate(model="dall-e-3", prompt=prompt)
7. 安全與內容政策
# DALL-E 3 có built-in safety filters
# Tự động reject: violence, explicit content, real people
# Handle content policy errors
from openai import BadRequestError
try:
response = client.images.generate(
model="dall-e-3",
prompt=user_prompt,
)
except BadRequestError as e:
if "content_policy" in str(e):
print("Prompt vi phạm content policy")
elif "billing" in str(e):
print("Hết quota hoặc billing issue")
raise
總結
| 特點 | 達爾-E 3 | 達爾-E 2 |
|---|---|---|
| 品質 | 優 | 好 |
| 隨時了解 | 自然語言 | 關鍵字 |
| 編輯 | 沒有 | 是(面罩) |
| 變化 | 沒有 | 是的 |
| 最大解析度 | 1792×1024 | 1024×1024 |
| 及時重寫 | 汽車 | 沒有 |
📌 下一篇: Midjourney、Flux 和新興模型 — 比較平台。