Skip to content

Images 图像生成 ​

图像生成与编辑接口。

参考文档: https://platform.openai.com/docs/api-reference/images

创建图像 ​

POST /v1/images/generations

请求体 (application/json) ​

参数类型必填说明
promptstring是图像生成提示词
modelstring否模型 ID(如 dall-e-3)
ninteger否生成图像数量,1-10,默认 1
sizestring否256x256、512x512、1024x1024、1792x1024、1024x1792,默认 1024x1024
qualitystring否standard、hd,默认 standard
stylestring否vivid、natural,默认 vivid
response_formatstring否url、b64_json,默认 url
userstring否用户标识

请求示例 ​

bash
curl -X POST "https://your-api-base/v1/images/generations" \
  -H "Authorization: Bearer sk-xxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A white siamese cat",
    "n": 1,
    "size": "1024x1024"
  }'
python
from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxx",
    base_url="https://your-api-base/v1"
)

response = client.images.generate(
    model="dall-e-3",
    prompt="A white siamese cat",
    n=1,
    size="1024x1024"
)

print(response.data[0].url)

200 成功响应 ​

json
{
  "created": 1677858242,
  "data": [
    {
      "url": "https://example.com/image.png",
      "revised_prompt": "A white siamese cat with blue eyes sitting on a cushion"
    }
  ]
}

编辑图像 ​

POST /v1/images/edits

请求体 (multipart/form-data) ​

参数类型必填说明
imagebinary是源图像文件
promptstring是编辑提示词
maskbinary否遮罩图像
modelstring否模型 ID
ninteger否生成图像数量
sizestring否图像尺寸
response_formatstring否响应格式

请求示例 ​

bash
curl -X POST "https://your-api-base/v1/images/edits" \
  -H "Authorization: Bearer sk-xxxxxx" \
  -F image="@original.png" \
  -F mask="@mask.png" \
  -F prompt="A sunlit indoor room with a pool" \
  -F model="dall-e-2" \
  -F n=1