Skip to content

Commit 754f8fe

Browse files
authored
feat: add imagen edit image sample (GoogleCloudPlatform#11210)
* feat: add imagen edit image sample * fix import order * fix import order * fix filepaths * delete requirements files and noxfile in imagen subdirectory
1 parent 31cf81a commit 754f8fe

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 an image without using a mask. The
16+
edit is applied to the entire image and is saved to a new file.
17+
Example usage:
18+
python edit_image_mask_free.py --project_id <project-id> --location <location> \
19+
--input_file <filepath> --output_file <filepath> --prompt <text>
20+
"""
21+
22+
# [START aiplatform_imagen_edit_image_mask_free]
23+
24+
import argparse
25+
26+
import vertexai
27+
from vertexai.preview.vision_models import Image, ImageGenerationModel
28+
29+
30+
def edit_image_mask_free(
31+
project_id: str, location: str, input_file: str, output_file: str, prompt: str
32+
) -> vertexai.preview.vision_models.ImageGenerationResponse:
33+
"""Edit a local image without using a mask.
34+
Args:
35+
project_id: Google Cloud project ID, used to initialize Vertex AI.
36+
location: Google Cloud region, used to initialize Vertex AI.
37+
input_file: Local path to the input image file. Image can be in PNG or JPEG format.
38+
output_file: Local path to the output image file.
39+
prompt: The text prompt describing what you want to see."""
40+
41+
vertexai.init(project=project_id, location=location)
42+
43+
model = ImageGenerationModel.from_pretrained("imagegeneration@002")
44+
base_img = Image.load_from_file(location=input_file)
45+
46+
images = model.edit_image(
47+
base_image=base_img,
48+
prompt=prompt,
49+
# Optional parameters
50+
seed=1,
51+
# Controls the strength of the prompt.
52+
# -- 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
53+
guidance_scale=21,
54+
number_of_images=1,
55+
)
56+
57+
images[0].save(location=output_file)
58+
59+
# Optional. View the edited image in a notebook.
60+
# images[0].show()
61+
62+
print(f"Created output image using {len(images[0]._image_bytes)} bytes")
63+
64+
return images
65+
66+
67+
# [END aiplatform_imagen_edit_image_mask_free]
68+
69+
if __name__ == "__main__":
70+
parser = argparse.ArgumentParser()
71+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
72+
parser.add_argument(
73+
"--location",
74+
help="The location in which to initialize Vertex AI.",
75+
default="us-central1",
76+
)
77+
parser.add_argument(
78+
"--input_file",
79+
help="The local path to the input file (e.g., 'my-input.png').",
80+
required=True,
81+
)
82+
parser.add_argument(
83+
"--output_file",
84+
help="The local path to the output file (e.g., 'my-output.png').",
85+
required=True,
86+
)
87+
parser.add_argument(
88+
"--prompt",
89+
help="The text prompt describing what you want to see (e.g., 'a dog').",
90+
required=True,
91+
)
92+
args = parser.parse_args()
93+
edit_image_mask_free(
94+
args.project_id,
95+
args.location,
96+
args.input_file,
97+
args.output_file,
98+
args.prompt,
99+
)
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_mask_free
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, "cat.png")
28+
_OUTPUT_FILE = os.path.join(_RESOURCES, "dog.png")
29+
_PROMPT = "a dog"
30+
31+
32+
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60)
33+
def test_edit_image_mask_free() -> None:
34+
response = edit_image_mask_free.edit_image_mask_free(
35+
_PROJECT_ID,
36+
_LOCATION,
37+
_INPUT_FILE,
38+
_OUTPUT_FILE,
39+
_PROMPT,
40+
)
41+
42+
assert len(response[0]._image_bytes) > 1000
1.43 MB
Loading
948 KB
Loading

generative_ai/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
pandas==1.3.5; python_version == '3.7'
22
pandas==2.0.1; python_version > '3.7'
3+
pillow==9.5.0; python_version < '3.8'
4+
pillow==10.0.1; python_version >= '3.8'
35
google-cloud-aiplatform[pipelines]==1.42.0
46
google-auth==2.17.3

0 commit comments

Comments
 (0)