关于 gemini-image-generator
通过 Google Gemini API 生成和编辑图像的 Python 命令行技能,支持文本生成、现有图像编辑及多张参考图输入,并可选择不同图像模型。
主要功能
- 01文本生成图像
- 02通过提示词编辑图像
- 03最多使用 14 张参考图
- 04选择模型和宽高比
- 05最高输出 4K 分辨率
适用场景
- 01制作应用图标和 UI 资源
- 02生成营销横幅
- 03创作游戏精灵和概念图
- 04为现有照片更改风格
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ ckorhonen/ claude-skills --skill gemini-image-generator安装后在 Claude Code 中输入/gemini-image-generator确认已加载。
附带文件scripts/generate_image.py
详细介绍
Gemini 图像生成器
概述
使用 Google 的 Gemini API 生成图像,支持文生图、图像编辑和多图参考输入。支持快速的 Gemini 2.5 Flash Image 模型,以及最高支持 4K 分辨率的高质量 Gemini 3.1 Flash Image 模型。
模型命名说明(2026 年): Google 的图像生成模型采用「Nano Banana」品牌。
gemini-2.5-flash-image-preview称为「Nano Banana」,gemini-3.1-flash-image-preview称为「Nano Banana 2」。它们与对话式 Gemini 模型不同。请参阅模型参考。
适用场景
- 生成应用图标、徽标和 UI 素材
- 创建营销视觉素材和宣传图形
- 使用 AI 生成的占位图制作 UI 设计原型
- 生成游戏精灵图和 2D 素材
- 创建概念艺术和情绪板
- 使用文本提示词编辑或修改现有图像
- 使用参考图像进行风格迁移
前提条件
- Python 3.9+
google-genai包GEMINI_API_KEY环境变量
安装
pip install google-genai
获取 API 密钥
- 前往 Google AI Studio
- 使用 Google 账号登录
- 点击「Create API Key」
- 复制密钥并将其设为环境变量:
export GEMINI_API_KEY="your-api-key"
将其添加到 shell 配置文件(~/.zshrc 或 ~/.bashrc)以便持久生效:
echo 'export GEMINI_API_KEY="your-api-key"' >> ~/.zshrc
快速入门
生成简单图像:
python scripts/generate_image.py -p "A fluffy orange cat sitting on a windowsill, warm sunlight, cozy atmosphere"
按指定宽高比生成:
python scripts/generate_image.py -p "Modern tech startup banner" -a 16:9 -o banner.png
编辑现有图像:
python scripts/generate_image.py -p "Make the sky more dramatic with sunset colors" -i photo.jpg -o edited.png
命令参考
python scripts/generate_image.py [options]
Required:
-p, --prompt TEXT Text prompt describing the image
Optional:
-o, --output PATH Output file path (default: auto-generated)
-m, --model MODEL Model to use (default: gemini-3.1-flash-image-preview)
-a, --aspect-ratio RATIO Aspect ratio (default: 1:1)
-s, --size SIZE Image size: 1K, 2K, 4K (default: 1K, Pro only)
-i, --input-image PATH Input image for editing mode
-r, --reference-images Reference image(s), can be repeated (max 14)
-v, --verbose Show detailed progress
模型
| 模型 | 分辨率 | 最适用场景 | 别名 |
|---|---|---|---|
gemini-3.1-flash-image-preview | 最高 4K | 最新模型,速度快且质量高 | Nano Banana 2 |
gemini-2.5-flash-image-preview | 最高 1K | 快速迭代、原型制作、批量生成 | Nano Banana |
gemini-3-pro-image-preview | 最高 4K | 上一代高质量备用模型 | Nano Banana Pro |
建议将 gemini-3.1-flash-image-preview 模型作为默认模型。若要更快、更省成本地生成,请使用 gemini-2.5-flash-image-preview:
python scripts/generate_image.py -p "Quick concept sketch" -m gemini-2.5-flash-image-preview
API 使用说明: 所有模型都使用相同的
google-genaiSDK,并在配置中设置response_modalities=['Image']。这些模型需要GEMINI_API_KEY,并使用generate_content端点(而非单独的图像端点)。
宽高比
| 比例 | 用途 |
|---|---|
1:1 | 应用图标、头像、缩略图 |
2:3 | 人像照片、书籍封面 |
3:2 | 风景照片、明信片 |
3:4 | 人像照片、社交媒体帖子 |
4:3 | 传统照片、演示文稿 |
4:5 | Instagram 帖子、人像社交媒体内容 |
5:4 | 大幅面印刷品 |
9:16 | 快拍、竖屏视频、手机壁纸 |
16:9 | 宽屏横幅、视频缩略图、页眉 |
21:9 | 超宽横幅、电影风格页眉 |
图像尺寸
仅适用于 Gemini 3 Pro 模型:
| 尺寸 | 分辨率 | 用途 |
|---|---|---|
1K | 1024px | 网页图形、缩略图 |
2K | 2048px | 印刷材料、精细图形 |
4K | 4096px | 高分辨率印刷品、大型显示屏 |
python scripts/generate_image.py -p "Detailed landscape" -s 4K -o landscape_4k.png
提示词编写指南
提示词结构
使用以下公式编写有效的提示词:
[Subject] + [Style] + [Details] + [Quality modifiers]
技巧
1. 明确主体细节
Bad: "a cat"
Good: "a fluffy orange tabby cat sitting on a windowsill"
2. 指定艺术风格
- 照片级真实、卡通、动漫、油画、水彩画
- 数字艺术、3D 渲染、像素艺术、矢量插画
- 特定风格:「吉卜力工作室风格」「赛博朋克美学」
3. 补充环境和光照
- 「黄金时段光线」「戏剧性阴影」「柔和的环境光」
- 「霓虹灯照亮的城市景观」「温馨的室内环境」「薄雾笼罩的森林」
4. 添加质量修饰词
- 「高质量」「细节丰富」「专业」
- 「焦点清晰」「影棚灯光」「电影感」
5. 指定构图
- 「居中构图」「三分法」
- 「特写」「远景」「鸟瞰视角」「等距视角」
按用途分类的提示词示例
应用图标
Minimalist app icon for a weather app, blue gradient background,
white cloud with golden sun rays, flat design, rounded corners,
iOS style, clean and modern
营销横幅
Professional tech startup banner, abstract geometric shapes
flowing from left to right, purple and blue gradient,
modern and clean aesthetic, corporate style
游戏精灵图
Pixel art character sprite, fantasy warrior with glowing sword,
32x32 style, transparent background, retro 16-bit game aesthetic,
vibrant colors
产品照片
Professional product photo of wireless earbuds on white background,
soft shadows, studio lighting, minimalist composition,
commercial photography style
概念艺术
Futuristic city skyline at sunset, flying vehicles between
towering skyscrapers, neon lights reflecting on wet streets,
cyberpunk atmosphere, cinematic composition, detailed
UI 模型素材
Abstract gradient background for mobile app, soft purple to pink
transition, subtle geometric patterns, modern and minimal,
suitable for dark text overlay
生成模式
文本生成图像
根据文本描述生成图像:
python scripts/generate_image.py -p "Your description here" -o output.png
图像编辑
使用文本提示词修改现有图像:
python scripts/generate_image.py \
-p "Change the background to a tropical beach at sunset" \
-i original.jpg \
-o edited.png
多图参考
使用最多 14 张参考图像来引导风格或内容:
python scripts/generate_image.py \
-p "Create a new character in this art style" \
-r style_ref1.png \
-r style_ref2.png \
-o new_character.png
示例
生成应用图标
# iOS-style weather icon
python scripts/generate_image.py \
-p "Minimalist weather app icon, blue sky gradient, white fluffy cloud, sun peeking out, flat design, rounded square, iOS 17 style" \
-a 1:1 \
-o weather_icon.png
# Fitness app icon
python scripts/generate_image.py \
-p "Fitness app icon, running figure silhouette, orange to red gradient background, energetic and dynamic, modern flat design" \
-a 1:1 \
-o fitness_icon.png
创建营销素材
# Website hero banner
python scripts/generate_image.py \
-p "Abstract tech hero banner, flowing data visualization, dark blue background with glowing cyan accents, futuristic and professional" \
-a 21:9 \
-s 2K \
-o hero_banner.png
# Social media post
python scripts/generate_image.py \
-p "Motivational quote background, soft sunrise gradient, minimalist mountain silhouette, peaceful and inspiring" \
-a 4:5 \
-o social_post_bg.png
生成游戏素材
# Character sprite
python scripts/generate_image.py \
-p "Pixel art hero character, knight with blue cape and silver armor, idle pose, transparent background, 16-bit retro style" \
-a 1:1 \
-o knight_sprite.png
# Environment tile
python scripts/generate_image.py \
-p "Grass tile for top-down RPG, seamless pattern, vibrant green with small flowers, pixel art style, 32x32 aesthetic" \
-a 1:1 \
-o grass_tile.png
编辑照片
# Change background
python scripts/generate_image.py \
-p "Replace background with a cozy coffee shop interior" \
-i portrait.jpg \
-o portrait_coffee_shop.png
# Style enhancement
python scripts/generate_image.py \
-p "Enhance with dramatic cinematic color grading, increase contrast, add film grain" \
-i landscape.jpg \
-o landscape_cinematic.png
故障排除
“未设置 GEMINI_API_KEY 环境变量”
设置 API 密钥:
export GEMINI_API_KEY="your-api-key"
“超出速率限制”
等待几分钟后重试。对于批量操作,请在请求之间添加延迟。
“违反内容政策”
修改提示词,避免包含违反 Google 使用政策的内容。可以尝试:
- 使用更通用的描述
- 避免使用特定品牌名称或受版权保护的角色
- 移除可能敏感的内容
“响应中没有图像”
模型有时会返回文本,而不是图像。可以尝试:
- 让提示词更加具体
- 在提示词中添加“生成一张……的图像”
- 使用其他宽高比
“不支持的图像格式”
输入图像支持的格式:PNG、JPEG、WebP
尺寸选项不起作用
gemini-3.1-flash-image-preview 和 gemini-3-pro-image-preview 支持尺寸选项(2K、4K)。gemini-2.5-flash-image-preview 模型生成的图像最高为 1024px。
直接使用 API(Python SDK)
如果你希望不使用 CLI,直接调用 API:
import os
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
response = client.models.generate_content(
model="gemini-3.1-flash-image-preview",
contents="A minimalist app icon for a weather app, blue gradient, white cloud",
config=types.GenerateContentConfig(
response_modalities=["Image", "Text"],
# aspect_ratio and image_size options depend on model support
)
)
for part in response.candidates[0].content.parts:
if part.inline_data is not None:
image = Image.open(BytesIO(part.inline_data.data))
image.save("output.png")
print("Saved to output.png")
elif part.text:
print(part.text)
重要: 图像生成使用
generate_content端点(而非单独的图像端点)。将response_modalities设置为包含"Image",即可启用图像输出。
最佳实践
- 从简单开始:先使用清晰、简洁的提示词,再逐步迭代
- 选择合适的模型:追求速度时使用
gemini-2.5-flash-image-preview,追求质量和 4K 时使用gemini-3.1-flash-image-preview - 根据用途选择宽高比:横幅使用 16:9,图标使用 1:1
- 保存高质量版本:需要精细素材时使用 4K
- 迭代提示词:细微调整也可能显著影响结果
- 使用参考图像:确保多次生成的风格一致
- 添加质量修饰词:“高质量”、“细节丰富”、“专业”
- 说明你不想要的内容:“无文字”、“简单背景”、“无人”