使用 Gemini(又称 Nano Banana 和 Nano Banana Pro)生成图片
本文介绍了使用Google Gemini模型进行图片生成和编辑的方法。通过Python代码示例展示了两种主要功能:文本转图片(如生成纳米香蕉餐厅图片)和图片+文字转图片(如合成猫咪吃香蕉的图片)。文章还详细介绍了Gemini 3 Pro Image模型的高级特性,包括高分辨率输出(1K/2K/4K)、文字渲染优化、Google搜索数据整合,以及支持最多14张参考图片混合的能力(含6张对象图和5张人
·
图片生成(文本转图片)
from google import genai
from google.genai import types
from PIL import Image
client = genai.Client()
prompt = (
"Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"
)
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=[prompt],
)
for part in response.parts:
if part.text is not None:
print(part.text)
elif part.inline_data is not None:
image = part.as_image()
image.save("generated_image.png")
注意:python要提前安装Pillow
pip install Pillow

图片编辑(文字和图片转图片)
from google import genai
from google.genai import types
from PIL import Image
client = genai.Client()
prompt = (
"Create a picture of my cat eating a nano-banana in a "
"fancy restaurant under the Gemini constellation",
)
image = Image.open("/path/to/cat_image.png")
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=[prompt, image],
)
for part in response.parts:
if part.text is not None:
print(part.text)
elif part.inline_data is not None:
image = part.as_image()
image.save("generated_image.png")
Gemini 3 Pro Image (gemini-3-pro-image-preview) 是一款先进的图片生成和编辑模型,针对专业资源制作进行了优化。Gemini 1.5 Pro 旨在通过高级推理来应对最具挑战性的工作流程,擅长处理复杂的多轮创建和修改任务。
- 高分辨率输出:内置 1K、2K 和 4K 视觉效果生成功能。
- 高级文字渲染:能够为信息图表、菜单、图表和营销素材资源生成清晰易读的风格化文字。
- 使用 Google 搜索进行接地:模型可以使用 Google 搜索作为工具来验证事实,并根据实时数据(例如当前天气地图、股票图表、近期活动)生成图像。
- 思考模式:模型会利用“思考”过程来推理复杂的提示。它会生成临时“思维图像”(在后端可见,但不收费),以在生成最终的高质量输出之前优化构图。
- 最多 14 张参考图片:您现在最多可以混合使用 14 张参考图片来生成最终图片。
最多可使用 14 张参考图片
借助 Gemini 3 Pro 预览版,您最多可以混合 14 张参考图片。这 14 张图片可以包含以下内容:
- 最多 6 张高保真对象图片,用于包含在最终图片中
-
最多 5 张人像照片,以保持角色一致性
from google import genai
from google.genai import types
from PIL import Image
prompt = "An office group photo of these people, they are making funny faces."
aspect_ratio = "5:4" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9"
resolution = "2K" # "1K", "2K", "4K"
client = genai.Client()
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=[
prompt,
Image.open('person1.png'),
Image.open('person2.png'),
Image.open('person3.png'),
Image.open('person4.png'),
Image.open('person5.png'),
],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE'],
image_config=types.ImageConfig(
aspect_ratio=aspect_ratio,
image_size=resolution
),
)
)
for part in response.parts:
if part.text is not None:
print(part.text)
elif image:= part.as_image():
image.save("office.png")
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)