{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "f705f4be70e9"
      },
      "outputs": [],
      "source": [
        "# Copyright 2025 Google LLC\n",
        "#\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "#     https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2408c01609e3"
      },
      "source": [
        "# Nano Banana 🍌: Gemini 2.5 Flash Image Recipes\n",
        "\n",
        "<table align=\"left\">\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://www.gstatic.com/pantheon/images/bigquery/welcome_page/colab-logo.svg\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fnano-banana%2Fnano_banana_recipes.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/nano-banana/nano_banana_recipes.ipynb\">\n",
        "      <img src=\"https://www.gstatic.com/images/branding/gcpiconscolors/vertexai/v1/32px.svg\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://console.cloud.google.com/bigquery/import?url=https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\">\n",
        "      <img src=\"https://www.gstatic.com/images/branding/gcpiconscolors/bigquery/v1/32px.svg\" alt=\"BigQuery Studio logo\"><br> Open in BigQuery Studio\n",
        "    </a>\n",
        "  </td>\n",
        "  <td style=\"text-align: center\">\n",
        "    <a href=\"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\">\n",
        "      <img width=\"32px\" src=\"https://raw.githubusercontent.com/primer/octicons/refs/heads/main/icons/mark-github-24.svg\" alt=\"GitHub logo\"><br> View on GitHub\n",
        "    </a>\n",
        "  </td>\n",
        "</table>\n",
        "\n",
        "<div style=\"clear: both;\"></div>\n",
        "\n",
        "<b>Share to:</b>\n",
        "\n",
        "<a href=\"https://www.linkedin.com/sharing/share-offsite/?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/8/81/LinkedIn_icon.svg\" alt=\"LinkedIn logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://bsky.app/intent/compose?text=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/7/7a/Bluesky_Logo.svg\" alt=\"Bluesky logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://twitter.com/intent/tweet?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/5a/X_icon_2.svg\" alt=\"X logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://reddit.com/submit?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://redditinc.com/hubfs/Reddit%20Inc/Brand/Reddit_Logo.png\" alt=\"Reddit logo\">\n",
        "</a>\n",
        "\n",
        "<a href=\"https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/nano-banana/nano_banana_recipes.ipynb\" target=\"_blank\">\n",
        "  <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg\" alt=\"Facebook logo\">\n",
        "</a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8b8faa59b492"
      },
      "source": [
        "| Author |\n",
        "| --- |\n",
        "| [Chouaieb Nemri](https://github.com/cnemri) |"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5ff8a1238700"
      },
      "source": [
        "Welcome to Nano Banana Recipes! This notebook offers a collection of practical examples and best practices for leveraging the Gemini 2.5 Flash model for a variety of image generation and editing tasks.\n",
        "\n",
        "Each recipe is designed to be clear, easy to follow, and adaptable for your own projects, demonstrating a specific capability of the model."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "f15495c1cd4d"
      },
      "source": [
        "## Environment Setup\n",
        "\n",
        "Begin by installing the required Python packages for the notebook."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "f43cd1090b9e"
      },
      "outputs": [],
      "source": [
        "%pip install --upgrade --quiet google-genai pillow"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6f0150a75c61"
      },
      "source": [
        "### Authenticate your notebook environment (Colab only)\n",
        "\n",
        "If you're running this notebook on Google Colab, run the cell below to authenticate your environment."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "d8f42443eb06"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "\n",
        "if \"google.colab\" in sys.modules:\n",
        "    from google.colab import auth\n",
        "\n",
        "    auth.authenticate_user()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "b6d46b3a5abf"
      },
      "source": [
        "### Configure Google Cloud Project\n",
        "\n",
        "To use Vertex AI, you need a Google Cloud project with the [Vertex AI API enabled](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
        "\n",
        "For more details, see the documentation on [setting up a project and development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cae30ea6ed0d"
      },
      "outputs": [],
      "source": [
        "# Use the environment variable if the user doesn't provide Project ID.\n",
        "import os\n",
        "\n",
        "# fmt: off\n",
        "PROJECT_ID = \"[your-project-id]\"  # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n",
        "# fmt: on\n",
        "if not PROJECT_ID or PROJECT_ID == \"[your-project-id]\":\n",
        "    PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n",
        "\n",
        "LOCATION = \"global\"\n",
        "\n",
        "from google import genai\n",
        "\n",
        "client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "573931bbe0eb"
      },
      "source": [
        "## Core Functions and Utilities\n",
        "\n",
        "This section contains the helper functions that will be used throughout the notebook to streamline image generation and processing tasks."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "f5afcdede8fa"
      },
      "outputs": [],
      "source": [
        "import io\n",
        "\n",
        "from IPython.display import display\n",
        "from PIL import Image\n",
        "from google.genai import types\n",
        "\n",
        "# --- Model and Generation Configuration ---\n",
        "MODEL_NAME = \"gemini-2.5-flash-image-preview\"\n",
        "GENERATION_CONFIG = types.GenerateContentConfig(\n",
        "    temperature=1,\n",
        "    top_p=0.95,\n",
        "    max_output_tokens=32768,\n",
        "    response_modalities=[\"TEXT\", \"IMAGE\"],\n",
        ")\n",
        "\n",
        "\n",
        "def create_blank_canvas(\n",
        "    aspect_ratio: str = \"1:1\",\n",
        "    width: int | None = None,\n",
        "    height: int | None = None,\n",
        "    color: str = \"white\",\n",
        ") -> types.Part:\n",
        "    \"\"\"Creates a blank image canvas with a specified aspect ratio or custom dimensions.\"\"\"\n",
        "    aspect_ratios = {\n",
        "        \"1:1\": (1024, 1024),\n",
        "        \"3:4\": (768, 1024),\n",
        "        \"4:3\": (1024, 768),\n",
        "        \"9:16\": (720, 1280),\n",
        "        \"16:9\": (1280, 720),\n",
        "    }\n",
        "\n",
        "    if aspect_ratio == \"custom\":\n",
        "        if not all([width, height]):\n",
        "            raise ValueError(\"Width and height are required for custom aspect ratio.\")\n",
        "        final_width, final_height = width, height\n",
        "    elif aspect_ratio in aspect_ratios:\n",
        "        final_width, final_height = aspect_ratios[aspect_ratio]\n",
        "    else:\n",
        "        raise ValueError(f\"Unsupported aspect ratio: {aspect_ratio}\")\n",
        "\n",
        "    image = Image.new(\"RGB\", (final_width, final_height), color)\n",
        "    buffer = io.BytesIO()\n",
        "    image.save(buffer, format=\"PNG\")\n",
        "    return types.Part.from_bytes(data=buffer.getvalue(), mime_type=\"image/png\")\n",
        "\n",
        "\n",
        "def generate_and_display_image(contents: list) -> None:\n",
        "    \"\"\"Generates an image using Gemini and displays it.\"\"\"\n",
        "    response = client.models.generate_content(\n",
        "        model=MODEL_NAME,\n",
        "        contents=contents,\n",
        "        config=GENERATION_CONFIG,\n",
        "    )\n",
        "\n",
        "    if response.candidates and response.candidates[0].content.parts:\n",
        "        for part in response.candidates[0].content.parts:\n",
        "            if part.inline_data and part.inline_data.data:\n",
        "                display(Image.open(io.BytesIO(part.inline_data.data)))\n",
        "                return\n",
        "    print(\"No image was generated.\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "543922fe092f"
      },
      "source": [
        "## Recipe 1: Generate an Image from Scratch\n",
        "\n",
        "This basic recipe demonstrates how to generate an image from a text prompt. You provide a description, and the model creates a visual representation."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "c55d833be8e4"
      },
      "outputs": [],
      "source": [
        "prompt = \"A futuristic cityscape at sunset, with flying cars and towering, glowing skyscrapers.\"\n",
        "contents = [types.Content(role=\"user\", parts=[types.Part.from_text(text=prompt)])]\n",
        "\n",
        "print(\"Generating image from scratch...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eee4941f1f86"
      },
      "source": [
        "## Recipe 2: Aspect Ratio Control\n",
        "This recipe shows how to control the output image's aspect ratio. By providing a blank canvas with specific dimensions, you can guide the model to generate an image that fits that shape."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cc22f12ba564"
      },
      "outputs": [],
      "source": [
        "canvas = create_blank_canvas(aspect_ratio=\"16:9\")\n",
        "prompt = \"\"\"A cinematic, wide-angle shot of a lone astronaut standing on a desolate alien planet, looking up at a swirling, colorful nebula. The planet's surface is rocky and red, and two moons are visible in the sky. Render this scene on the provided canvas to match its 16:9 aspect ratio.\"\"\"\n",
        "contents = [\n",
        "    types.Content(role=\"user\", parts=[canvas, types.Part.from_text(text=prompt)])\n",
        "]\n",
        "\n",
        "print(\"Generating image with 16:9 aspect ratio...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "22343b41d9f2"
      },
      "source": [
        "## Recipe 3: Image Outpainting\n",
        "\n",
        "Outpainting allows you to extend an existing image beyond its original borders. This recipe takes a source image and a larger canvas and intelligently fills in the new areas.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/3_image_outpainting/source_image.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cb1e41389f45"
      },
      "outputs": [],
      "source": [
        "source_image_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/3_image_outpainting/source_image.png\"\n",
        "source_image = types.Part.from_uri(file_uri=source_image_path)\n",
        "canvas = create_blank_canvas(aspect_ratio=\"16:9\")\n",
        "\n",
        "prompt = \"\"\"This is a creative outpainting task. Take the provided source image and seamlessly extend it to fill the entire blank canvas. The new areas should logically and stylistically continue the scene from the original image. Imagine what might exist just beyond the borders of the original photo and bring it to life.\"\"\"\n",
        "\n",
        "contents = [\n",
        "    types.UserContent(parts=[source_image, canvas, types.Part.from_text(text=prompt)])\n",
        "]\n",
        "\n",
        "print(\"Performing image outpainting...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "73a55eef2b7a"
      },
      "source": [
        "## Recipe 4: Image Editing\n",
        "\n",
        "This recipe demonstrates how to edit an existing image with a text prompt. You can add, remove, or change objects and backgrounds.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/4_image_editing/image_to_edit.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "fee319df9b4e"
      },
      "outputs": [],
      "source": [
        "image_to_edit_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/4_image_editing/image_to_edit.png\"\n",
        "source_image = types.Part.from_uri(file_uri=image_to_edit_path)\n",
        "prompt = \"\"\"Please add a red bow tie and a black top hat to the person in the image. Change the background to an elegant ballroom setting with chandeliers and ornate decorations. Ensure the additions look natural and blend seamlessly with the original image.\"\"\"\n",
        "\n",
        "contents = [\n",
        "    types.Content(role=\"user\", parts=[source_image, types.Part.from_text(text=prompt)])\n",
        "]\n",
        "\n",
        "print(\"Editing image...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5657aaff0a6f"
      },
      "source": [
        "## Recipe 5: Style Transfer\n",
        "\n",
        "Style transfer applies the artistic style of one image or artist to the content of another. Here, we transform a photograph into a painting in the style of a famous artist.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/5_style_transfer/content_image.png\" width=\"250\" alt=\"Content image for style transfer\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "e6a70be69a47"
      },
      "outputs": [],
      "source": [
        "content_image_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/5_style_transfer/content_image.png\"\n",
        "content_image = types.Part.from_uri(file_uri=content_image_path)\n",
        "prompt = \"Turn this into a Vincent Van Gogh style painting.\"\n",
        "contents = [\n",
        "    types.Content(role=\"user\", parts=[types.Part.from_text(text=prompt), content_image])\n",
        "]\n",
        "\n",
        "print(\"Performing style transfer...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0deb504636ad"
      },
      "source": [
        "## Recipe 6: Photo Restoration\n",
        "\n",
        "This recipe showcases the model's ability to repair and enhance old or damaged photographs, including colorization and fixing imperfections.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/6_photo_restoration/damaged_photo.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1600e25951e9"
      },
      "outputs": [],
      "source": [
        "damaged_photo_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/6_photo_restoration/damaged_photo.png\"\n",
        "damaged_photo = types.Part.from_uri(file_uri=damaged_photo_path)\n",
        "prompt = \"\"\"Restore and color this old photograph as if it was taken by a modern digital camera. Your output shall solely be extracted photograph. ignore surroundings and fill all canvas by the photograph.\"\"\"\n",
        "contents = [\n",
        "    types.Content(role=\"user\", parts=[damaged_photo, types.Part.from_text(text=prompt)])\n",
        "]\n",
        "\n",
        "print(\"Restoring photo...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2f0248b29d78"
      },
      "source": [
        "## Recipe 7: Multiple Reference Images\n",
        "This recipe demonstrates how to combine elements from multiple source images into a single, coherent scene. The model can identify objects and concepts from different inputs and merge them.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/7_multiple_references/bedroom.png\" width=\"250\" />\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/7_multiple_references/items.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3d6158e6832e"
      },
      "outputs": [],
      "source": [
        "canvas = create_blank_canvas(aspect_ratio=\"16:9\")\n",
        "\n",
        "# In a real scenario, these files would exist. We'll use placeholders.\n",
        "ref_dir = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/7_multiple_references/\"\n",
        "ref_paths = [\n",
        "    \"bedroom.png\",\n",
        "    \"items.png\",\n",
        "]\n",
        "\n",
        "ref_images = [types.Part.from_uri(file_uri=f\"{ref_dir}{path}\") for path in ref_paths]\n",
        "\n",
        "prompt = \"List all elements of the provided images, then create a new image that combines those elements into a consistent bedroom scene. Use empty bedroom as base preserving its camera angle. Render the final result on the provided blank canvas to ensure a 16:9 aspect ratio.\"\n",
        "contents = [\n",
        "    types.Content(\n",
        "        role=\"user\", parts=[*ref_images, canvas, types.Part.from_text(text=prompt)]\n",
        "    )\n",
        "]\n",
        "\n",
        "print(\"Generating image from multiple references...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "e71158a2d541"
      },
      "source": [
        "## Recipe 8: Virtual Try-On\n",
        "\n",
        "This recipe showcases a practical e-commerce application: virtual try-on. It takes an image of a model and an image of a garment and realistically dresses the model in the clothing.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/8_virtual_try_on/model.png\" width=\"250\" />\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/8_virtual_try_on/garment.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6d6ba027fbdd"
      },
      "outputs": [],
      "source": [
        "model_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/8_virtual_try_on/model.png\"\n",
        "garment_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/8_virtual_try_on/garment.png\"\n",
        "model_image = types.Part.from_uri(file_uri=model_path)\n",
        "garment_image = types.Part.from_uri(file_uri=garment_path)\n",
        "\n",
        "prompt = \"Take the garment from the second image and realistically place it on the person in the first image. Adjust the fit, lighting, and shadows to make it look natural.\"\n",
        "contents = [\n",
        "    types.Content(\n",
        "        role=\"user\",\n",
        "        parts=[model_image, garment_image, types.Part.from_text(text=prompt)],\n",
        "    )\n",
        "]\n",
        "\n",
        "print(\"Performing virtual try-on...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4b72aab3b33a"
      },
      "source": [
        "## Recipe 9: Product Recontextualization\n",
        "\n",
        "This recipe is useful for marketing and advertising. It takes a product image with a plain background and places it into a new, more appealing context or scene.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/9_product_recontext/product.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "03b5f1846946"
      },
      "outputs": [],
      "source": [
        "product_path = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/9_product_recontext/product.png\"\n",
        "product_image = types.Part.from_uri(file_uri=product_path)\n",
        "prompt = \"Take the product in this image and place it in a professionally styled kitchen setting, on a marble countertop next to a window with soft, natural light. The final image should look like a high-end advertisement.\"\n",
        "contents = [\n",
        "    types.Content(role=\"user\", parts=[product_image, types.Part.from_text(text=prompt)])\n",
        "]\n",
        "\n",
        "print(\"Recontextualizing product...\")\n",
        "\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "76e29b885369"
      },
      "source": [
        "## Recipe 10: Images with Text\n",
        "This recipe demonstrates the model's ability to generate images that include coherent and well-formatted text, such as creating a menu or a poster."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "626bfef048c4"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"\n",
        "    Create a Pizzeria menu with the following content:\n",
        "    The Artisan Crust Pizzeria\n",
        "    Crafted Pizza, Simply Delicious.\n",
        "    Our Signature Pizzas\n",
        "    Classic Margherita - $11 / $17\n",
        "    San Marzano tomato, fresh mozzarella, basil.\n",
        "    Spicy Pepperoni - $12 / $18\n",
        "    Tomato sauce, mozzarella, premium pepperoni.\n",
        "    Garden Veggie - $12 / $19\n",
        "    Tomato sauce, mozzarella, bell peppers, onion, mushrooms, olives.\n",
        "    Smoky BBQ Chicken - $13 / $19\n",
        "    BBQ sauce base, grilled chicken, mozzarella, red onion.\n",
        "    Beverages\n",
        "    Fountain Soda - $3\n",
        "    Coca-Cola, Diet Coke, Sprite, Root Beer.\n",
        "    Bottled Water - $3\n",
        "    Still or Sparkling.\n",
        "    Order Online or Call: 555-123-4567\n",
        "    \"\"\"\n",
        "contents = [types.Content(role=\"user\", parts=[types.Part.from_text(text=prompt)])]\n",
        "\n",
        "print(\"Generating image with text...\")\n",
        "generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "a736bf1214de"
      },
      "source": [
        "## Recipe 11: Character Consistency\n",
        "\n",
        "Maintaining character consistency across multiple images is a common challenge. This recipe shows how to use a reference image to generate new scenes with the same character in different contexts.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/11_character_consistency/reference.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5f5f354cfe69"
      },
      "outputs": [],
      "source": [
        "image_file = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/11_character_consistency/reference.png\"\n",
        "image_part = types.Part.from_uri(file_uri=image_file)\n",
        "canvas = create_blank_canvas(aspect_ratio=\"9:16\")  # Portrait aspect ratio\n",
        "\n",
        "user_ideas = [\n",
        "    \"The character is US president signing Artificial Intelligence Act and showing it to the camera\",\n",
        "    \"The character is a Firefighter posing next to a fire truck in a city street.\",\n",
        "    \"The character is a buddhist monk meditating on a mountain top during sunrise\",\n",
        "    \"The character is a plumber posing next to a van with plumbing tools (his brand is Nemri Plumbing)\",\n",
        "]\n",
        "\n",
        "for i, user_idea in enumerate(user_ideas):\n",
        "    print(f\"Generating image {i + 1}/{len(user_ideas)}: {user_idea}\")\n",
        "\n",
        "    contents = [\n",
        "        image_part,\n",
        "        canvas,\n",
        "        types.Part.from_text(\n",
        "            text=\"Use previous reference images of the character to generate the new scene\"\n",
        "        ),\n",
        "        types.Part.from_text(text=user_idea),\n",
        "        types.Part.from_text(text=\"make sure to use canvas for aspect ratio\"),\n",
        "    ]\n",
        "\n",
        "    generate_and_display_image(contents)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ad3e2502ff7b"
      },
      "source": [
        "## Recipe 12: Shifting Camera Perspective\n",
        "\n",
        "This recipe demonstrates how to take an existing image and regenerate it from a different camera angle or perspective, as described in a text prompt.\n",
        "\n",
        "<img src=\"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/12_shifting_camera_perspective/original.png\" width=\"250\" />"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dc58341969e3"
      },
      "outputs": [],
      "source": [
        "image_file = \"https://storage.googleapis.com/github-repo/generative-ai/gemini/nano-banana/recipes/12_shifting_camera_perspective/original.png\"\n",
        "image_part = types.Part.from_uri(file_uri=image_file)\n",
        "prompt = \"aerial perspective of a camera looking down at the stressed candidate\"\n",
        "contents = [\n",
        "    types.Content(role=\"user\", parts=[image_part, types.Part.from_text(text=prompt)])\n",
        "]\n",
        "\n",
        "print(\"Shifting camera perspective...\")\n",
        "generate_and_display_image(contents)"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "nano_banana_recipes.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
