NanoBanana Pro

NanoBanana Pro 是 Gemini 的圖像產生服務,支援多種寬高比(1:1、16:9、9:16 等)和解析度(1K、2K、4K)。不同解析度價格相同,但 4K 圖產生速度慢,不建議使用。查看價格請點選 價格頁

端點POST https://aiberm.com/v1beta/models/gemini-3-pro-image-preview:generateContent

重點:請求體 contents 中傳入文字(生圖描述),generationConfig 中設定 responseModalitiesimageConfig(寬高比、解析度)等;回應裡從 candidates[0].content.partsinline_data 得到產生的圖片。

範例程式碼

Python 用底線命名(如 response_modalities),REST/cURL 用駝峰命名(如 responseModalities),含義相同。

1from google import genai
2from google.genai import types
3 
4base_url = "https://aiberm.com"
5api_key = "YOUR_API_KEY"
6prompt = "生成一只可爱的小海獭图片"
7model = "gemini-3-pro-image-preview"
8output_file = "gemini_sdk_generated.png"
9 
10# 图片生成配置
11response_modalities = ["IMAGE"] # 或 ["TEXT", "IMAGE"]
12aspect_ratio = "16:9" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9"
13image_size = "2K" # "1K", "2K", "4K"(仅 gemini-3-pro-image-preview 支持)
14 
15# 生成参数
16temperature = 1.0
17top_p = 0.95
18max_output_tokens = 8192
19enable_google_search = False # 是否启用谷歌搜索(会增加延迟与计费,生图建议关闭)
20 
21client = genai.Client(
22 api_key=api_key,
23 http_options=types.HttpOptions(api_version="v1beta", base_url=base_url)
24)
25 
26config = types.GenerateContentConfig(
27 response_modalities=response_modalities,
28 temperature=temperature,
29 top_p=top_p,
30 max_output_tokens=max_output_tokens,
31 system_instruction="You are a helpful assistant.",
32 image_config=types.ImageConfig(
33 aspect_ratio=aspect_ratio,
34 image_size=image_size if "pro-image" in model else None
35 )
36)
37if enable_google_search:
38 config.tools = [{"google_search": {}}]
39 
40response = client.models.generate_content(
41 model=model,
42 contents=[prompt],
43 config=config
44)
45 
46for part in response.parts:
47 if part.text is not None:
48 print(part.text)
49 elif part.inline_data is not None:
50 part.as_image().save(output_file)
51 print(f"图片已保存: {output_file}")

cURL 圖像參數

下面列出的是 cURL 請求裡的參數名(駝峰);用 Python SDK 時請改成底線寫法,如 aspectRatioaspect_ratio

imageConfiggenerationConfig.imageConfig 或 SDK ImageConfig):

  • aspectRatio string — 寬高比:1:12:33:23:44:34:55:49:1616:921:9
  • imageSize string — 解析度:1K2K4K(僅 gemini-3-pro-image-preview)

generationConfig 其他常用參數:

  • responseModalities string[] — 輸出類型:["IMAGE"]["TEXT", "IMAGE"]
  • temperature number — 隨機度,0~2,越高越隨機
  • topP number — 核取樣,0~1
  • maxOutputTokens number — 最大輸出 token 數(如 8192)
  • systemInstruction object — 系統指令,約束模型行為(如 {"parts": [{"text": "..."}]}
  • tools array — 工具(如 [{"google_search": {}}]

如何開啟 Google 搜尋?

Gemini 格式支援啟用 Google 搜尋。在設定中傳入 tools 即可,模型會依需求呼叫即時網路資訊並回傳帶引用的回答。

不建議預設開啟:開啟後會增加延遲與計費、對純生圖情境沒有幫助。若無即時檢索需求,建議關閉搜尋。

Python(SDK):

config = types.GenerateContentConfig(
    # ... 其他参数如 response_modalities、image_config 等
    tools=[{"google_search": {}}]
)
response = client.models.generate_content(model=model, contents=[prompt], config=config)

cURL(REST):

在請求體頂層增加 tools 欄位:

curl -X POST "https://aiberm.com/v1beta/models/gemini-3-pro-image-preview:generateContent" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [{"text": "你的问题"}]}],
    "generationConfig": { ... },
    "tools": [{"google_search": {}}]
  }'

常見問題

  • 回應裡沒有圖片: 確認模型為 gemini-3-pro-image-preview,且 prompt 是清晰的圖像描述。
  • 解碼失敗: 只擷取 data:image/...;base64, 後面的 base64 字串,且去掉換行再解碼。