Nano Banana の画像編集と複数画像の合成
モデル ID とエンドポイントの選び方は Nano Banana 概要を参照してください。
ポイントネイティブエンドポイントでは画像とテキストを同じ
parts に入れます。OpenAI 互換エンドポイントでは /v1/images/edits を使います。Python(ネイティブ)
edit.py
from google import genai
from google.genai import types
client = genai.Client(
api_key="YOUR_OFOX_API_KEY",
http_options={"api_version": "v1beta", "base_url": "https://api.ofox.io/gemini"},
)
with open("apple.png", "rb") as f:
image = types.Part.from_bytes(data=f.read(), mime_type="image/png")
resp = client.models.generate_content(
model="google/gemini-3.1-flash-image",
contents=["リンゴを緑色に変え、それ以外はそのままにする", image],
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio="1:1", image_size="1K"),
),
)
for part in resp.candidates[0].content.parts:
if part.inline_data:
with open("apple_green.png", "wb") as f:
f.write(part.inline_data.data)- 複数画像の合成:同じ
partsに複数のinlineDataを入れ、テキストで各画像の役割を説明します。例:「1 枚目の画像の人物を 2 枚目の画像のシーンに配置する」。 - アスペクト比の維持:画像編集でも
aspectRatioを指定する必要があります。指定しないと出力のアスペクト比はモデルが決定し、元画像と一致しない場合があります。 - 参照画像の課金:参照画像は入力トークンとして課金されます。実測では 1024×1024 の参照画像 1 枚で 1,120 トークンでした。レスポンスの
promptTokensDetailsのIMAGE要素で確認できます。
エラーが発生した場合は エラー対処を参照してください。
最終更新日