图片生成 / dall-e-3
POST v1/images/generations根据提示词以及其他参数创建图片
官方文档:https://platform.openai.com/docs/api-reference/images/create
请求参数
Authorization
在 Header 添加参数 Authorization,其值为在 Bearer 之后拼接 Token。
Authorization: Bearer ******************Body 参数
Content-Type: application/json
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 必需 | 模型名称。用于指定图像生成的模型(例如 dall-e-2 或 dall-e-3)。 |
| quality | string | 必需 | 生成图片的质量。hd 选项会生成更细致和一致的图片。仅 dall-e-3 支持此参数。 |
| prompt | string | 必需 | 提示词。期望生成图片的文本描述。 • dall-e-2:最大长度 1000 字符 • dall-e-3:最大长度 4000 字符 提示:描述需具体详细,包含关键视觉元素、艺术风格及构图视角。 |
| n | integer | 必需 | 生成数量。要生成的图片数量(范围 1-10)。 • 注意:dall-e-3 仅支持 n=1。 |
| size | string | 必需 | 图片尺寸。 • dall-e-2:支持 256x256, 512x512, 1024x1024 • dall-e-3:支持 1024x1024 (正方形), 1792x1024 (横向), 1024x1792 (纵向) |
| style | string | 可选 | 图片风格。仅 dall-e-3 支持。 • vivid:超现实、戏剧性风格。 • natural:更自然、写实风格。 |
| user | string | 可选 | 用户标识。代表最终用户的唯一 ID,用于帮助 OpenAI 监控和检测滥用行为。 |
请求示例 (JSON)
{
"model": "dall-e-3",
"quality": "hd",
"prompt": "a white siamese cat",
"n": 1,
"size": "1024x1024"
}返回响应
200 成功
Content-Type: application/json
响应参数
| 字段名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| created | integer | 必需 | 创建时间戳。通常是 Unix 时间戳,表示任务创建或完成的时间。 |
| data | array [object] | 必需 | 数据列表。包含生成的图像信息数组,数组中的每个对象代表一张生成的图片。 |
| └─ revised_prompt | string | 可选 | 经过系统优化或人工修改后的最终提示词。 |
| └─ url | string | 可选 | 图片地址。生成图片的直接访问链接(如果请求中指定了返回 URL)。 |
响应示例
{
"created": 1760483997,
"data": [
{
"revised_prompt": "A white Siamese cat with sleek fur and bright blue eyes, sitting elegantly on a soft cushion by a sunny window. The sunlight is streaming through the window, creating a warm and cozy ambiance. The background features a blurred view of a garden outside the window, hinting at flowers and greenery, while the cat exudes an air of grace and poise.",
"url": "https://dalleprodsec.blob.core.windows.net/private/images/971071d6-3b7f-4a71-ab81-a5799a148628/generated_00.png?se=2025-10-15T23%3A20%3A12Z&sig=ko7zSeMfSXPv2X5o0RJal5KbURn5qUUgCHtwMEmJHtk%3D&ske=2025-10-15T19%3A00%3A58Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2025-10-08T19%3A00%3A58Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
}
]
}示例代码
python
import http.client
import json
conn = http.client.HTTPSConnection("nxaiapp.com")
payload = json.dumps({
"model": "dall-e-3",
"quality": "hd",
"prompt": "a white siamese cat",
"n": 1,
"size": "1024x1024"
})
headers = {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1/images/generations", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))