Nano Banana 文生圖與尺寸
模型 ID 與介面選擇見 Nano Banana 概覽。
重點比例由
aspectRatio 指定,解析度由 imageSize 指定,兩者皆位於 generationConfig.imageConfig 中。文生圖範例
Python
nano_banana.py
import base64
from google import genai
from google.genai import types
client = genai.Client(
api_key="YOUR_OFOX_API_KEY",
http_options={"api_version": "v1beta", "base_url": "https://api.ofox.ai/gemini"},
)
resp = client.models.generate_content(
model="google/gemini-3.1-flash-image",
contents="白色桌面上的一顆紅蘋果",
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio="16:9", image_size="2K"),
),
)
for part in resp.candidates[0].content.parts:
if part.inline_data:
with open("output.png", "wb") as f:
f.write(part.inline_data.data)Python SDK 回傳的 inline_data.data 為二進位資料,可直接寫入檔案;cURL 回傳的是 base64 字串,需先解碼。
各模型支援的取值
常用 10 種比例:1:1、2:3、3:2、3:4、4:3、4:5、5:4、9:16、16:9、21:9。不在清單中的比例(如 5:3)會回傳 400 Request contains an invalid argument。
解析度寫法:imageSize 建議使用大寫 1K、2K、4K,與 Google 官方文件一致;OfoxAI 實測也接受小寫。512 檔不帶單位 K。
不傳 imageConfig:比例由模型決定。需要固定比例請明確傳入。
實測輸出尺寸
4K 的 PNG 約 16 MB,經 base64 編碼後回應主體超過 20 MB。請確認用戶端、代理與日誌系統不會截斷大型回應。
遇到錯誤請參閱 錯誤排查。
最後更新於