发布于 2026-01-05 3 阅读
0

如何使用 Nano Banana 构建:完整开发者教程

如何使用 Nano Banana 构建:完整开发者教程

谷歌近期发布了Gemini 2.5 Flash Image,这是一款功能强大的全新图像生成和编辑模型,其代号为 Nano Banana。该模型引入了最先进的图像创建和处理功能,从而解锁了众多全新应用。

本指南为希望使用Gemini Developer API将 Gemini 2.5 Flash Image(又名 Nano Banana)集成到其应用程序中的开发人员提供了全面的演练。

本指南将涵盖以下内容:

  1. 在 AI Studio 中使用 Nano Banana
  2. 项目设置
  3. 图像创建
  4. 图像编辑
  5. 照片修复
  6. 多幅输入图像
  7. 对话式图像编辑
  8. 长宽比
  9. 仅图像输出
  10. 最佳实践和有效提示
  11. 社区案例和灵感
  12. 资源

以下是本教程中您将构建的内容示例:

prompt = "Restore and colorize this image from 1932"

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=[prompt, image],
)
Enter fullscreen mode Exit fullscreen mode

并排对比图,展示了原始的黑白照片

我们开始吧!

如果您更喜欢观看本文的视频版本,可以点击这里观看:

2025年11月更新:Nano Banana Pro,一款用于生成工作室级图像的高保真模型,现已推出。请参阅Nano Banana Pro开发者教程,了解如何使用它进行构建。

1)在 Google AI Studio 中使用 Nano Banana

虽然最终用户可以通过Gemini 应用访问 Nano Banana ,但对于开发者而言,进行原型设计和测试的最佳环境是Google AI Studio。AI Studio 是一个实验平台,可以在编写任何代码之前体验所有可用的 AI 模型,它也是使用 Gemini API 进行构建的入口点。

您可以在 AI Studio 中免费使用 Nano Banana。要开始使用,请访问aistudio.google.com,使用您的 Google 帐户登录,然后从模型选择器中选择Nano Banana 。

如需直接访问,请使用此链接开始与模型的新会话:

ai.studio/banana

一张 Google AI Studio 界面的图片,显示了模型选择过程,其中已选择 Nano Banana 模型。

提示:您也可以直接在 AI Studio ( ai.studio/apps)中编写 Nano Banana Web 应用程序,或者浏览代码并重新混合现有应用程序之一。

2)项目设置

要按照本指南操作,您需要以下物品:

步骤 A:生成 API 密钥

请按照以下步骤操作:

  • 在 Google AI Studio 中,点击左侧导航面板中的“获取 API 密钥” 。
  • 在下一页,点击“创建 API 密钥”
  • 选择一个现有的 Google Cloud 项目或创建一个新项目。此项目用于管理 API 使用情况的结算。

流程完成后,您的API密钥将显示出来。请复制并妥善保管。

步骤二:启用计费功能

虽然在 AI Studio 中进行原型设计是免费的,但通过 API 使用模型则需要付费。您必须在 Google Cloud 项目中启用结算功能。

在 API 密钥管理界面,点击项目旁边的“设置计费”,然后按照屏幕上的说明进行操作。

图片显示了 Google AI Studio 界面中的结算设置提示。

Nano Banana 多少钱?

使用 Nano Banana 生成图像,每张图像收费 0.039 美元*。1 美元大约可以生成 25 张图像。

官方定价为每百万个输入令牌 0.30 美元,每百万个输出令牌 30 美元。一张标准的 1024x1024 像素输出图像消耗 1290 个令牌,相当于每张图像 0.039 美元。详情请参阅Gemini 2.5 Flash 图像定价表

步骤 C:安装 SDK

选择您首选语言的 SDK。

Python:

pip install -U google-genai
# Install the Pillow library for image manipulation
pip install Pillow
Enter fullscreen mode Exit fullscreen mode

JavaScript / TypeScript:

npm install @google/genai
Enter fullscreen mode Exit fullscreen mode

