はじめに
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 | HD | ~$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
概要
| 特長 | ダルイー3 | ダルイー2 |
|---|---|---|
| 品質 | 素晴らしい | 良い |
| 迅速な理解 | 自然言語 | キーワード |
| 編集 | いいえ | はい (マスク) |
| バリエーション | いいえ | はい |
| 最大解像度 | 1792×1024 | 1024×1024 |
| 即時書き換え | 自動 | いいえ |
📌 次の記事: Midjourney、Flux、新興モデル — プラットフォームの比較。