Skip to content

Commit 2e3ef8e

Browse files
authored
feat: add edit product image editing sample and test for Imagen 2 (GoogleCloudPlatform#11380)
* feat: add edit product image sample and test * Trigger Build * Trigger Build
1 parent afcecec commit 2e3ef8e

File tree

10 files changed

+147
-1
lines changed

10 files changed

+147
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright 2024 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+
"""Google Cloud Vertex AI sample for editing a product image. You can
16+
modify the background content but preserve the product's appearance.
17+
Example usage:
18+
python edit_image_product_image.py --project_id <project-id> \
19+
--location <location> --input_file <filepath> \
20+
--output_file <filepath> --prompt <text>
21+
"""
22+
23+
# [START generativeaionvertexai_imagen_edit_image_product_image]
24+
25+
import argparse
26+
27+
import vertexai
28+
from vertexai.preview.vision_models import Image, ImageGenerationModel
29+
30+
31+
def edit_image_product_image(
32+
project_id: str,
33+
location: str,
34+
input_file: str,
35+
output_file: str,
36+
prompt: str,
37+
) -> vertexai.preview.vision_models.ImageGenerationResponse:
38+
"""Edit a local image by modifying the background content.
39+
Args:
40+
project_id: Google Cloud project ID, used to initialize Vertex AI.
41+
location: Google Cloud region, used to initialize Vertex AI.
42+
input_file: Local path to the input image file. Image can be in PNG or JPEG format.
43+
output_file: Local path to the output image file.
44+
prompt: The text prompt describing what you want to see in the background."""
45+
46+
vertexai.init(project=project_id, location=location)
47+
48+
model = ImageGenerationModel.from_pretrained("imagegeneration@006")
49+
base_img = Image.load_from_file(location=input_file)
50+
51+
images = model.edit_image(
52+
base_image=base_img,
53+
prompt=prompt,
54+
edit_mode="product-image",
55+
# Optional parameters
56+
number_of_images=1,
57+
# Allows for product repositioning: "reposition" (default; moves product
58+
# to center), "fixed" (keeps product in original position)
59+
# product_position="fixed",
60+
)
61+
62+
images[0].save(location=output_file)
63+
64+
# Optional. View the edited image in a notebook.
65+
# images[0].show()
66+
67+
print(f"Created output image using {len(images[0]._image_bytes)} bytes")
68+
69+
return images
70+
71+
72+
# [END generativeaionvertexai_imagen_edit_image_product_image]
73+
74+
if __name__ == "__main__":
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
77+
parser.add_argument(
78+
"--location",
79+
help="The location in which to initialize Vertex AI.",
80+
default="us-central1",
81+
)
82+
parser.add_argument(
83+
"--input_file",
84+
help="The local path to the input file (e.g., 'my-input.png').",
85+
required=True,
86+
)
87+
parser.add_argument(
88+
"--output_file",
89+
help="The local path to the output file (e.g., 'my-output.png').",
90+
required=True,
91+
)
92+
parser.add_argument(
93+
"--prompt",
94+
help="The text prompt describing what you want to insert into the background.",
95+
required=True,
96+
)
97+
args = parser.parse_args()
98+
edit_image_product_image(
99+
args.project_id,
100+
args.location,
101+
args.input_file,
102+
args.output_file,
103+
args.prompt,
104+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2024 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+
import os
16+
17+
import backoff
18+
19+
import edit_image_product_image
20+
21+
from google.api_core.exceptions import ResourceExhausted
22+
23+
24+
_RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources")
25+
_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
26+
_LOCATION = "us-central1"
27+
_INPUT_FILE = os.path.join(_RESOURCES, "pillow.png")
28+
_OUTPUT_FILE = os.path.join(_RESOURCES, "pillow_on_beach.png")
29+
_PROMPT = "beach"
30+
31+
32+
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60)
33+
def test_edit_image_product_image() -> None:
34+
response = edit_image_product_image.edit_image_product_image(
35+
_PROJECT_ID,
36+
_LOCATION,
37+
_INPUT_FILE,
38+
_OUTPUT_FILE,
39+
_PROMPT,
40+
)
41+
42+
assert len(response[0]._image_bytes) > 1000
-522 KB
Loading
-116 KB
Loading
-9.14 KB
Loading
-1.6 MB
Loading
1.39 KB
Loading
734 KB
Loading
1.27 MB
Loading

generative_ai/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ pandas==1.3.5; python_version == '3.7'
22
pandas==2.0.1; python_version > '3.7'
33
pillow==9.5.0; python_version < '3.8'
44
pillow==10.0.1; python_version >= '3.8'
5-
google-cloud-aiplatform[pipelines]==1.42.1
5+
google-cloud-aiplatform[pipelines]==1.46.0
66
google-auth==2.17.3
77
anthropic[vertex]==0.21.3

0 commit comments

Comments
 (0)