以下示例使用 Python SDK 进行演示。使用 Nano Banana 的等效 JavaScript代码片段请参见此GitHub Gist

3)从文本生成图像

gemini-2.5-flash-image使用 Nano Banana 根据描述性文本提示生成一张或多张图片。所有 API 请求均需使用模型 ID 。

from google import genai
from PIL import Image
from io import BytesIO

# Configure the client with your API key
client = genai.Client(api_key="YOUR_API_KEY")

prompt = """Create a photorealistic image of an orange cat
with a green eyes, sitting on a couch."""

# Call the API to generate content
response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=prompt,
)

# The response can contain both text and image data.
# Iterate through the parts to find and save the image.
for part in response.candidates[0].content.parts:
    if part.text is not None:
        print(part.text)
    elif part.inline_data is not None:
        image = Image.open(BytesIO(part.inline_data.data))
        image.save("cat.png")
Enter fullscreen mode Exit fullscreen mode

输出:

一张逼真的照片,描绘了一只橙色、绿色眼睛的猫坐在沙发上。

该模型是多模态的,因此响应结构是一个列表,parts其中可以包含交错的文本和图像数据(inline_data)。上面的代码遍历这些部分以提取并保存生成的图像。

4) 使用文本和图像输入进行图像编辑

提供一张现有图片以及一段文字提示,即可进行编辑。该模型能够很好地保持输入图片的人物和内容一致性。

from google import genai
from PIL import Image
from io import BytesIO

client = genai.Client(api_key="YOUR_API_KEY")

prompt = """Using the image of the cat, create a photorealistic,
street-level view of the cat walking along a sidewalk in a
New York City neighborhood, with the blurred legs of pedestrians
and yellow cabs passing by in the background."""

image = Image.open("cat.png")

# Pass both the text prompt and the image in the 'contents' list
response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=[prompt, image],
)

for part in response.candidates[0].content.parts:
    if part.text is not None:
        print(part.text)
    elif part.inline_data is not None:
        image = Image.open(BytesIO(part.inline_data.data))
        image.save("cat2.png")
Enter fullscreen mode Exit fullscreen mode

输入和输出:

并排对比图,展示了猫咪在纽约市散步的原始照片和编辑后的照片。

5) 使用 Nano Banana 进行照片修复

该模型的强大功能之一是照片修复。只需简单的操作,它就能修复老照片并为其着色,效果令人惊艳。

from google import genai
from PIL import Image
from io import BytesIO

client = genai.Client(api_key="YOUR_API_KEY")

prompt = "Restore and colorize this image from 1932"

image = Image.open("lunch.jpg")  # "Lunch atop a Skyscraper, 1932"

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=[prompt, image],
)

for part in response.candidates[0].content.parts:
    if part.text is not None:
        print(part.text)
    elif part.inline_data is not None:
        image = Image.open(BytesIO(part.inline_data.data))
        image.save("lunch-restored.png")
Enter fullscreen mode Exit fullscreen mode

原始输出:

并排对比图,展示了原始的黑白照片

6)处理多个输入图像

您可以提供多张图片作为输入,以进行更复杂的编辑任务。

from google import genai
from PIL import Image
from io import BytesIO

client = genai.Client(api_key="YOUR_API_KEY")

prompt = "Make the girl wear this t-shirt. Leave the background unchanged."

image1 = Image.open("girl.png")
image2 = Image.open("tshirt.png")

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=[prompt, image1, image2],
)

for part in response.candidates[0].content.parts:
    if part.text is not None:
        print(part.text)
    elif part.inline_data is not None:
        image = Image.open(BytesIO(part.inline_data.data))
        image.save("girl-with-tshirt.png")
Enter fullscreen mode Exit fullscreen mode

输入 1 和 2 以及输出:

一张图片显示了女孩的输入照片、T恤的输入照片以及女孩穿着T恤的最终输出图像。

7)对话式图像编辑

为了实现迭代优化,您可以使用chats会话来跨多个请求保持上下文。这样您就可以以对话的方式编辑图像。

