Skip to Content

Images API

兩個端點:生成(文字 → 圖)、編輯(圖 + 文字 → 圖)。回應都是 OpenAI 標準結構 data[0].b64_json

Gemini 系列圖像模型(如 google/gemini-3.1-flash-image-preview)在本端點只能生成不能編輯。需要編輯請走 Gemini 原生協定

生成圖像

POST https://api.ofox.ai/v1/images/generations

用 gpt-image-2

gen.py
import base64 from openai import OpenAI client = OpenAI(api_key="YOUR_OFOX_API_KEY", base_url="https://api.ofox.ai/v1") resp = client.images.generate( model="openai/gpt-image-2", prompt="A simple red apple on a white table", size="1024x1024", quality="low", output_format="png", ) with open("output.png", "wb") as f: f.write(base64.b64decode(resp.data[0].b64_json))

實測輸出:

gpt-image-2 生成的紅蘋果

用 gemini-3.1-flash-image-preview

同一端點也接受 Gemini 圖像模型。不要傳 n——閘道會把 n 錯誤對應為 numberOfImages 欄位並回報 400,每次固定生成 1 張。

gen_gemini.py
import base64 from openai import OpenAI client = OpenAI(api_key="YOUR_OFOX_API_KEY", base_url="https://api.ofox.ai/v1") resp = client.images.generate( model="google/gemini-3.1-flash-image-preview", prompt="A simple red apple on a white table, photorealistic", size="1024x1024", quality="low", output_format="png", ) with open("output.png", "wb") as f: f.write(base64.b64decode(resp.data[0].b64_json))

實測輸出:

Gemini 生成的紅蘋果

參數

參數類型必填說明
modelstringopenai/gpt-image-2google/gemini-3.1-flash-image-preview
promptstring自然語言描述
qualitystringauto / low / medium / high / standard / hd
nnumber1–10,預設 1。Gemini 模型不支援
sizestringauto / 1024x1024 / 1536x1024 / 1024x1536 / 256x256 / 512x512 / 1792x1024 / 1024x1792
output_formatstringpng / jpeg / webp
backgroundstringtransparent / opaque / auto
streamboolean預設 false

回應

{ "created": 1777385517, "data": [ { "b64_json": "<圖片 Base64>", "index": 0 } ], "model": "openai/gpt-image-2", "size": "1024x1024", "quality": "low", "usage": { "input_tokens": 14, "input_tokens_details": { "text_tokens": 14 }, "output_tokens": 208, "total_tokens": 222 } }

圖片在 data[0].b64_json,自行 base64 解碼後儲存。

編輯圖像

POST https://api.ofox.ai/v1/images/edits

multipart/form-data,需上傳圖片檔案。

此端點僅支援 OpenAI / Azure OpenAI 模型google/gemini-3.1-flash-image-preview 呼叫會回傳 Image editing is not supported for model——改走 Gemini 原生協定編輯圖像

呼叫

edit.py
import base64 from openai import OpenAI client = OpenAI(api_key="YOUR_OFOX_API_KEY", base_url="https://api.ofox.ai/v1") with open("apple.png", "rb") as f: resp = client.images.edit( model="openai/gpt-image-2", image=f, prompt="把蘋果改成綠色,其他保持不變", size="auto", quality="low", ) with open("apple_edited.png", "wb") as out: out.write(base64.b64decode(resp.data[0].b64_json))

image 欄位傳本機檔案路徑(cURL 用 @ 前綴),不是 URL。

實測對比:

原圖編輯後
原始紅蘋果編輯後的綠蘋果

參數

參數類型必填說明
modelstring推薦 openai/gpt-image-2
imagefilePNG / JPEG 檔案
promptstring編輯指令
qualitystringlow / medium / high
nnumber預設 1
sizestringauto 表示與原圖一致

回應

與生成一致:

{ "created": 1777385669, "data": [ { "b64_json": "<編輯後圖片 Base64>", "index": 0 } ], "model": "openai/gpt-image-2", "size": "auto", "quality": "low", "usage": { "input_tokens": 1041, "input_tokens_details": { "image_tokens": 1024, "text_tokens": 17 }, "num_input_images": 1, "output_tokens": 358, "total_tokens": 1399 } }

usage.input_tokens_details.image_tokens 是輸入圖片消耗的 token,num_input_images 是輸入圖片張數。

支援的模型與價格見 模型目錄 

Last updated on