Gemini 图像生成 (Nano Banana Pro)

使用 Google 的 Gemini API 生成和编辑图像。必须设置环境变量 GEMINI_API_KEY

默认模型

型号 分辨率 最适合
gemini-3-pro-image-preview gemini-3-pro-image-preview 1K-4K 所有图像生成(默认)

注意: 始终使用此 Pro 型号。仅在明确要求时才使用不同的模型。

快速参考

默认设置

  • 型号: gemini-3-pro-image-preview
  • 分辨率: 1K(默认,选项:1K、2K、4K)
  • 纵横比: 1:1(默认)

可用的宽高比

1:12:33:23:44:34:55:49:1616:921:9

可用分辨率

1K(默认)、2K4K

核心 API 模式

导入操作系统
从谷歌导入genai
来自 google.genai 导入类型

客户端 = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

# 基本生成(1K,1:1 - 默认)
响应 = client.models.generate_content(
    型号=“gemini-3-pro-image-preview”,
    content=["此处提示您"],
    config=types.GenerateContentConfig(
        response_modalities=['文本', '图像'],
    ),
)

对于response.parts中的部分:
    如果部分.文本:
        打印(部分.文本)
    elif 部分.inline_data:
        图像 = 部分.as_image()
        image.save("输出.png")

自定义分辨率和宽高比

来自 google.genai 导入类型

响应 = client.models.generate_content(
    型号=“gemini-3-pro-image-preview”,
    内容=[提示],
    config=types.GenerateContentConfig(
        response_modalities=['文本', '图像'],
        image_config=类型.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")

编辑图像

传递带有文本提示的现有图像:

从 PIL 导入图像

img = Image.open("输入.png")
响应 = client.models.generate_content(
    型号=“gemini-3-pro-image-preview”,
    content=["为该场景添加日落", img],
    config=types.GenerateContentConfig(
        response_modalities=['文本', '图像'],
    ),
)

多轮细化

使用聊天进行迭代编辑:

来自 google.genai 导入类型

聊天 = client.chats.create(
    型号=“gemini-3-pro-image-preview”,
    config=types.GenerateContentConfig(response_modalities=['TEXT', 'IMAGE'])
)

response = chat.send_message("为'Acme Corp'创建徽标")
# 保存第一张图片...

response = chat.send_message("使文本加粗并添加蓝色渐变")
# 保存优化后的图像...

提示最佳实践

逼真的场景

包括相机详细信息:镜头类型、灯光、角度、情绪。

“逼真的特写肖像,85mm 镜头,柔和的黄金时段光线,浅景深”

风格化艺术

明确指定样式:

“卡哇伊风格的快乐小熊猫贴纸,粗体轮廓,卡通阴影,白色背景”

图像中的文本

明确字体样式和位置:

“使用干净的无衬线、黑白、咖啡豆图案的文本‘Daily Grind’创建徽标”

产品模型

描述照明设置和表面:

“工作室照明的抛光混凝土产品照片,三点柔光箱设置,45 度角”

高级功能

Google 搜索基础

根据实时数据生成图像:

response = client.models.generate_content(
    model="gemini-3-pro-image-preview",
    contents=["Visualize today's weather in Tokyo as an infographic"],
    config=types.GenerateContentConfig(
        response_modalities=['TEXT', 'IMAGE'],
        tools=[{"google_search": {}}]
    )
)

多个参考图像(最多 14 个)

组合来自多个来源的元素:

response = client.models.generate_content(
    model="gemini-3-pro-image-preview",
    contents=[
        "Create a group photo of these people in an office",
        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("输出.jpg")

# 错误 - 将导致“图像与媒体类型不匹配”错误
image.save("output.png") # 创建带有 PNG 扩展名的 JPEG!

转换为 PNG(如果需要)

如果您特别需要 PNG 格式:

从 PIL 导入图像

# 使用 Gemini 生成
对于response.parts中的部分:
    如果部分.inline_data:
        img = 部分.as_image()
        # 通过以显式格式保存转换为 PNG
        img.save("输出.png", 格式="PNG")

验证图像格式

使用 file 命令检查实际格式与扩展名:

file image.png
# If output shows "JPEG image data" - rename to .jpg!

注释

  • 所有生成的图像都包含 SynthID 水印
  • Gemini 默认返回 JPEG 格式 - 始终使用 .jpg 扩展名
  • 仅图像模式 (responseModalities: ["IMAGE"]) 不适用于 Google 搜索接地
  • 对于编辑,以对话方式描述更改 - 模型理解语义屏蔽
  • 默认为 1K 分辨率以提高速度;当质量至关重要时使用 2K/4K