from google import genai
from PIL import Image
from io import BytesIO

client = genai.Client(api_key="YOUR_API_KEY")

# Create a chat
chat = client.chats.create(
    model="gemini-2.5-flash-image"
)

# Make the first image edit
response1 = chat.send_message(
    [
        "Change the cat to a bengal cat, leave everything else the same",
        Image.open("cat.png"),
    ]
)
# display / save image...

# Continue chatting and editing
response2 = chat.send_message("The cat should wear a funny party hat")
# display / save image...
Enter fullscreen mode Exit fullscreen mode

输入和输出 1 和 2:

图片展示了原图中的猫,第一次修改后将其改为孟加拉猫,第二次修改后则展示了正在睡觉的孟加拉猫。

提示:如果您发现经过多次对话编辑后图像特征开始退化或“漂移”,最好使用最新的图像和更详细、更统一的提示开始新的会话,以保持高保真度。

8)长宽比

您可以使用请求aspect_ratio中的相应字段控制输出图像的宽高比。您可以在文档中找到所有支持的宽高比。image_config

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

prompt = """Create a photorealistic image of an orange cat
with a green eyes, sitting on a couch."""

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=prompt,
    config={"image_config": {"aspect_ratio": "16:9"}},
)
Enter fullscreen mode Exit fullscreen mode

输出:

16:9 格式的猫的图片

如果没有指定纵横比,则该模型默认将输出图像的大小与输入图像的大小相匹配,否则将生成 1:1 的正方形。

9) 仅图像输出

response_modalities您可以通过设置配置来配置响应,使其仅返回图像而不返回文本["Image"]

response = client.models.generate_content(
    model="gemini-2.5-flash-image",
    contents=prompt,
    config={"response_modalities": ["Image"]},
)
Enter fullscreen mode Exit fullscreen mode

10)纳米香蕉的最佳实践和提示技巧

为了获得最佳的纳米香蕉使用效果,请遵循以下使用指南:

  • 务必做到非常具体:你提供的关于主题、颜色、光线和构图的细节越多,你对最终效果的控制力就越强。
  • 提供背景和意图:解释图像的目的或预期氛围。模特对背景的理解会影响其创作选择。
  • 迭代和改进:不要指望第一次就能做到完美。利用模型的对话能力,逐步修改并完善你的图像。
  • 使用分步说明:对于复杂的场景,请将提示分解成一系列清晰、按顺序的说明。
  • 使用积极的框架:不要使用“没有汽车”之类的消极提示,而是积极地描述理想的场景:“一条空旷荒凉的街道,没有车辆通行的迹象”。
  • 控制摄像机:使用摄影和电影术语来指导构图,例如“广角镜头”、“微距镜头”或“低角度透视”。

要深入了解最佳实践,请查看官方博客文章“提示最佳实践”文档中的提示指南。

11)社区案例与灵感

探索社区正在使用 Nano Banana 构建什么:

  • @henrydaubrez的视角切换X 帖子
  • @multimodalart的“小样本学习实现一致的角色设计” X 帖子
  • “红色箭头所指之处” Google 地图转换,作者@tokuminX 帖子
  • 由@yachimat_manga创作,根据简笔画注释生成图像X 帖子
  • @deedydas利用静态图像创建 3D 模型X 帖子
  • @bilawalsidhu正在创建基于位置的 AR 体验X 帖子
  • @demishassabis将 2D 地图转换为 3D 图形X 帖子

12)资源和后续步骤

本指南涵盖了使用 Nano Banana(又名 Gemini 2.5 Flash Image)进行构建的基础知识。您已经学习了如何设置环境、生成和编辑镜像以及应用高级技巧。现在,您可以开始将这些强大的功能应用到自己的项目中了。

如需了解更多信息,请查阅官方资源:

如果你用这个做出了什么很酷的东西,我很想看看!欢迎私信或在 X 上标记我:@patloeber

文章来源:https://dev.to/googleai/how-to-build-with-nano-banana-complete-developer-tutorial-646