Skip to content

圖像生成與編輯 ​

圖像端點只適用於支援 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 工具。是否可用取決於模型、群組與上游帳號能力。

生成成功但看不到圖片 ​

  1. 查看原始 JSON 是否存在 b64_json 或圖像工具結果。
  2. 確認用戶端支援 Base64 圖片呈現。
  3. 使用上方 Python 範例直接儲存,排除用戶端顯示問題。
  4. 若回應沒有圖像欄位,記錄請求 ID 並檢查模型是否支援圖像輸出。