图像生成与编辑
图像端点只适用于支持 Images API 的 OpenAI 分组。建议先用 /v1/models 确认当前 Key 能看到图像模型。
生成图片
bash
curl https://codexapi.asia/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A clean studio photograph of a white ceramic cup.",
"size": "1024x1024",
"quality": "high",
"response_format": "b64_json"
}'响应图片通常位于 data[0].b64_json。部分聊天软件只会显示文字,不会自动渲染 Base64 图片,需要客户端主动解码并保存。
Python 解码
python
import base64
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["CODEX_RELAY_API_KEY"],
base_url="https://codexapi.asia/v1",
)
result = client.images.generate(
model="gpt-image-2",
prompt="A minimal product photo of a desk lamp.",
size="1024x1024",
response_format="b64_json",
)
with open("image.png", "wb") as image_file:
image_file.write(base64.b64decode(result.data[0].b64_json))编辑图片
图像编辑使用 multipart 表单:
bash
curl https://codexapi.asia/v1/images/edits \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "model=gpt-image-2" \
-F "prompt=Change the background to pure white" \
-F "image=@input.png" \
-F "response_format=b64_json"Responses 图像工具
支持 Responses 工具的模型可以在 /v1/responses 请求中携带 image_generation 工具。是否可用取决于模型、分组和上游账号能力。
生成成功但看不到图片
按以下顺序检查:
- 查看原始 JSON 中是否存在
b64_json或图像工具结果。 - 确认客户端支持 Base64 图片渲染。
- 使用上面的 Python 示例直接保存,排除客户端展示问题。
- 如响应没有图像字段,记录请求 ID,并检查模型是否确实支持图像输出。
