Generating Consistent Imagery with Gemini 🍌
Source notebook
Repo path:
gemini/use-cases/media-generation/consistent_imagery_generation.ipynb· Open on GitHub · intermediate
Generates a consistent robot image story from one archive image using Gemini 2.5 Flash Image.
Summary
This notebook teaches how to reuse an existing image as a visual reference and generate a sequence of consistent new images with Gemini 2.5 Flash Image. It sets up the Google Gen AI SDK, loads an archive image, creates a robot character sheet, generates successive scenes from prior assets, visualizes the asset lineage as a directed graph, and saves prompts plus source links in PNG metadata.
Key code patterns
Unified Gen AI client
from google import genai
check_environment()
client = genai.Client()
check_configuration(client)Creates one SDK client that can use either Agent Platform or Google AI Studio configuration.
Image generation config
GENERATION_CONFIG = GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=ImageConfig(aspect_ratio="16:9"),
)Constrains output to image responses and requests a supported 16:9 aspect ratio.
Reference-guided generation
contents = [*sources, prompt] if sources else prompt
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=contents,
config=GENERATION_CONFIG,
)Combines prior PIL images with a prompt so new images preserve character and scene context.
Retry transient client errors
tenacity.Retrying(
stop=tenacity.stop_after_attempt(7),
wait=tenacity.wait_incrementing(start=10, increment=1),
retry=tenacity.retry_if_exception(should_retry_request),
reraise=True,
)Retries selected 400 and 429 ClientError cases called out for first Cloud Storage access and low quota.
Asset lineage graph
graph = nx.DiGraph(assets=assets)
for asset in assets.values():
graph.add_node(asset.id, asset=asset)
for source_id in asset.source_ids:
graph.add_edge(source_id, asset.id)Tracks generated-from relationships between archive, character sheet, and scene assets.
Prompt metadata persistence
metadata = PngInfo()
metadata.add_text("source_ids", "\n".join(asset.source_ids))
metadata.add_text("prompt", asset.prompt)
asset.pil_image.save(image_path, pnginfo=metadata)Stores source asset IDs and prompts inside PNG metadata for local restore.
Models & APIs used
- Models: gemini-2.5-flash-image
- APIs / services: Agent Platform, Gemini API, Google AI Studio
- SDKs / libraries:
google-genai,networkx,tenacity,matplotlib,pillow
When to use this
Use this pattern to generate a consistent visual story or asset set from existing images using reference images and iterative prompts.
Gotchas & caveats
- Agent Platform requires a Google Cloud project and the Agent Platform API enabled.
- Preview models must use location global.
- Google AI Studio requires a Gemini API key.
- Colab secrets or environment variables are needed for SDK configuration outside managed environments.
- Temporary projects may hit 1 QPM quota and trigger 429 retries.
- First Cloud Storage access in a workshop project may trigger a retryable 400 while service agents are provisioned.
- Repeated generations can vary when prompt details are left open.
- Closed eyes were described as difficult to get consistently after many accumulated transformations.
Best practices
- Use environment variables or Colab Secrets instead of hardcoding API configuration.
- Use a character sheet as a reusable design reference for future image-generation tasks.
- Refer explicitly to input images, such as Image 1 and Image 2, to avoid ambiguity.
- Clarify removed or changed objects, such as no longer holding the map or removing ice axes.
- Spend time refining the first scene because it cascades into later generated scenes.
- Use previous scenes plus the character sheet to preserve context across successive generations.
- Save prompts and source IDs in image metadata so assets can be restored without a database.
- Focus significant changes over iterative steps or create character sheets upfront for more deterministic control.
Related
- Concepts: Gemini Capabilities · Vision · Image & Video Generation
- Entities: Google GenAI SDK · Vertex AI · Imagen · Gemini
- Area: Gemini Notebooks
- Best practices: Gemini Capabilities - Best Practices · Vision - Best Practices · Image & Video Generation - Best Practices