Virtual Try-On: Image Generation

Source notebook

Repo path: vision/getting-started/virtual_try_on.ipynb · Open on GitHub · intro

Uses Google Gen AI SDK to generate virtual try-on images from local and Cloud Storage clothing images.

Summary

This notebook teaches how to call the Virtual Try-On model with person and product images using the Google Gen AI SDK for Python. It demonstrates local-file try-on for separate top and bottom items, combining outfit items into one product image, and using a Cloud Storage clothing image with an Imagen-generated person. The workflow covers authentication, project and region setup, image display helpers, model calls, safety filters, output formats, and saving generated output.

Key code patterns

Initialize Gen AI client

PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
client = genai.Client(
    enterprise=True,
    project=PROJECT_ID,
    location=LOCATION,
)

Configures the SDK client with a Google Cloud project and region for model calls.

Virtual try-on from local files

response = client.models.recontext_image(
    model="virtual-try-on-001",
    source=RecontextImageSource(
        person_image=Image.from_file(location=person_image),
        product_images=[ProductImage(product_image=Image.from_file(location=top_image))],
    ),
    config=RecontextImageConfig(output_mime_type="image/jpeg", number_of_images=1),
)

Shows the core Virtual Try-On request using a local person image and local clothing image.

Generate person with Imagen

image = client.models.generate_images(
    model="imagen-4.0-generate-001",
    prompt=prompt,
    config=GenerateImagesConfig(
        output_mime_type="image/jpeg",
        number_of_images=1,
        image_size="2K",
    ),
)

Creates a person image that can be passed directly into the Virtual Try-On workflow.

Use Cloud Storage product image

ProductImage(
    product_image=Image(
        gcs_uri="gs://cloud-samples-data/generative-ai/image/dress.jpg"
    )
)

Demonstrates referencing a clothing image stored in Cloud Storage instead of a local file.

Models & APIs used

When to use this

Use this pattern when building fashion or retail workflows that generate images of a person wearing one or more clothing items.

Gotchas & caveats

  • The notebook requires an existing Google Cloud project and enabling the Agent Platform API link for aiplatform.googleapis.com.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID must be supplied or available through GOOGLE_CLOUD_PROJECT.
  • LOCATION defaults to us-central1 when GOOGLE_CLOUD_REGION is not set.
  • The Virtual Try-On model can only specify one clothing item per request unless multiple items are combined into one product image.
  • The notebook points readers to Virtual Try-On quotas and Imagen pricing documentation.
  • Generated images use safety_filter_level settings such as BLOCK_LOW_AND_ABOVE or BLOCK_MEDIUM_AND_ABOVE.
  • SynthID digital watermarking is added by default according to the notebook.

Best practices

  • Use Image.from_file for local person and product images.
  • Save an intermediate try-on output locally when it will be reused in a later request.
  • Set output_mime_type and number_of_images explicitly in RecontextImageConfig or GenerateImagesConfig.
  • Use safety_filter_level explicitly when generating or recontextualizing images.
  • Combine multiple clothing items into one photo when trying on multiple items in a single request.
  • Use a gcs_uri Image object when the product image is stored in Cloud Storage.
  • Convert display images to RGB for broader Jupyter environment support.