gemini-imagegen について
gemini-imagegenは、Google Geminiのネイティブなマルチモーダル画像生成機能で画像を生成・編集するスキルです。下書きや高解像度の作成に応じたモデル選択に加え、参照画像やシリーズの一貫性に関する手順を提供します。
主な機能
- 01テキストプロンプトから画像を生成
- 02参照画像を使った画像編集
- 032種類のGemini画像モデルを選択
- 04対応するアスペクト比を設定
- 05複数の参照画像で構図と一貫性を調整
- 06バッチ生成とプロンプト設計のガイド
活用シーン
- 01下書きや高解像度画像の作成
- 02参照画像に基づく画像編集
- 03統一したスタイルのスライド画像を連続生成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ dp-archive/ archive --skill gemini-imagegenインストール後、Claude Code で/gemini-imagegenを入力して読み込まれたか確認します。
詳細
Gemini Image Generation
Google Gemini のネイティブなマルチモーダル画像生成機能を使って、画像を生成・編集します。
Model Selection
| モデル ID | コードネーム | 最適な用途 | 最大解像度 |
|---|---|---|---|
gemini-2.5-flash-image | Nano Banana | 高速な下書き、大量生成、低レイテンシ | 1K |
gemini-3-pro-image-preview | Nano Banana Pro | スタジオ品質、テキスト描画、複雑なプロンプト | 4K |
デフォルトでは、速度やコストが懸念される場合を除き、gemini-3-pro-image-preview(Pro)を使用します。
Setup
# 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 が設定されていない場合は、環境変数として設定するようユーザーに案内してください。
チャットでキーを貼り付けるようユーザーに求めないでください。
Text-to-Image
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
Aspect Ratio
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
Image Editing (with reference image)
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 枚の人物参照画像をサポートします。
Batch Generation (for slides)
複数の画像(例:スライドごとに 1 枚)を生成する場合は、順番にループ処理し、連番のファイル名で保存します:
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
Error Handling
- 安全性フィルターによるブロック:モデルは、安全でないと判断したプロンプトを拒否することがあります。プロンプトの曖昧さを減らして(暴力的/成人向け/医療関連の画像表現を削除して)再試行してください。
- 空の応答:
response.candidatesが空、または画像パーツを含まない場合、プロンプトが曖昧すぎる可能性があります。具体的なシーンの詳細を追加して再試行してください。 - レート制限(429):指数関数的な遅延を設定して再試行してください。デフォルト:
time.sleep(2 ** attempt)。 - タイムアウト:適切なタイムアウトを設定してください。Pro モデルでは複雑なプロンプトに 10~30 秒かかる場合があります。
Prompt Best Practices
- 構成:シーン → 主題 → スタイル → 構図 → 制約
- アートスタイルを必ず指定します:「フラットなベクターイラスト」「水彩画」「3D レンダリング」「フォトリアルな写真」
- 照明と雰囲気を含めます:「柔らかな拡散光」「ドラマチックなリムライト」「ゴールデンアワー」
- 画像内のテキストについては、正確なテキストを引用し、フォントスタイルと配置を指定します
- スライド用のイラストでは、テキストを重ねるスペースを確保するため、「[side] 側にネガティブスペースを設ける」と追加します
- 英語以外のコンテンツでも、英語のプロンプトを使用します(生成品質が向上します)
- プロンプトは 500 語未満にし、具体的にしつつ冗長にならないようにします
Style Consistency for Multi-Image Sets
一連の画像(例:スライドデッキ)を生成する場合は、すべてのプロンプトの先頭に スタイルプレフィックス を追加します:
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 対応