Gemini图像生成Skill gemini-imagegen

该技能使用谷歌Gemini API进行图像生成和编辑,支持从文本提示创建图像、编辑现有图像、应用风格转移、生成带文本的logo、创建贴纸、产品模拟等任务。适用于AIGC、人工智能生成内容、图像处理、多轮细化、参考图像组合,方便SEO搜索如Gemini API、图像生成、图像编辑、AIGC应用。

AIGC 0 次安装 0 次浏览 更新于 3/9/2026

name: gemini-imagegen description: 此技能应使用谷歌Gemini API(Nano Banana Pro)进行图像生成和编辑。适用于从文本提示创建图像、编辑现有图像、应用风格转移、生成带文本的logo、创建贴纸、产品模拟或任何图像生成/操作任务。支持文本到图像、图像编辑、多轮细化以及从多个参考图像组合生成。

Gemini 图像生成(Nano Banana Pro)

使用谷歌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镜头,柔和金色小时光,浅景深”

风格化艺术

明确指定风格:

“一个卡哇伊风格的快乐红熊猫贴纸,粗轮廓,赛璐璐着色,白色背景”

图像中的文本

明确字体样式和位置:

“创建一个logo,文本为’Daily Grind’,使用干净无衬线字体,黑白配色,咖啡豆主题”

产品模拟

描述光照设置和表面:

“工作室光照产品照片,置于抛光混凝土上,三点软盒设置,45度角度”

高级功能

谷歌搜索基础

基于实时数据生成图像:

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 image data” - 重命名为 .jpg!

注意事项

  • 所有生成图像包含SynthID水印
  • Gemini默认返回JPEG格式 - 始终使用 .jpg 扩展名
  • 仅图像模式(responseModalities: ["IMAGE"])与谷歌搜索基础不兼容
  • 编辑时,描述性交流更改—模型理解语义掩码
  • 默认为1K分辨率以提速;在质量关键时使用2K/4K