
Google 最新发布了 Gemini 2.5 Flash Image,这是一款强大的全新图像生成和编辑模型,代号 Nano Banana。该模型具备最先进的图像创作与编辑能力,为开发者解锁了丰富的应用场景。
本教程将为开发者全面介绍如何通过 Gemini Developer API 集成 Gemini 2.5 Flash Image(即 Nano Banana),包括:
- 在 AI Studio 中使用 Nano Banana
- 项目环境搭建
- 图像生成
- 图像编辑
- 照片修复
- 多图输入
- 对话式图像编辑
- 最佳实践与高效提示词技巧
- 社区案例与灵感
- 参考资源
以下是本教程中的一个实际案例:
prompt = "恢复并为这张 1932 年的照片上色" response = client.models.generate_content( model="gemini-2.5-flash-image-preview", contents=[prompt, image], )
普通用户可在 Gemini 应用中体验 Nano Banana,但开发者最推荐的原型测试环境是 Google AI Studio。AI Studio 是进行各种 AI 模型实验的最佳“游乐场”,也是集成 Gemini API 的起点。
你可以免费在 AI Studio 内使用 Nano Banana。只需访问 aistudio.google.com,登录 Google 账户,然后在模型选择器中挑选 Nano Banana。
直接开启新会话可访Nano Banana 教程问:ai.studio/banana
小贴士:你还可以直接在 AI Studio 里开发和测试 Nano Banana Web 应用(ai.studio/apps),也可以查看和 Remix 已有的社区应用。
按照本指南,你需要准备以下内容:
- 在 Google AI Studio 获取 API Key
- 为你的项目开通计费
- 安装 Google Gen AI SDK(支持 Python 或 JavaScript/TypeScript)
- 在 Google AI Studio 左侧导航栏点击“获取 API 密钥”
- 点击“创建 API 密钥”
- 选择已有 Google Cloud 项目或新建项目(用于计费管理)
- 创建完成后,系统会显示你的 API 密钥,请妥善保存
在 AI Studio 内原型测试免费,但通过 API 使用模型是付费的。你需要在 Google Cloud 项目中开启计费。
- 在 API Key 管理界面,点击项目旁边的 “设置计费”,并按指引操作
Nano Banana 的价格?
生成一张图片费用 $0.039(大约 1 美元可生成 25 张图片)。
官方标准为:每 100 万输入 Token 收费 $0.30,每 100 万输出 Token 收费 $30。标准 1024x1024px 输出图片约消耗 1290 Token,折算下来每张图 $0.039。详细可查阅 官方价格表。
请选择你常用的开发语言:
Python:
pip install -U google-genai pip install Pillow # 图像处理库
JavaScript / TypeScript:
npm install @google/genai
以下示例以 Python 为主,等效的 JavaScript 代码可查阅 GitHub Gist。
使用 Nano Banana,可以通过描述性文本生成一张或多张图片。所有 API 请求均使用模型 ID:gemini-2.5-flash-image-preview。
from google import genai from PIL import Image from io import BytesIO client = genai.Client(api_key="YOUR_API_KEY") prompt = """ 生成一只橘猫,绿色眼睛,坐在沙发上,风格写实。 """ response = client.models.generate_content( model="gemini-2.5-flash-image-preview", contents=prompt, ) 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")
输出说明:
模型响应是一个 parts 列表,可能包含混合文本和图片数据(inline_data)。上面代码会遍历 parts,自动提取并保存图片。
你可以上传一张图片,配合文本提示,实现图片的高级编辑。模型善于保持人物/主体的一致性。
from google import genai from PIL import Image from io import BytesIO client = genai.Client(api_key="YOUR_API_KEY") prompt = """ 以这只猫为基础,生成一张写实风格的图片, 场景为纽约街头,猫在街边人行道上行走, 背景为模糊的人腿和经过的黄色出租车。 """ image = Image.open("cat.png") response = client.models.generate_content( model="gemini-2.5-flash-image-preview", 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")
只需一句提示词,Nano Banana 能高质量修复、上色老照片。
from google import genai from PIL import Image from io import BytesIO client = genai.Client(api_key="YOUR_API_KEY") prompt = "恢复并为这张 1932 年的照片上色" image = Image.open("lunch.jpg") # 1932年摩天大楼午餐照片 response = client.models.generate_content( model="gemini-2.5-flash-image-preview", 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")
你可以输入多张图片,实现更复杂的编辑需求。
from google import genai from PIL import Image from io import BytesIO client = genai.Client(api_key="YOUR_API_KEY") prompt = "让这个女孩穿上这件T恤,背景保持不变。" image1 = Image.open("girl.png") image2 = Image.open("tshirt.png") response = client.models.generate_content( model="gemini-2.5-flash-image-preview", 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")
支持多轮“聊天”编辑,持续上下文,适合渐进式修改。
from google import genai from PIL import Image from io import BytesIO client = genai.Client(api_key="YOUR_API_KEY") chat = client.chats.create(model="gemini-2.5-flash-image-preview") # 第一步修改 response1 = chat.send_message([ "把猫换成一只孟加拉猫,其他都不变", Image.open("cat.png"), ]) # 保存图片... # 继续编辑 response2 = chat.send_message("给猫戴上一顶有趣的派对帽") # 保存图片...
小贴士:如果你发现多轮编辑后图片细节出现模糊或“漂移”,建议用最新图片+详细提示词开启新会话,可保持高品质。
- 极致具体:越具体越好,描述主体、颜色、光线、构图等细节,能更好控制结果
- 提供背景与意图:说明图片的用途、氛围等,模型会结合上下文做出更优创意
- 迭代优化:首次生成不一定完美,多次对话式微调能得到最佳图像
- 分步骤描述:复杂场景可拆解为多步提示
- 正面描述:少用否定词,尽量用期望场景的正向表达
- 摄影/电影术语:用“广角”、“微距”、“低机位视角”等术语控制画面风格
更多提示词建议可查阅 提示词指南。
- 视角转换(by @henrydaubrez)
- 角色一致性 Few-shot 学习(by @multimodalart)
- Google Maps “红箭头视角”变换(by @tokumin)
- 用简笔画生成图片(by @yachimat_manga)
- 静态图片生成 3D 模型(by @deedydas)
- 基于地理位置的 AR 体验(by @bilawalsidhu)
- 2D 地图转 3D 图形(by @demishassabis)
本教程涵盖了 Nano Banana(Gemini 2.5 Flash Image)的基础用法:环境搭建、图片生成与编辑、高阶技巧等。你已准备好将这些强大能力集成进自己的项目!
推荐进一步阅读:
- Google AI Studio
- Gemini API 官方文档
- AI Studio 内置 Pixshop 应用
如果你用 Nano Banana 做出了有趣的项目,欢迎联系或在 X(推特)上 @patloeber!
发布者:Ai探索者,转载请注明出处:https://javaforall.net/254710.html原文链接:https://javaforall.net
