Skip to content

Commit 189a057

Browse files
authored
feat(genai): Add new image generation samples (#13320)
* feat: genai samples for imagen customization * fix: change genai version * chore: install pillow * fix: save args * feat:add starting image * fix: comments * fix: lint * fix: tests * fix: comments * fix: lint
1 parent ee07ac3 commit 189a057

9 files changed

+425
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
16+
def canny_edge_customization(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
18+
from google import genai
19+
from google.genai.types import ControlReferenceConfig, ControlReferenceImage, EditImageConfig, Image
20+
21+
client = genai.Client()
22+
23+
# TODO(developer): Update and un-comment below line
24+
# output_gcs_uri = "gs://your-bucket/your-prefix"
25+
26+
# Create a reference image out of an existing canny edge image signal
27+
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_canny.png
28+
control_reference_image = ControlReferenceImage(
29+
reference_id=1,
30+
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/car_canny.png"),
31+
config=ControlReferenceConfig(control_type="CONTROL_TYPE_CANNY"),
32+
)
33+
34+
image = client.models.edit_image(
35+
model="imagen-3.0-capability-001",
36+
prompt="a watercolor painting of a red car[1] driving on a road",
37+
reference_images=[control_reference_image],
38+
config=EditImageConfig(
39+
edit_mode="EDIT_MODE_CONTROLLED_EDITING",
40+
number_of_images=1,
41+
seed=1,
42+
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
43+
person_generation="ALLOW_ADULT",
44+
output_gcs_uri=output_gcs_uri,
45+
),
46+
)
47+
48+
# Example response:
49+
# gs://your-bucket/your-prefix
50+
print(image.generated_images[0].image.gcs_uri)
51+
# [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
52+
return image.generated_images[0].image.gcs_uri
53+
54+
55+
if __name__ == "__main__":
56+
canny_edge_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
16+
def style_transfer_customization(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_imggen_raw_reference_with_txt_img]
18+
from google import genai
19+
from google.genai.types import EditImageConfig, Image, RawReferenceImage
20+
21+
client = genai.Client()
22+
23+
# TODO(developer): Update and un-comment below line
24+
# output_gcs_uri = "gs://your-bucket/your-prefix"
25+
26+
# Create a raw reference image of teacup stored in Google Cloud Storage
27+
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/teacup-1.png
28+
raw_ref_image = RawReferenceImage(
29+
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/teacup-1.png"),
30+
reference_id=1
31+
)
32+
33+
image = client.models.edit_image(
34+
model="imagen-3.0-capability-001",
35+
prompt="transform the subject in the image so that the teacup[1] is made entirely out of chocolate",
36+
reference_images=[raw_ref_image],
37+
config=EditImageConfig(
38+
edit_mode="EDIT_MODE_DEFAULT",
39+
number_of_images=1,
40+
seed=1,
41+
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
42+
person_generation="ALLOW_ADULT",
43+
output_gcs_uri=output_gcs_uri,
44+
),
45+
)
46+
47+
# Example response:
48+
# gs://your-bucket/your-prefix
49+
print(image.generated_images[0].image.gcs_uri)
50+
# [END googlegenaisdk_imggen_raw_reference_with_txt_img]
51+
return image.generated_images[0].image.gcs_uri
52+
53+
54+
if __name__ == "__main__":
55+
style_transfer_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
16+
def scribble_customization(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
18+
from google import genai
19+
from google.genai.types import ControlReferenceConfig, ControlReferenceImage, EditImageConfig, Image
20+
21+
client = genai.Client()
22+
23+
# TODO(developer): Update and un-comment below line
24+
# output_gcs_uri = "gs://your-bucket/your-prefix"
25+
26+
# Create a reference image out of an existing scribble image signal
27+
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_scribble.png
28+
control_reference_image = ControlReferenceImage(
29+
reference_id=1,
30+
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/car_scribble.png"),
31+
config=ControlReferenceConfig(control_type="CONTROL_TYPE_SCRIBBLE"),
32+
)
33+
34+
image = client.models.edit_image(
35+
model="imagen-3.0-capability-001",
36+
prompt="an oil painting showing the side of a red car[1]",
37+
reference_images=[control_reference_image],
38+
config=EditImageConfig(
39+
edit_mode="EDIT_MODE_CONTROLLED_EDITING",
40+
number_of_images=1,
41+
seed=1,
42+
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
43+
person_generation="ALLOW_ADULT",
44+
output_gcs_uri=output_gcs_uri,
45+
),
46+
)
47+
48+
# Example response:
49+
# gs://your-bucket/your-prefix
50+
print(image.generated_images[0].image.gcs_uri)
51+
# [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
52+
return image.generated_images[0].image.gcs_uri
53+
54+
55+
if __name__ == "__main__":
56+
scribble_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
16+
def style_customization(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_imggen_style_reference_with_txt_img]
18+
from google import genai
19+
from google.genai.types import EditImageConfig, Image, StyleReferenceConfig, StyleReferenceImage
20+
21+
client = genai.Client()
22+
23+
# TODO(developer): Update and un-comment below line
24+
# output_gcs_uri = "gs://your-bucket/your-prefix"
25+
26+
# Create a style reference image of a neon sign stored in Google Cloud Storage
27+
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png
28+
style_reference_image = StyleReferenceImage(
29+
reference_id=1,
30+
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/neon.png"),
31+
config=StyleReferenceConfig(style_description="neon sign"),
32+
)
33+
34+
image = client.models.edit_image(
35+
model="imagen-3.0-capability-001",
36+
prompt="generate an image of a neon sign [1] with the words: have a great day",
37+
reference_images=[style_reference_image],
38+
config=EditImageConfig(
39+
edit_mode="EDIT_MODE_DEFAULT",
40+
number_of_images=1,
41+
seed=1,
42+
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
43+
person_generation="ALLOW_ADULT",
44+
output_gcs_uri=output_gcs_uri,
45+
),
46+
)
47+
48+
# Example response:
49+
# gs://your-bucket/your-prefix
50+
print(image.generated_images[0].image.gcs_uri)
51+
# [END googlegenaisdk_imggen_style_reference_with_txt_img]
52+
return image.generated_images[0].image.gcs_uri
53+
54+
55+
if __name__ == "__main__":
56+
style_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
16+
def subject_customization(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
18+
from google import genai
19+
from google.genai.types import (
20+
ControlReferenceConfig,
21+
ControlReferenceImage,
22+
EditImageConfig,
23+
Image,
24+
SubjectReferenceConfig,
25+
SubjectReferenceImage
26+
)
27+
28+
client = genai.Client()
29+
30+
# TODO(developer): Update and un-comment below line
31+
# output_gcs_uri = "gs://your-bucket/your-prefix"
32+
33+
# Create subject and control reference images of a photograph stored in Google Cloud Storage
34+
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/person.png
35+
subject_reference_image = SubjectReferenceImage(
36+
reference_id=1,
37+
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/person.png"),
38+
config=SubjectReferenceConfig(
39+
subject_description="a headshot of a woman", subject_type="SUBJECT_TYPE_PERSON"
40+
),
41+
)
42+
control_reference_image = ControlReferenceImage(
43+
reference_id=2,
44+
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/person.png"),
45+
config=ControlReferenceConfig(control_type="CONTROL_TYPE_FACE_MESH"),
46+
)
47+
48+
image = client.models.edit_image(
49+
model="imagen-3.0-capability-001",
50+
prompt="""
51+
a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by a professional artist,
52+
light and low-contrast stokes, bright pastel colors, a warm atmosphere, clean background, grainy paper,
53+
bold visible brushstrokes, patchy details
54+
""",
55+
reference_images=[subject_reference_image, control_reference_image],
56+
config=EditImageConfig(
57+
edit_mode="EDIT_MODE_DEFAULT",
58+
number_of_images=1,
59+
seed=1,
60+
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
61+
person_generation="ALLOW_ADULT",
62+
output_gcs_uri=output_gcs_uri,
63+
),
64+
)
65+
66+
# Example response:
67+
# gs://your-bucket/your-prefix
68+
print(image.generated_images[0].image.gcs_uri)
69+
# [END googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
70+
return image.generated_images[0].image.gcs_uri
71+
72+
73+
if __name__ == "__main__":
74+
subject_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-api-core==2.24.0
2+
google-cloud-storage==2.19.0
3+
pytest==8.2.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-genai==1.11.0
2+
pillow==11.1.0

0 commit comments

Comments
 (0)