Nano Banana image editing & multi-image
For model IDs and choosing an endpoint, see the Nano Banana overview.
Key pointThe native API puts the image and the text in the same
parts; the OpenAI-compatible API uses /v1/images/edits.Python (native)
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.ai/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=["Make the apple green, keep everything else the same", 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)- Multi-image composition: put several
inlineDataitems in the samepartsand describe the role of each image in the text, e.g. “Place the person from the first image into the scene from the second image”. - Keeping the aspect ratio: pass
aspectRatiowhen editing too; otherwise the model decides the output ratio, which may not match the original. - Reference image billing: reference images are billed as input tokens. We measured 1,120 tokens for one 1024×1024 reference image; see the
IMAGEentry ofpromptTokensDetailsin the response.
If you run into errors, see Troubleshooting errors.
Last updated on