mcpField
G
gemini-imagegen

gemini-imagegen

github.com/dp-archive创意与媒体1.1k50 次安装34 周前更新
代码仓库

关于 gemini-imagegen

gemini-imagegen 是一项使用 Google Gemini 原生多模态图像生成功能来生成和编辑图像的技能,面向图像创作工作流。它提供适用于草稿或高分辨率任务的模型选项,并说明如何使用参考图和保持系列风格一致。

主要功能

  1. 01根据文本提示生成图像
  2. 02使用参考图编辑图像
  3. 03在两种 Gemini 图像模型间选择
  4. 04设置支持的宽高比
  5. 05用多张参考图进行构图和一致性控制
  6. 06提供批量生成与提示词设计指引

适用场景

  1. 01创作草图或高分辨率图像
  2. 02依据参考输入编辑图像
  3. 03生成风格一致的系列幻灯片图像

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dp-archive/archive --skill gemini-imagegen

安装后在 Claude Code 中输入/gemini-imagegen确认已加载。

详细介绍

Gemini 图像生成

通过 Google Gemini 原生多模态图像生成功能生成和编辑图像。

模型选择

模型 ID代号最适合最高分辨率
gemini-2.5-flash-imageNano Banana快速草稿、大批量生成、低延迟1K
gemini-3-pro-image-previewNano Banana Pro工作室级品质、文本渲染、复杂提示词4K

默认使用:gemini-3-pro-image-preview(Pro),除非需要优先考虑速度或成本。

设置

# Install (once)
# pip install google-genai

from google import genai
import os, base64

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

如果缺少 GEMINI_API_KEY,请指示用户将其设置为环境变量。 切勿要求用户在聊天中粘贴密钥。

文生图

from google import genai
from google.genai import types
import os

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

response = client.models.generate_content(
    model="gemini-3-pro-image-preview",
    contents="A photorealistic cat on a rainbow sofa",
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
    ),
)

# Extract and save
for part in response.candidates[0].content.parts:
    if part.inline_data is not None:
        with open("output.png", "wb") as f:
            f.write(part.inline_data.data)
        break

宽高比

通过 image_config 设置:

config=types.GenerateContentConfig(
    response_modalities=["TEXT", "IMAGE"],
    image_config=types.ImageConfig(
        aspect_ratio="16:9",  # for slides / widescreen
    ),
)

支持的比例:1:1、2:3、3:2、3:4、4:3、4:5、5:4、9:16、16:9、21:9

常见选择:

  • 幻灯片 / 演示文稿 → 16:9
  • 社交媒体 / 肖像 → 9:16 或 4:5
  • 方形缩略图 → 1:1

图像编辑(使用参考图像)

from google.genai import types
from pathlib import Path
import base64

ref_bytes = Path("input.jpg").read_bytes()

response = client.models.generate_content(
    model="gemini-3-pro-image-preview",
    contents=[
        types.Part(inline_data=types.Blob(mime_type="image/jpeg", data=base64.b64encode(ref_bytes).decode())),
        types.Part(text="Remove the background and replace with a sunset gradient"),
    ],
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
    ),
)

Pro 支持最多 14 张参考图像,用于多图合成;并支持最多 5 张人物参考图像,以保持角色 / 身份一致性。

批量生成(用于幻灯片)

生成多张图像时(例如每张幻灯片一张),请按顺序循环生成, 并使用带编号的文件名保存:

import os, time
from google import genai
from google.genai import types

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

prompts = [...]  # list of prompt strings

for i, prompt in enumerate(prompts, 1):
    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"),
        ),
    )
    for part in response.candidates[0].content.parts:
        if part.inline_data is not None:
            with open(f"slide_{i}.png", "wb") as f:
                f.write(part.inline_data.data)
            break
    time.sleep(1)  # rate limit courtesy

错误处理

  • 安全过滤器拦截:模型可能会拒绝其认为不安全的提示词。请调整提示词,减少歧义(移除暴力 / 成人 / 医疗图像相关措辞),然后重试。
  • 响应为空:如果 response.candidates 为空或不包含图像部分,提示词可能过于含糊。请添加具体的场景细节,然后重试。
  • 速率限制(429):使用指数延迟退避。默认值:time.sleep(2 ** attempt)。
  • 超时:设置合理的超时时间;Pro 模型处理复杂提示词可能需要 10–30 秒。

提示词最佳实践

  • 结构:场景 → 主体 → 风格 → 构图 → 限制条件
  • 务必指定艺术风格:“扁平矢量插画”“水彩画”“3D 渲染”“写实照片”
  • 描述光线和氛围:“柔和的漫射光”“戏剧性的轮廓光”“黄金时刻”
  • 对于图像中的文字:引用准确文本,并指定字体风格和位置
  • 对于幻灯片插图:添加“[一侧]留白”,为叠加文字腾出空间
  • 即使内容不是英文,也请使用英文提示词(生成质量更好)
  • 提示词保持在 500 词以内;具体明确,但不要冗长

多图组的风格一致性

生成一组图像时(例如幻灯片),请在每条提示词前添加风格前缀:

Style prefix: "flat vector illustration, soft pastel color palette, clean lines, minimal detail, 16:9 widescreen"

Slide 1 prompt: "{style_prefix}, a wide establishing shot of a modern office building at sunrise"
Slide 2 prompt: "{style_prefix}, a close-up of hands typing on a laptop keyboard"

这样可以确保所有生成图像在视觉上保持协调一致。

评论

支持 Markdown