名称: gemini-imagegen 描述: 该技能应用于使用Gemini API(Nano Banana Pro)生成和编辑图像。适用于从文本提示创建图像、编辑现有图像、应用风格转移、生成带文本的logo、创建贴纸、产品模型或任何图像生成/处理任务。支持文本到图像、图像编辑、多轮细化和从多个参考图像组合。
Gemini 图像生成(Nano Banana Pro)
使用 Google 的 Gemini API 生成和编辑图像。必须设置环境变量 GEMINI_API_KEY。
默认模型
| 模型 | 分辨率 | 最佳用途 |
|---|---|---|
gemini-3-pro-image-preview |
1K-4K | 所有图像生成(默认) |
注意: 始终使用此 Pro 模型。仅在明确请求时使用其他模型。
快速参考
默认设置
- 模型:
gemini-3-pro-image-preview - 分辨率: 1K(默认,选项:1K, 2K, 4K)
- 纵横比: 1:1(默认)
可用纵横比
1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9
可用分辨率
1K(默认), 2K, 4K
核心 API 模式
import os
from google import genai
from google.genai import types
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
# 基本生成(1K, 1:1 - 默认)
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=["您的提示在这里"],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE'],
),
)
for part in response.parts:
if part.text:
print(part.text)
elif part.inline_data:
image = part.as_image()
image.save("output.png")
自定义分辨率和纵横比
from google.genai import types
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=[prompt],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE'],
image_config=types.ImageConfig(
aspect_ratio="16:9", # 宽屏格式
image_size="2K" # 更高分辨率
),
)
)
分辨率示例
# 1K(默认)- 快速,适合预览
image_config=types.ImageConfig(image_size="1K")
# 2K - 平衡质量和速度
image_config=types.ImageConfig(image_size="2K")
# 4K - 最高质量,较慢
image_config=types.ImageConfig(image_size="4K")
纵横比示例
# 方形(默认)
image_config=types.ImageConfig(aspect_ratio="1:1")
# 横向宽屏
image_config=types.ImageConfig(aspect_ratio="16:9")
# 超宽全景
image_config=types.ImageConfig(aspect_ratio="21:9")
# 纵向
image_config=types.ImageConfig(aspect_ratio="9:16")
# 照片标准
image_config=types.ImageConfig(aspect_ratio="4:3")
编辑图像
传递现有图像和文本提示:
from PIL import Image
img = Image.open("input.png")
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=["为此场景添加日落", img],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE'],
),
)
多轮细化
使用聊天进行迭代编辑:
from google.genai import types
chat = client.chats.create(
model="gemini-3-pro-image-preview",
config=types.GenerateContentConfig(response_modalities=['TEXT', 'IMAGE'])
)
response = chat.send_message("为 'Acme Corp' 创建一个 logo")
# 保存第一张图像...
response = chat.send_message("使文本更粗体并添加蓝色渐变")
# 保存细化后的图像...
提示最佳实践
逼真场景
包括相机细节:镜头类型、光线、角度、氛围。
“一个逼真的特写肖像,85mm镜头,柔和的黄金小时光,浅景深”
风格化艺术
明确指定风格:
“一个卡哇伊风格的快乐红熊猫贴纸,粗轮廓,赛璐珞着色,白色背景”
图像中的文本
明确字体风格和位置:
“创建一个带文本 ‘Daily Grind’ 的logo,使用简洁的无衬线字体,黑白配色,咖啡豆图案”
产品模型
描述光线设置和表面:
“工作室光线下产品照片,在抛光混凝土地面上,三点软箱设置,45度角度”
高级功能
Google 搜索基础
基于实时数据生成图像:
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=["以信息图表形式可视化今天东京的天气"],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE'],
tools=[{"google_search": {}}]
)
)
多个参考图像(最多14个)
从多个来源组合元素:
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=[
"在办公室中创建这些人的合影",
Image.open("person1.png"),
Image.open("person2.png"),
Image.open("person3.png"),
],
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE'],
),
)
重要:文件格式和媒体类型
关键: Gemini API 默认以 JPEG 格式返回图像。保存时,始终使用 .jpg 扩展名以避免媒体类型不匹配。
# 正确 - 使用 .jpg 扩展名(Gemini 返回 JPEG)
image.save("output.jpg")
# 错误 - 将导致“图像不匹配媒体类型”错误
image.save("output.png") # 创建了 JPEG 但使用 PNG 扩展名!
转换为 PNG(如果需要)
如果特别需要 PNG 格式:
from PIL import Image
# 使用 Gemini 生成
for part in response.parts:
if part.inline_data:
img = part.as_image()
# 通过指定格式保存转换为 PNG
img.save("output.png", format="PNG")
验证图像格式
使用 file 命令检查实际格式与扩展名:
file image.png
# 如果输出显示“JPEG 图像数据” - 重命名为 .jpg!
备注
- 所有生成的图像包含 SynthID 水印
- Gemini 默认返回 JPEG 格式 - 始终使用
.jpg扩展名 - 仅图像模式(
responseModalities: ["IMAGE"])不能与 Google 搜索基础一起使用 - 对于编辑,以对话方式描述更改——模型理解语义掩码
- 默认使用 1K 分辨率以提高速度;质量关键时使用 2K/4K