GPT Image
POST /v1/images/generations
使用 gpt-image-1 模型生成图像,兼容 OpenAI Images API 格式。
生成图像
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|
model | string | 是 | 固定为 gpt-image-1 |
prompt | string | 是 | 图像描述提示词 |
n | integer | 否 | 生成数量,默认 1 |
size | string | 否 | 图片尺寸:1024x1024(默认)、1536x1024、1024x1536、auto |
quality | string | 否 | 质量:auto(默认)、low、medium、high |
background | string | 否 | 背景:auto(默认)、transparent、opaque |
output_format | string | 否 | 输出格式:png(默认)、jpeg、webp |
请求示例
curl -X POST https://crazyrouter.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-image-1",
"prompt": "一只穿着宇航服的猫咪在月球上行走,背景是地球",
"n": 1,
"size": "1024x1024",
"quality": "high"
}'
响应示例
{
"created": 1709123456,
"data": [
{
"url": "https://crazyrouter.com/files/image_abc123.png",
"revised_prompt": "A cat wearing a spacesuit walking on the moon's surface with Earth visible in the background"
}
]
}
编辑图像
基于已有图像进行编辑,支持遮罩区域编辑。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|
model | string | 是 | 固定为 gpt-image-1 |
image | file | 是 | 原始图片文件(multipart) |
prompt | string | 是 | 编辑描述 |
mask | file | 否 | 遮罩图片,透明区域为需要编辑的部分 |
n | integer | 否 | 生成数量 |
size | string | 否 | 输出尺寸 |
请求示例
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://crazyrouter.com/v1"
)
response = client.images.edit(
model="gpt-image-1",
image=open("original.png", "rb"),
mask=open("mask.png", "rb"),
prompt="在天空中添加一道彩虹",
n=1,
size="1024x1024"
)
print(response.data[0].url)
GPT Image 支持透明背景输出,设置 background: "transparent" 并使用 PNG 格式即可。