|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.genai.types import Image |
| 16 | + |
| 17 | + |
| 18 | +def virtual_try_on(output_file: str) -> Image: |
| 19 | + # [START googlegenaisdk_imggen_virtual_try_on_with_txt_img] |
| 20 | + from google import genai |
| 21 | + from google.genai.types import RecontextImageSource, ProductImage |
| 22 | + |
| 23 | + client = genai.Client() |
| 24 | + |
| 25 | + # TODO(developer): Update and un-comment below line |
| 26 | + # output_file = "output-image.png" |
| 27 | + |
| 28 | + image = client.models.recontext_image( |
| 29 | + model="virtual-try-on-preview-08-04", |
| 30 | + source=RecontextImageSource( |
| 31 | + person_image=Image.from_file(location="test_resources/man.png"), |
| 32 | + product_images=[ProductImage(product_image=Image.from_file(location="test_resources/sweater.jpg"))] |
| 33 | + ) |
| 34 | + ) |
| 35 | + |
| 36 | + image.generated_images[0].image.save(output_file) |
| 37 | + |
| 38 | + print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes") |
| 39 | + # Example response: |
| 40 | + # Created output image using 1234567 bytes |
| 41 | + |
| 42 | + # [END googlegenaisdk_imggen_virtual_try_on_with_txt_img] |
| 43 | + return image.generated_images[0].image |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + virtual_try_on(output_file="test_resources/man_in_sweater.png") |
0 commit comments