文档 · 接入
图片生成
POST /v1/images/generations:根据文字描述生成图片。请从目录中选择类型为「图像」的模型。
curl https://<your-endpoint>/v1/images/generations \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-1",
"prompt": "A lighthouse on a cliff at sunset, watercolor",
"size": "1024x1024"
}'常用参数
| 参数 | 说明 |
|---|---|
model | 必填。图像模型 ID |
prompt | 必填。图片描述,越具体越好 |
size | 尺寸,如 1024x1024;可选值因模型而异 |
n | 生成张数 |
支持的尺寸、张数和返回格式(
url 或 b64_json)因模型而异,以模型详情页为准。返回的 URL 通常是临时的,请及时下载保存。保存结果
import base64
from openai import OpenAI
client = OpenAI(base_url="https://<your-endpoint>/v1", api_key="YOUR_API_KEY")
img = client.images.generate(
model="gpt-image-1",
prompt="A minimalist poster of a mountain lake, flat colors",
size="1024x1024",
n=1,
)
item = img.data[0]
if item.b64_json: # some models return base64
open("out.png", "wb").write(base64.b64decode(item.b64_json))
else: # others return a temporary URL; download it promptly
print(item.url)图像模型可能按次或按 tokens 计费,详见价格。