Virtual Try On

gemini-cookbookgemini-apiexamplesgemini
Copyright 2025 Google LLC.
[ ]

Virtual Try-On with Gemini 2.5 & Imagen 3

This notebook is an implementation of virtually trying on different outfits by image editing and inpainting using Imagen 3, along with Gemini 2.5 for creating segmentation masks.

Note: This notebook is designed to run on Google Colab. Certain commands, such as %cd, are specific to the Colab environment and may not work outside of it.

NSTiwari's GitHub avatar

This notebook was contributed by Nitin Tiwari.

LinkedIn - See Nitin other notebooks here.

Have a cool Gemini example? Feel free to share it too!

Note: For the older implementation that used SAM 2.1 to generate segmentation masks, refer to this notebook.

Before you begin

Before starting, ensure you have the following prerequisites in place:

  • Gemini API Access: Make sure you have access to the Gemini API.

  • GCP Account: You’ll need an active GCP account with a billing project set up and the Vertex AI API enabled. If you’re unsure how to get started, follow the instructions in this setup guide.

Once you have these ready, you're good to go.

Step 1: Install Gen AI SDK

[ ]

Step 2: Import libraries

[ ]

Step 3: Configure Gemini API key and GCP Project ID

To run the following cell, your Gemini API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see Authentication image for an example.

Additionally, please also store your GCP Project ID in a Colab Secret named GCP_PROJECT_ID.

[ ]
MODEL_ID

System Instructions

[ ]
[ ]

Step 4: Referring Expression Segmentation by Gemini 2.5

Pass the image, and the object to be segmented as part of the text prompt to Gemini 2.5.

[ ]
[ ]
input_image
object_to_segment
Output

Step 5: Configure Imagen 3 parameters

Next, certain parameters need to be configured as follows:

  • Base image: The input image file to be edited.
  • Mask file:: The binary mask of the object to be inpainted.
  • Edit mode: Options like inpainting-insert, inpainting-remove, or outpainting.
  • Mask mode: Background or foreground
  • Dilation: A float value between 0 and 1 indicating the percentage of the provided mask.
[ ]
prompt
edit_mode
mask_mode
dilation
0.01

Step 6: Generate and inpaint new image using Imagen 3

Finally, the image is ready to be generated and edited using Imagen 3.

Note: For accurate image generation and editing, ensure a precise mask of the object is provided. Additionally, a detailed prompt should be given for the image to be generated. Achieving optimal results may require several attempts.

[ ]
[ ]
Output

Congratulations on successfully completing this tutorial and building a Virtual Try-On application using Gemini 2.5's segmentation and Imagen 3's powerful image generation capabilities.