Skip to Content
模型用法Nano Banana改圖與多圖融合

Nano Banana 改圖與多圖融合

模型 ID 與介面選擇見 Nano Banana 概覽。

重點原生介面將圖片與文字放入同一個 parts;OpenAI 相容介面使用 /v1/images/edits。
edit.py
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"}, ) with open("apple.png", "rb") as f: image = types.Part.from_bytes(data=f.read(), mime_type="image/png") resp = client.models.generate_content( model="google/gemini-3.1-flash-image", contents=["把蘋果改成綠色,其他保持不變", image], config=types.GenerateContentConfig( response_modalities=["IMAGE"], image_config=types.ImageConfig(aspect_ratio="1:1", image_size="1K"), ), ) for part in resp.candidates[0].content.parts: if part.inline_data: with open("apple_green.png", "wb") as f: f.write(part.inline_data.data)
  • 多圖融合:在同一個 parts 中放入多個 inlineData,並在文字中說明每張圖片的用途,例如「將第一張圖中的人物放到第二張圖的場景中」。
  • 保持比例:改圖時同樣需要傳入 aspectRatio,否則輸出比例由模型決定,可能與原圖不一致。
  • 參考圖計費:參考圖依輸入 token 計費。實測一張 1024×1024 的參考圖計 1,120 個 token,見回應中 promptTokensDetails 的 IMAGE 項。

遇到錯誤請參閱 錯誤排查。

最後更新於