Sign in to your Google Cloud account. If you're new to
Google Cloud,
create an account to evaluate how our products perform in
real-world scenarios. New customers also get $300 in free credits to
run, test, and deploy workloads.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True
fromgoogleimportgenaifromgoogle.genai.typesimportRecontextImageSource,ProductImageclient=genai.Client()# TODO(developer): Update and un-comment below line# output_file = "output-image.png"image=client.models.recontext_image(model="virtual-try-on-preview-08-04",source=RecontextImageSource(person_image=Image.from_file(location="test_resources/man.png"),product_images=[ProductImage(product_image=Image.from_file(location="test_resources/sweater.jpg"))],),)image.generated_images[0].image.save(output_file)print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")# Example response:# Created output image using 1234567 bytes
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Generate Virtual Try-On Images\n\n| **Preview**\n|\n|\n| This product or feature is a Generative AI Preview offering, subject to\n| the \"Pre-GA Offerings Terms\" of the\n| [Google Cloud Service Specific Terms](/terms/service-terms),\n| as well as the\n| [Additional Terms for Generative AI Preview Products](/trustedtester/aitos). For this\n| Generative AI Preview offering, Customers may elect to use it for\n| production or commercial purposes, or disclose Generated Output to\n| third-parties, and may process personal data as outlined in the\n| [Cloud Data Processing\n| Addendum](/terms/data-processing-addendum),\n| subject to the obligations and restrictions described in the agreement\n| under which you access Google Cloud. Pre-GA products are available \"as is\"\n| and might have limited support. For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nVirtual Try-On lets you generate images of people to virtually try-on clothing\nproducts. You provide an image of a person and an image of a clothing product,\nand then Virtual Try-On generates an image of the person wearing that\nproduct.\n\n[Try Virtual Try-On in a Colab](https://colab.sandbox.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/vision/getting-started/virtual_try_on.ipynb)\n\nBefore you begin\n----------------\n\nSign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n\nIn the Google Cloud console, on the project selector page,\nselect or create a Google Cloud project.\n| **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n[Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n\n\n[Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n\nEnable the Vertex AI API.\n\n\n[Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com) \nIn the Google Cloud console, on the project selector page,\nselect or create a Google Cloud project.\n| **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n[Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n\n\n[Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n\nEnable the Vertex AI API.\n\n\n[Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com)\n1. Set up authentication for your environment.\n\n Select the tab for how you plan to use the samples on this page: \n\n ### Python\n\n\n To use the Python samples on this page in a local\n development environment, install and initialize the gcloud CLI, and\n then set up Application Default Credentials with your user credentials.\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n 2. If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n 3.\n\n If you're using a local shell, then create local authentication credentials for your user\n account:\n\n ```bash\n gcloud auth application-default login\n ```\n\n You don't need to do this if you're using Cloud Shell.\n\n\n If an authentication error is returned, and you are using an external identity provider\n (IdP), confirm that you have\n [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Set up ADC for a local development environment](/docs/authentication/set-up-adc-local-dev-environment)\n in the Google Cloud authentication documentation.\n\n ### REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI.\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\nGenerate images\n---------------\n\n### Python\n\n#### Install\n\n```\npip install --upgrade google-genai\n```\n\n\nTo learn more, see the\n[SDK reference documentation](https://googleapis.github.io/python-genai/).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=global\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n from google import genai\n from google.genai.types import RecontextImageSource, ProductImage\n\n client = genai.Client()\n\n # TODO(developer): Update and un-comment below line\n # output_file = \"output-image.png\"\n\n image = client.models.recontext_image(\n model=\"virtual-try-on-preview-08-04\",\n source=RecontextImageSource(\n person_image=Image.from_file(location=\"test_resources/man.png\"),\n product_images=[\n ProductImage(product_image=Image.from_file(location=\"test_resources/sweater.jpg\"))\n ],\n ),\n )\n\n image.generated_images[0].image.save(output_file)\n\n print(f\"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes\")\n # Example response:\n # Created output image using 1234567 bytes\n\n### REST\n\n\nBefore using any of the request data,\nmake the following replacements:\n\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: The region that your project is located in. For more information about supported regions, see [Generative AI on Vertex AI\n locations](/vertex-ai/generative-ai/docs/learn/locations).\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: Your Google Cloud [project ID](/resource-manager/docs/creating-managing-projects#identifiers).\n- \u003cvar translate=\"no\"\u003eBASE64_PERSON_IMAGE\u003c/var\u003e: The Base64-encoded image of the person image.\n- \u003cvar translate=\"no\"\u003eBASE64_PRODUCT_IMAGE\u003c/var\u003e: The Base64-encoded image of the product image.\n- \u003cvar translate=\"no\"\u003eIMAGE_COUNT\u003c/var\u003e: The number of images to generate. The accepted range of values is `1` to `4`.\n- \u003cvar translate=\"no\"\u003eGCS_OUTPUT_PATH\u003c/var\u003e: The Cloud Storage path to store the virtual try-on output to.\n\n\nHTTP method and URL:\n\n```\nPOST https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04:predict\n```\n\n\nRequest JSON body:\n\n```\n{\n \"instances\": [\n {\n \"personImage\": {\n \"image\": {\n \"bytesBase64Encoded\": \"BASE64_PERSON_IMAGE\"\n }\n },\n \"productImages\": [\n {\n \"image\": {\n \"bytesBase64Encoded\": \"BASE64_PRODUCT_IMAGE\"\n }\n }\n ]\n }\n ],\n \"parameters\": {\n \"sampleCount\": IMAGE_COUNT,\n \"storageUri\": \"GCS_OUTPUT_PATH\"\n }\n}\n```\n\nTo send your request, choose one of these options: \n\n#### curl\n\n| **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) , or by using [Cloud Shell](/shell/docs), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\nSave the request body in a file named `request.json`,\nand execute the following command:\n\n```\ncurl -X POST \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n -H \"Content-Type: application/json; charset=utf-8\" \\\n -d @request.json \\\n \"https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04:predict\"\n```\n\n#### PowerShell\n\n| **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\nSave the request body in a file named `request.json`,\nand execute the following command:\n\n```\n$cred = gcloud auth print-access-token\n$headers = @{ \"Authorization\" = \"Bearer $cred\" }\n\nInvoke-WebRequest `\n -Method POST `\n -Headers $headers `\n -ContentType: \"application/json; charset=utf-8\" `\n -InFile request.json `\n -Uri \"https://REGION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/publishers/google/models/virtual-try-on-preview-08-04:predict\" | Select-Object -Expand Content\n```\nThe request returns image objects. In this example, two image objects are returned, with two prediction objects as base64-encoded images.\n\n```\n{\n \"predictions\": [\n {\n \"mimeType\": \"image/png\",\n \"bytesBase64Encoded\": \"BASE64_IMG_BYTES\"\n },\n {\n \"bytesBase64Encoded\": \"BASE64_IMG_BYTES\",\n \"mimeType\": \"image/png\"\n }\n ]\n}\n```\n\n\u003cbr /\u003e"]]