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 中设置 responseModalities、imageConfig(宽高比、分辨率)等;响应里从 candidates[0].content.parts 取 inline_data 得到生成图片。
示例代码
Python 用下划线命名(如 response_modalities),REST/cURL 用驼峰命名(如 responseModalities),含义相同。
1from google import genai2from google.genai import types3 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.017top_p = 0.9518max_output_tokens = 819219enable_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 None35 )36)37if enable_google_search:38 config.tools = [{"google_search": {}}]39 40response = client.models.generate_content(41 model=model,42 contents=[prompt],43 config=config44)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 时请改成下划线写法,如 aspectRatio → aspect_ratio。
imageConfig(generationConfig.imageConfig 或 SDK ImageConfig):
- aspectRatio
string— 宽高比:1:1、2:3、3:2、3:4、4:3、4:5、5:4、9:16、16:9、21:9 - imageSize
string— 分辨率:1K、2K、4K(仅 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": {}}])
如何开启谷歌搜索?
仅 Gemini 格式支持启用谷歌搜索。在配置中传入 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 字符串,且去掉换行再解码。