Google Gemini 3 Pro 的实战开发指南,193 次互动表示受关注。适合需要集成 AI 图像能力的开发者。
喜欢 Nano Banana 吗?是不是已经给所有朋友都生成了手办图片,还在所有对手背后加上了幽灵脸?现在,没那么「nano」的 Gemini 3 Pro Image 模型来了,不过大家肯定更愿意叫它 Nano Banana Pro!
Flash 模型(Nano Banana)带来了速度和亲民的价格,而 Pro 版本则引入了「思考」能力、搜索溯源,以及高保真的 4K 输出。是时候用复杂的创意任务彻底玩疯了!
本指南将带你使用 Gemini Developer API,逐步了解 Nano Banana Pro 的高级功能。至于 prompt 编写建议,接下来可以阅读 prompt 指南。
本指南将涵盖:
注意:如果想阅读本文的交互式版本,可以查看 Python cookbook 或 AI Studio 的 JavaScript Notebook。
最终用户可以在 Gemini app 中使用 Nano Banana Pro,但对开发者来说,Google AI Studio 才是制作原型和测试 prompt 的最佳环境。AI Studio 是一个用于体验所有可用 AI 模型的 playground,你可以先在这里进行实验,再开始编写代码;它也是使用 Gemini API 进行开发的入口。
你可以直接在 AI Studio 中使用 Nano Banana Pro。首先访问 aistudio.google.com,登录 Google 账号,然后在模型选择器中选择 Nano Banana Pro(Gemini 3 Pro Image)。
与 Nano Banana 不同,Pro 版本没有免费额度,这意味着你需要选择一个已启用结算功能的 API key(参见下面的「项目设置」部分)。
提示:你还可以在 ai.studio/apps 中直接通过 vibe coding 开发 Nano Banana Web 应用,也可以浏览现有应用的代码,并在其基础上进行 remix。
要按照本指南进行操作,你需要准备:
如果你已经是一名硬核 Gemini API 用户,而且上面的东西全都准备好了,那太棒了!直接跳过这一节,进入下一节即可。否则,可以按照以下步骤开始:
首次登录 AI Studio 时,系统应该会自动创建一个 Google Cloud 项目和一个 API key。
打开 API key 管理页面,单击「复制」图标,复制你的 API key。
由于 Nano Banana Pro 没有免费额度,因此必须为 Google Cloud 项目启用结算功能。
在 API key 管理页面中,单击项目旁边的 Set up billing,然后按照屏幕上的说明进行操作。
使用 Nano Banana Pro 生成图像的价格比 Flash 版本更高,尤其是生成 4K 图像时。本文发布时,生成一张 1K 或 2K 图像的价格为 0.134 美元,而生成一张 4K 图像的价格为 0.24 美元(此外还要加上输入和文本输出的 token 费用)。
请查看文档中的价格信息,了解最新详情。
专业提示:使用 Batch API 可以节省 50% 的生成费用。作为交换,你可能需要等待最长 24 小时才能拿到生成的图像。
选择你偏好的编程语言所对应的 SDK。
Python:你需要安装 Python 3,以及至少 1.52 版本的 Python SDK。
pip install -U "google-genai>=1.52.0"
# Install the Pillow library for image manipulation
pip install Pillow
JavaScript / TypeScript:你需要安装至少 1.30 版本的 JS/TS SDK。
npm install @google/genai
注意:下面的示例使用 Python SDK 进行演示。用于在 JavaScript 中使用 Nano Banana 的等效代码片段,可以在这个 JS Notebook 中找到。
要使用 Pro 模型,需要使用 gemini-3-pro-image-preview 模型 ID。
from google import genai
from google.genai import types
# Initialize the client
client = genai.Client(api_key="YOUR_API_KEY")
# Set the model ID
PRO_MODEL_ID = "gemini-3-pro-image-preview"
在深入花哨的功能之前,先来看看标准的生成方式。你可以通过 response_modalities 控制输出内容(同时获取文本和图像,或者只获取图像),并通过 aspect_ratio 控制宽高比。
prompt = "Create a photorealistic image of a siamese cat with a green left eye and a blue right one"
aspect_ratio = "16:9" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9" or "21:9"
response = client.models.generate_content(
model=PRO_MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image'], # Or just ['Image']
image_config=types.ImageConfig(
aspect_ratio=aspect_ratio,
)
)
)
# Save the image
for part in response.parts:
if image:= part.as_image():
image.save("cat.png")
你也可以使用 Chat 模式(实际上,对于多轮编辑,我更推荐这种方式)。具体示例可以查看第 8 个例子「Polyglot Banana」。
Nano Banana Pro 不只是在画图,它还会思考。这意味着,在生成图像之前,它可以推理你那些最复杂、最烧脑的 prompt。最棒的是,你甚至还能窥探它的大脑!
要启用这一功能,请在 thinking_config 中设置 include_thoughts=True。
prompt = "Create an unusual but realistic image that might go viral"
aspect_ratio = "16:9"
response = client.models.generate_content(
model=PRO_MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image'],
image_config=types.ImageConfig(
aspect_ratio=aspect_ratio,
),
thinking_config=types.ThinkingConfig(
include_thoughts=True # Enable thoughts
)
)
)
# Save the image and thoughts
for part in response.parts:
if part.thought:
print(f"Thought: {part.text}")
elif image:= part.as_image():
image.save("viral.png")
你应该会得到类似这样的内容:
## Imagining Llama Commuters
I'm focusing on the llamas now. The goal is to capture them as
daily commuters on a bustling bus in La Paz, Bolivia. My plan
involves a vintage bus crammed with amused passengers. The image
will highlight details like one llama looking out the window,
another interacting with a passenger, all while people take
photos.
[IMAGE]
## Visualizing the Concept
I'm now fully immersed in the requested scenario. My primary
focus is on the "unusual yet realistic" aspects. The scene is
starting to take shape with the key elements established.
这种透明度可以帮助你理解模型是如何解读需求的。就像是在和你的艺术家对话!
最具颠覆性的功能之一就是 Search Grounding。Nano Banana Pro 不会被困在过去,它可以访问 Google Search 的实时数据,从而生成准确且时效性强的图像。想知道天气?没问题。
例如,你可以让它把当前天气预报可视化:
prompt = "Visualize the current weather forecast for the next 5 days in Tokyo as a clean, modern weather chart. add a visual on what i should wear each day"
response = client.models.generate_content(
model=PRO_MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image'],
image_config=types.ImageConfig(
aspect_ratio="16:9",
),
tools=[{"google_search": {}}] # Enable Google Search
)
)
# Save the image
for part in response.parts:
if image:= part.as_image():
image.save("weather.png")
# Display sources (you must always do that)
print(response.candidates[0].grounding_metadata.search_entry_point.rendered_content)
需要印刷级别的图像吗?Nano Banana Pro 支持 4K 分辨率。毕竟有时候,越大就是越好。
prompt = "A photo of an oak tree experiencing every season"
resolution = "4K" # Options: "1K", "2K", "4K", be careful lower case do not work.
response = client.models.generate_content(
model=PRO_MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image'],
image_config=types.ImageConfig(
aspect_ratio="1:1",
image_size=resolution
)
)
)
注意:生成 4K 图像的成本更高,请合理使用!
该模型可以使用十几种语言生成图像中的文字,甚至还能翻译这些文字。它基本上就是一个服务于视觉内容的万能翻译器。
# Generate an infographic in Spanish
message = "Make an infographic explaining Einstein's theory of General Relativity suitable for a 6th grader in Spanish"
response = chat.send_message(message,
config=types.GenerateContentConfig(
image_config=types.ImageConfig(aspect_ratio="16:9")
)
)
# Save the image
for part in response.parts:
if image:= part.as_image():
image.save("relativity.png")
# Translate it to Japanese
message = "Translate this infographic in Japanese, keeping everything else the same"
response = chat.send_message(message)
# Save the image
for part in response.parts:
if image:= part.as_image():
image.save("relativity_JP.png")
Flash 模型最多可以混合 3 张图像,而 Pro 模型最多可以处理 14 张图像!一个 prompt 就能开一场完整的派对。它非常适合制作复杂拼贴图,或者一次性展示完整的产品线。
# Mix multiple images
response = client.models.generate_content(
model=PRO_MODEL_ID,
contents=[
"An office group photo of these people, they are making funny faces.",
PIL.Image.open('John.png'),
PIL.Image.open('Jane.png'),
# ... add up to 14 images
],
)
# Save the image
for part in response.parts:
if image:= part.as_image():
image.save("group_picture.png")
注意:如果希望角色保持非常高的还原度,建议将图像数量限制在 5 张以内——对于一场派对之夜来说,这已经绰绰有余了!
下面是一些只有 Nano Banana Pro 才能实现的效果。准备好大开眼界吧:
Prompt:「搜索 Web,然后生成一张等距视角、细节丰富的像素艺术图像,展示 Guillaume Vernade 的职业生涯。」
它会使用 Search Grounding 查找某个人的具体信息,然后以指定风格将这些信息可视化。
Prompt:「向我展示一张介绍十四行诗运作方式的信息图,其中包含一首以香蕉为主题的十四行诗,以及对这首诗的长篇文学分析。采用精美的复古美学风格。」
该模型可以生成连贯的长篇文字,并将其完美地融入复杂布局。
Prompt:「拍摄一张照片,内容是一本放在精美剧院座椅上的百老汇演出节目册,这场演出讲述的是 TCG 玩家。节目册专业、制作精良、表面有光泽,我们可以同时看到封面,以及其中一页展示舞台照片的内页。」
它可以创建印刷材料的照片级 Mockup,同时呈现准确的光照和纹理。
为了让 Nano Banana 模型取得最佳效果,请遵循以下 prompt 编写指南:
做到极其具体: 你提供的主体、颜色、光线和构图细节越多,对输出结果的控制力就越强。
提供上下文和意图: 说明图像的用途或你想营造的氛围。模型对上下文的理解会影响它的创意选择。
迭代并优化: 不要期待第一次尝试就获得完美结果。利用模型的对话能力逐步进行修改,不断完善图像。
使用分步指令: 对于复杂场景,将 prompt 拆分为一系列清晰且顺序明确的指令。
使用正向描述: 与其使用「不要出现汽车」这样的否定 prompt,不如正面描述你想要的场景:「一条空旷、荒无人烟、看不到任何交通迹象的街道。」
控制相机: 使用摄影和电影术语来指导构图,例如「广角镜头」「微距镜头」或「低角度视角」。
充分利用 Search Grounding: 当你确定希望模型使用实时数据或现实世界的数据时,请把需求说得非常明确。「搜索 Olympique Lyonnais 最近几场比赛的 Web 信息,并制作一张信息图」,会比只说「制作一张 OL 最近几场比赛的信息图」效果更好(后者应该也能工作,但没必要冒险)。
使用 Batch API 降低成本并获得更多 quota: Batch API 可以把少量或大量请求组合在一起发送。处理这些请求最多可能需要 24 小时,但作为交换,你可以节省 50% 的生成成本,而且 quota 也更高!
如果想更深入地了解最佳实践,请查看文档中的 prompt 指南,以及官方博客发布的 Nano Banana prompt 最佳实践。
Nano Banana Pro(Gemini 3 Pro Image)为 AI 图像生成开辟了新的前沿。凭借思考、搜索和 4K 渲染能力,它既是严肃创作者的工具,也能带来十足的乐趣。
准备好尝试了吗?查看我的 prompt 指南,前往 Google AI Studio,试用或定制我们的 Apps,或者查看 cookbook。
部分评论可能只有已登录的访客才能看到。请登录以查看所有评论。
如果需要采取进一步措施,你可以考虑屏蔽此人和/或举报滥用行为。