Nano Banana 是 Google 的社群暱稱(也是內部簡稱) Gemini 2.5 Flash 影像 ——一個高品質、低延遲的多模態影像生成 + 編輯模型。本指南(包含程式碼、模式、部署步驟和 CometAPI 範例)展示了三種可在生產環境中使用的實用呼叫方法:(1) 與 OpenAI 相容的聊天介面(文字→圖像),(2) Google 官方 generateContent 文字→圖像介面,以及(3)Google 官方 generateContent 使用 Base64 輸入/輸出的影像→影像介面。在此過程中,您將獲得逐步分發/部署建議、環境設定、如何從 CometAPI 取得 API 操作、定價和浮水印說明,以及獲得可靠、經濟高效結果的最佳技巧。
什麼是 Nano Banana(Gemini 2.5 Flash 圖片)?
Nano Banana 是 Gemini 2.5 Flash Image 的非正式名稱,它是 Google Gemini 系列中最新的圖像模型。它專為照片級逼真的圖像生成和精準的圖像編輯(本地編輯、多圖像融合、跨編輯的字符一致性保留)而設計,可透過 Google 的 Gemini API、Google AI Studio 和 Vertex AI 使用。模型隨附一個不可見的 SynthID 浮水印,方便用戶識別來源。
Google 帳戶 + 雲端項目 如果您打算透過 Google 直接呼叫 Gemini(Gemini API / Vertex AI),請建立 Google Cloud 專案並啟用 Vertex AI / Gemini API。您需要計費和適當的角色(例如, Vertex AI Admin or Service Account 具有推理權)。
Gemini API 訪問 — 一些 Gemini 圖像模型處於預覽/有限可用性狀態;您可能需要根據您的帳戶透過 Google AI Studio 或 Vertex AI 請求存取或使用該模型。
CometAPI(可選網關) 如果您希望使用與供應商無關的 API 來代理不同的模型(包括 Gemini),請在 CometAPI 上註冊以獲取 API 金鑰並查看其模型清單(他們公開了 Gemini 2.5 Flash 變體和與 OpenAI 相容的端點)。 CometAPI 可以簡化開發,讓您無需更改應用程式碼即可切換供應商。
from google import genai
from base64 import b64decode, b64encode
client = genai.Client(api_key="YOUR_GEMINI_KEY")
prompt = {
"content": "A hyperrealistic photo of a vintage motorcycle parked under neon lights at midnight",
"mime_type": "text/plain"
}
# request generateContent for image output
result = client.generate_content(
model="gemini-2-5-flash-image-preview",
prompt=prompt,
response_modalities=,
image_format="PNG",
)
# handle binary or base64 in response (depends on API mode)
(注意:請檢查官方客戶端 API 以取得準確的參數名稱 - 上述範例遵循 Google 文件中的模式。)
# pip install google-genai
from google import genai
from google.genai import types
import base64
client = genai.Client(api_key="YOUR_GOOGLE_API_KEY")
# Read local image
with open("input_photo.jpg", "rb") as f:
img_bytes = f.read()
# Using SDK helper to attach bytes as a part
response = client.models.generate_content(
model="gemini-2.5-flash-image-preview",
contents=[
types.Part.from_bytes(
data=img_bytes,
mime_type="image/jpeg"
),
"Make a high-quality edit: change the subject's jacket color to teal, keep natural lighting and preserve the person's facial features."
],
)
# The returned image will typically be in response.candidates[].content.parts with base64-encoded data
# Decode and save (pseudo-access shown; check SDK response structure)
b64_out = response.candidates.content.parts.data # example path
with open("edited.jpg","wb") as out:
out.write(base64.b64decode(b64_out))
Python 範例:透過靜止點使用 Base64 實現映像→影像
import base64, json, requests
API_URL = "https://api.gemini.googleapis.com/v1/generateContent"
API_KEY = "YOUR_GEMINI_KEY"
# read and base64-encode image
with open("input.jpg","rb") as f:
b64 = base64.b64encode(f.read()).decode("utf-8")
payload = {
"model": "gemini-2-5-flash-image-preview",
"input": [
{"mime_type": "image/jpeg", "bytes_base64": b64},
{"mime_type": "text/plain", "text": "Remove the lamppost and make the sky golden at sunset."}
],
"response_modalities":
}
resp = requests.post(API_URL, headers={"Authorization":f"Bearer {API_KEY}", "Content-Type":"application/json"}, json=payload)
resp.raise_for_status()
data = resp.json()
# data.candidates... may contain image base64 — decode and save
out_b64 = data
with open("edited.png","wb") as out:
out.write(base64.b64decode(out_b64))