Files
2026-07-13 13:30:30 +08:00

772 lines
29 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BCl9pcbOOeA0"
},
"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": "ZPC2X_a9ErW7"
},
"source": [
"# Gemini 2.5 Flash Image (Nano Banana 🍌) Generation\n",
"\n",
"> **NOTE:** On October 2, 2026, Gemini 2.5 Flash Image will be retired on Gemini Enterprise Agent Platform. After this date, you will be unable to use this model. Please use Gemini 3.1 Flash Image (Nano Banana 2) or Gemini 3 Pro Image (Nano Banana Pro) instead.\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/getting-started/intro_gemini_2_5_image_gen.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/agent-platform/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fgetting-started%2Fintro_gemini_2_5_image_gen.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/agent-platform/workbench/instances?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/getting-started/intro_gemini_2_5_image_gen.ipynb\">\n",
" <img width=\"32px\" src=\"https://storage.googleapis.com/github-repo/workbench-icon.svg\" alt=\"Workbench logo\"><br> Open in Workbench\n",
" </a>\n",
" </td>\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_2_5_image_gen.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",
"<p>\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/getting-started/intro_gemini_2_5_image_gen.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/getting-started/intro_gemini_2_5_image_gen.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/getting-started/intro_gemini_2_5_image_gen.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/getting-started/intro_gemini_2_5_image_gen.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/getting-started/intro_gemini_2_5_image_gen.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>\n",
"</p>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f0cc0f48513b"
},
"source": [
"| Author |\n",
"| --- |\n",
"| [Katie Nguyen](https://github.com/katiemn) |"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "axauUzNXEl_R"
},
"source": [
"## Overview\n",
"\n",
"Gemini 2.5 Flash Image is a powerful, generalist multimodal model that offers state-of-the-art image generation and conversational image editing capabilities. This enables you to converse with Gemini and create or edit images with interwoven text.\n",
"\n",
"In this tutorial, you'll learn how to use Gemini 2.5 Flash Image on Agent Platform using the Google Gen AI SDK to try out the following scenarios:\n",
" - Image generation:\n",
" - Text-to-image generation\n",
" - Interleaved image and text sequences\n",
" - Image editing:\n",
" - Image-to-image with subject customization and style transfer\n",
" - Multi-turn image editing with localization\n",
" - Editing with multiple reference images"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D50ekWXjEl_S"
},
"source": [
"## Get started"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jLJQdbgSbb4M"
},
"source": [
"### Install Google Gen AI SDK for Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SQ0qcEWuXNXs"
},
"outputs": [],
"source": [
"%pip install --upgrade --quiet google-genai"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dmWOrTJ3gx13"
},
"source": [
"### Authenticate your notebook environment (Colab only)\n",
"\n",
"If you are running this notebook on Google Colab, run the following cell to authenticate your environment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NyKGtVQjgx13"
},
"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": "gfs2gxVrAN02"
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "O6ZGaZlxP9L0"
},
"source": [
"### Set Google Cloud project information\n",
"\n",
"To get started using Agent Platform, you must have an existing Google Cloud project and [enable the Agent Platform API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
"\n",
"Learn more about [setting up a project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects) and a [development environment](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "u8IivOG5SqY6"
},
"outputs": [],
"source": [
"import os\n",
"from io import BytesIO\n",
"\n",
"import matplotlib.image as img\n",
"import matplotlib.pyplot as plt\n",
"import requests\n",
"from IPython.display import Image, Markdown, display\n",
"from PIL import Image as PIL_Image\n",
"from google import genai\n",
"from google.genai.types import FinishReason, GenerateContentConfig, ImageConfig, Part\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",
"client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "854fbf388e2b"
},
"source": [
"### Load the image model\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7eeb063ac6d4"
},
"outputs": [],
"source": [
"MODEL_ID = \"gemini-2.5-flash-image\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xgOucVQlVR4t"
},
"source": [
"## Image generation\n",
"\n",
"First, you'll send text prompts to Gemini 2.5 Flash Image describing the images you'd like to generate.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MmA_RAbFwED4"
},
"source": [
"### Text to image\n",
"\n",
"In the cell below, you'll call the `generate_content` method and pass in the following arguments:\n",
"\n",
" - `model`: The ID of the model you want to use.\n",
" - `contents`: This is your prompt, in this case, a text only user message describing the image to be generated.\n",
" - `config`: A config for specifying content settings.\n",
" - `response_modalities`: To generate an image, you must include `IMAGE` in the `response_modalities` list.\n",
" - `ImageConfig`: Set the `aspect_ratio`. Valid ratios are: \"1:1\", \"3:2\", \"2:3\", \"3:4\", \"4:3\", \"4:5\", \"5:4\", \"9:16\", \"16:9\", \"21:9\"\n",
" - `candidate_count`: The number of candidates to generate.\n",
"\n",
"\n",
"All generated images include a [SynthID watermark](https://deepmind.google/technologies/synthid/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "QK2Mi3zmkHSA"
},
"outputs": [],
"source": [
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=\"a cartoon infographic on flying sneakers\",\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"9:16\",\n",
" ),\n",
" candidate_count=1,\n",
" ),\n",
")\n",
"\n",
"# Check for errors if an image is not generated\n",
"if response.candidates[0].finish_reason != FinishReason.STOP:\n",
" reason = response.candidates[0].finish_reason\n",
" raise ValueError(f\"Prompt Content Error: {reason}\")\n",
"\n",
"\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5l4YLy8Vq_-v"
},
"source": [
"### Text to image and text\n",
"\n",
"In addition to generating images, Gemini can also create interleaved sequences of images and text.\n",
"\n",
"For example, you could ask the model to generate a recipe for banana bread with images showing different stages of the cooking process. Or, you could ask the model to generate images of different wildflowers with accompanying titles and descriptions.\n",
"\n",
"Let's try out the interleaved text and image functionality by prompting Gemini 2.5 Flash Image to create a tutorial for assembling a peanut butter and jelly sandwich.\n",
"\n",
"You'll notice that in the prompt we ask the model to generate both text and images for each step. This encourages the model to create interleaved text and images. Also, make sure to set `response_modalities=[\"TEXT\", \"IMAGE\"]` so you'll get both modalities in the response."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FwCeB0Hxlrz2"
},
"outputs": [],
"source": [
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=\"Create a tutorial explaining how to make a peanut butter and jelly sandwich in three easy steps. For each step, provide a title with the number of the step, an explanation, and also generate an image to illustrate the content. Label each image with the step number but no other words.\",\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"4:3\",\n",
" ),\n",
" ),\n",
")\n",
"\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R3g5n23lDtsN"
},
"source": [
"## Image editing\n",
"\n",
"Gemini 2.5 Flash Image can generate image-to-image outputs from multiple reference images. This is useful for tasks like ensuring character consistency, generating logos, transferring styles, and inserting or removing objects.\n",
"\n",
"### Subject customization\n",
"\n",
"Let's try out a subject customization example by asking Gemini 2.5 Flash Image to create an image of this dog in a pencil sketch style.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZFI-Q53Bg0oK"
},
"source": [
"#### Download the dog image\n",
"\n",
"The following example uses an image from Cloud Storage. If you prefer to use a different image, you can either change the URL in the `wget` command or, if you have a local file, update the `subject_image` variable in the subsequent step."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3kEFbvgHhlvI"
},
"outputs": [],
"source": [
"!wget https://storage.googleapis.com/cloud-samples-data/generative-ai/image/dog-1.jpg"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Bd9CASBlNc1p"
},
"outputs": [],
"source": [
"subject_image = \"dog-1.jpg\" # @param {type: 'string'}\n",
"\n",
"# Display the image\n",
"fig, axis = plt.subplots(1, 1, figsize=(6, 12))\n",
"axis.imshow(img.imread(subject_image))\n",
"axis.axis(\"off\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z284xua1iVXm"
},
"source": [
"#### Send the request\n",
"\n",
"Since you're including data from a local image in the request, you'll need to include `Part.from_bytes` to the request `contents`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UAiUyW1VMzPn"
},
"outputs": [],
"source": [
"with open(subject_image, \"rb\") as f:\n",
" image = f.read()\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=[\n",
" Part.from_bytes(\n",
" data=image,\n",
" mime_type=\"image/jpeg\",\n",
" ),\n",
" \"Create a pencil sketch image of this dog wearing a cowboy hat in a western-themed setting.\",\n",
" ],\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"1:1\",\n",
" ),\n",
" candidate_count=1,\n",
" ),\n",
")\n",
"\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2lQcs5x0jJLx"
},
"source": [
"### Style transfer\n",
"\n",
"In this next example, you'll use the style from a living room to reimagine a kitchen in the same style."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9IFuYABFjz24"
},
"source": [
"#### Download the living room image\n",
"\n",
"Again, the following example uses an image from Cloud Storage. If you prefer to use a different image, you can either change the URL in the `wget` command or, if you have a local file, update the `style_image` variable in the subsequent step."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FSl1zOVJwmMU"
},
"outputs": [],
"source": [
"!wget https://storage.googleapis.com/cloud-samples-data/generative-ai/image/living-room.png"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BC0oPWSDwvn4"
},
"outputs": [],
"source": [
"style_image = \"living-room.png\" # @param {type: 'string'}\n",
"\n",
"# Display the image\n",
"fig, axis = plt.subplots(1, 1, figsize=(6, 12))\n",
"axis.imshow(img.imread(style_image))\n",
"axis.axis(\"off\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zsjSSGCLkAxW"
},
"source": [
"#### Send the request"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vuCxm1YOxCKn"
},
"outputs": [],
"source": [
"with open(style_image, \"rb\") as f:\n",
" image = f.read()\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=[\n",
" Part.from_bytes(\n",
" data=image,\n",
" mime_type=\"image/png\",\n",
" ),\n",
" \"Using the concepts, colors, and themes from this living room generate a kitchen and dining room with the same aesthetic.\",\n",
" ],\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"21:9\",\n",
" ),\n",
" candidate_count=1,\n",
" ),\n",
")\n",
"\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2vnEO2OfPkjn"
},
"source": [
"### Multi-turn image editing\n",
"\n",
"In this next section, you supply a starting image and iteratively alter certain aspects of the image by chatting with Gemini 2.5 Flash Image.\n",
"\n",
"\n",
"Rather than using local images in the next example, you'll use images stored in Google Cloud Storage. Run the next step to view the starting image of a perfume bottle. If you'd like to use a different image from Cloud Storage, replace the `perfume_url` and `perfume_uri` below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "GGK3jQ86reqy"
},
"outputs": [],
"source": [
"perfume_url = (\n",
" \"https://storage.googleapis.com/cloud-samples-data/generative-ai/image/perfume.jpg\"\n",
")\n",
"perfume_uri = \"gs://cloud-samples-data/generative-ai/image/perfume.jpg\"\n",
"\n",
"\n",
"# Display the image\n",
"fig, axes = plt.subplots(1, 2, figsize=(12, 6))\n",
"perfume_image = PIL_Image.open(BytesIO(requests.get(perfume_url).content))\n",
"axes[0].imshow(perfume_image)\n",
"for i, ax in enumerate(axes):\n",
" ax.axis(\"off\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XF3brYa9lSON"
},
"source": [
"#### Start a chat\n",
"\n",
"In this next step, you'll start a chat so you can continuously edit your images by talking with Gemini. Since you're now using reference images stored in Cloud Storage, you'll use `Part.from_uri` in the `message` content."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "B-2KXCNgQEYu"
},
"outputs": [],
"source": [
"chat = client.chats.create(model=MODEL_ID)\n",
"\n",
"response = chat.send_message(\n",
" message=[\n",
" Part.from_uri(\n",
" file_uri=perfume_uri,\n",
" mime_type=\"image/jpeg\",\n",
" ),\n",
" \"change the perfume color to a light purple\",\n",
" ],\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"3:2\",\n",
" ),\n",
" ),\n",
")\n",
"\n",
"data = perfume_uri\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))\n",
" data = part.inline_data.data"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Loi4drVpdCjn"
},
"source": [
"Now, you'll include the previous image data in a new message in the existing chat, along with a new text prompt, to update the previously generated image. This time, you'll ask to write a word on the perfume bottle, and since Gemini is able to handle different languages, let's ask for the word in French."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "99B7me3HGC1I"
},
"outputs": [],
"source": [
"response = chat.send_message(\n",
" message=[\n",
" Part.from_bytes(\n",
" data=data,\n",
" mime_type=\"image/jpeg\",\n",
" ),\n",
" \"inscribe the word flowers in French on the perfume bottle in a delicate white cursive font\",\n",
" ],\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"3:2\",\n",
" ),\n",
" ),\n",
")\n",
"\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1l19iG4CZMEr"
},
"source": [
"### Multiple reference images\n",
"\n",
"When editing images with Gemini 2.5 Flash Image, you can also supply multiple input images to create new ones. In this next example, you'll prompt Gemini with an image of a woman and a suitcase. You'll then ask Gemini to combine the objects from these images in order to create a new one. You'll also ask Gemini to supply text to accompany the image.\n",
"\n",
"\n",
"Run the following cell to visualize the starting images stored in Cloud Storage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HuLVkC3edP6s"
},
"outputs": [],
"source": [
"person_url = (\n",
" \"https://storage.googleapis.com/cloud-samples-data/generative-ai/image/woman.jpg\"\n",
")\n",
"suitcase_url = (\n",
" \"https://storage.googleapis.com/cloud-samples-data/generative-ai/image/suitcase.png\"\n",
")\n",
"\n",
"fig, axes = plt.subplots(1, 2, figsize=(12, 6))\n",
"person_image = PIL_Image.open(BytesIO(requests.get(person_url).content))\n",
"axes[0].imshow(person_image)\n",
"suitcase_image = PIL_Image.open(BytesIO(requests.get(suitcase_url).content))\n",
"axes[1].imshow(suitcase_image)\n",
"for i, ax in enumerate(axes):\n",
" ax.axis(\"off\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EYobavQRnZ6Q"
},
"source": [
"Now, you'll send the request. The only difference from previous image editing calls is that you'll supply multiple `Part.from_uri` instances depending on the number of reference images you have."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WD3XoWjuZQFr"
},
"outputs": [],
"source": [
"response = client.models.generate_content(\n",
" model=MODEL_ID,\n",
" contents=[\n",
" Part.from_uri(\n",
" file_uri=\"gs://cloud-samples-data/generative-ai/image/suitcase.png\",\n",
" mime_type=\"image/png\",\n",
" ),\n",
" Part.from_uri(\n",
" file_uri=\"gs://cloud-samples-data/generative-ai/image/woman.jpg\",\n",
" mime_type=\"image/jpeg\",\n",
" ),\n",
" \"Generate an image of the woman pulling the suitcase in an airport. Separately, write a short caption for this image that would be suitable for a social media post.\",\n",
" ],\n",
" config=GenerateContentConfig(\n",
" response_modalities=[\"TEXT\", \"IMAGE\"],\n",
" image_config=ImageConfig(\n",
" aspect_ratio=\"9:16\",\n",
" ),\n",
" candidate_count=1,\n",
" ),\n",
")\n",
"\n",
"\n",
"for part in response.candidates[0].content.parts:\n",
" if part.text:\n",
" display(Markdown(part.text))\n",
" if part.inline_data:\n",
" display(Image(data=part.inline_data.data, width=500))"
]
}
],
"metadata": {
"colab": {
"name": "intro_gemini_2_5_image_gen.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}