Skip to content

Commit d7ab25f

Browse files
authored
feat(genai): sample for virtual try on (#13539)
* feat(genai): sample for virtual try on * fix: spacing * fix: lint
1 parent a0d7585 commit d7ab25f

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-genai==1.27.0
1+
google-genai==1.29.0
22
pillow==11.1.0

genai/image_generation/test_image_generation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import imggen_scribble_ctrl_type_with_txt_img
4141
import imggen_style_reference_with_txt_img
4242
import imggen_subj_refer_ctrl_refer_with_txt_imgs
43+
import imggen_virtual_try_on_with_txt_img
4344
import imggen_with_txt
4445

4546

@@ -172,6 +173,14 @@ def test_img_customization_canny_edge(output_gcs_uri: str) -> None:
172173
assert response
173174

174175

176+
def test_img_virtual_try_on() -> None:
177+
OUTPUT_FILE = os.path.join(RESOURCES, "man_in_sweater.png")
178+
response = imggen_virtual_try_on_with_txt_img.virtual_try_on(
179+
OUTPUT_FILE
180+
)
181+
assert response
182+
183+
175184
def test_imggen_mmflash_examples() -> None:
176185
# generate image
177186
fname = imggen_mmflash_with_txt.generate_content()
1.51 MB
Loading
1.53 MB
Loading
360 KB
Loading

0 commit comments

Comments
 (0)