Virtual Try-On: Batch Generation Pipeline
Source notebook
Repo path:
vision/use-cases/batch_virtual_try_on.ipynb· Open on GitHub · intermediate
Batch-generates virtual try-on images from person and apparel inputs using Google GenAI SDK.
Summary
It teaches how to use the Google Gen AI SDK for Python with the Virtual Try-On model to generate images of people wearing product apparel. The workflow installs the SDK, authenticates in Colab, initializes a GenAI enterprise client with a Google Cloud project and region, accepts local uploads or public URLs, creates every person-product pairing, calls recontext_image, previews results, and downloads a ZIP.
Key code patterns
Initialize GenAI 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 Google GenAI client for a project and region before calling the model.
Normalize local and URL images
if local_images:
person_image_obj = Image.from_file(location=person_img)
product_image_obj = Image.from_file(location=product_img)
else:
person_response = requests.get(person_img)
person_response.raise_for_status()
person_image_obj = Image(image_bytes=person_response.content)Shows the two supported input paths: uploaded files and URL-fetched image bytes.
Batch pairwise generation
for person_img in person_images:
for product_img in product_images:
generated_image = client.models.recontext_image(
model="virtual-try-on-001",
source=RecontextImageSource(
person_image=person_image_obj,
product_images=[ProductImage(product_image=product_image_obj)],
),
)Creates one try-on result for each person and product image combination.
Download generated results
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zip_file:
for i, result in enumerate(results):
image_data = result.image_bytes
zip_file.writestr(f"result_{i + 1}.png", image_data)
files.download(zip_filename)Packages generated image bytes into a timestamped ZIP for local download.
Models & APIs used
- Models: virtual-try-on-001
- APIs / services: Vertex AI, Virtual Try-On API, Cloud Storage
- SDKs / libraries:
google-genai,google.colab,matplotlib,numpy,requests,Pillow
When to use this
Use this pattern when you need to batch-generate virtual try-on outputs across multiple person and apparel image combinations.
Gotchas & caveats
- Colab requires auth.authenticate_user() before using Google Cloud credentials.
- The notebook requires an existing Google Cloud project and enabling the Agent Platform API with apiid aiplatform.googleapis.com.
- PROJECT_ID must be set directly or through GOOGLE_CLOUD_PROJECT.
- LOCATION defaults to us-central1 from GOOGLE_CLOUD_REGION when no region is set.
- Choose only one input option per batch job: local images or public URLs.
- Public image inputs must be one URL per line, not comma-separated values or local file paths.
- The notebook adds time.sleep(2) between jobs to help with API quota limits.
- Supported clothing is limited in the notebook to tops, bottoms, and footwear.
Best practices
- Reads project and region from environment variables when notebook parameters are not set.
- Displays input images before generation and displays generated images afterward for visual inspection.
- Uses response.raise_for_status() when fetching public image URLs.
- Tracks current job and total jobs while processing the Cartesian product of inputs.
- Catches per-job exceptions and skips failed jobs instead of stopping the whole batch.
- Adds a short delay between API calls to reduce quota pressure.
- Writes generated outputs into a compressed ZIP file for download.
Related
- Concepts: Vision · Image & Video Generation · Applied Use Cases
- Entities: Vertex AI · Google GenAI SDK · Cloud Storage
- Area: Vision Notebooks
- Best practices: Vision - Best Practices · Image & Video Generation - Best Practices · Applied Use Cases - Best Practices