Skip to content

feat(genai): Add new image generation samples #13320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions genai/image_generation/imggen_canny_ctrl_type_with_txt_img.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def canny_edge_customization(output_gcs_uri: str) -> str:
# [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
from google import genai
from google.genai.types import ControlReferenceConfig, ControlReferenceImage, EditImageConfig, Image

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

# Create a reference image out of an existing canny edge image signal
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_canny.png
control_reference_image = ControlReferenceImage(
reference_id=1,
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/car_canny.png"),
config=ControlReferenceConfig(control_type="CONTROL_TYPE_CANNY"),
)

image = client.models.edit_image(
model="imagen-3.0-capability-001",
prompt="a watercolor painting of a red car[1] driving on a road",
reference_images=[control_reference_image],
config=EditImageConfig(
edit_mode="EDIT_MODE_CONTROLLED_EDITING",
number_of_images=1,
seed=1,
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
person_generation="ALLOW_ADULT",
output_gcs_uri=output_gcs_uri,
),
)

# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)
# [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
return image.generated_images[0].image.gcs_uri


if __name__ == "__main__":
canny_edge_customization(output_gcs_uri="gs://your-bucket/your-prefix")
55 changes: 55 additions & 0 deletions genai/image_generation/imggen_raw_reference_with_txt_img.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def style_transfer_customization(output_gcs_uri: str) -> str:
# [START googlegenaisdk_imggen_raw_reference_with_txt_img]
from google import genai
from google.genai.types import EditImageConfig, Image, RawReferenceImage

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

# Create a raw reference image of teacup stored in Google Cloud Storage
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/teacup-1.png
raw_ref_image = RawReferenceImage(
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/teacup-1.png"),
reference_id=1
)

image = client.models.edit_image(
model="imagen-3.0-capability-001",
prompt="transform the subject in the image so that the teacup[1] is made entirely out of chocolate",
reference_images=[raw_ref_image],
config=EditImageConfig(
edit_mode="EDIT_MODE_DEFAULT",
number_of_images=1,
seed=1,
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
person_generation="ALLOW_ADULT",
output_gcs_uri=output_gcs_uri,
),
)

# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)
# [END googlegenaisdk_imggen_raw_reference_with_txt_img]
return image.generated_images[0].image.gcs_uri


if __name__ == "__main__":
style_transfer_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def scribble_customization(output_gcs_uri: str) -> str:
# [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
from google import genai
from google.genai.types import ControlReferenceConfig, ControlReferenceImage, EditImageConfig, Image

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

# Create a reference image out of an existing scribble image signal
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_scribble.png
control_reference_image = ControlReferenceImage(
reference_id=1,
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/car_scribble.png"),
config=ControlReferenceConfig(control_type="CONTROL_TYPE_SCRIBBLE"),
)

image = client.models.edit_image(
model="imagen-3.0-capability-001",
prompt="an oil painting showing the side of a red car[1]",
reference_images=[control_reference_image],
config=EditImageConfig(
edit_mode="EDIT_MODE_CONTROLLED_EDITING",
number_of_images=1,
seed=1,
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
person_generation="ALLOW_ADULT",
output_gcs_uri=output_gcs_uri,
),
)

# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)
# [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
return image.generated_images[0].image.gcs_uri


if __name__ == "__main__":
scribble_customization(output_gcs_uri="gs://your-bucket/your-prefix")
56 changes: 56 additions & 0 deletions genai/image_generation/imggen_style_reference_with_txt_img.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def style_customization(output_gcs_uri: str) -> str:
# [START googlegenaisdk_imggen_style_reference_with_txt_img]
from google import genai
from google.genai.types import EditImageConfig, Image, StyleReferenceConfig, StyleReferenceImage

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

# Create a style reference image of a neon sign stored in Google Cloud Storage
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png
style_reference_image = StyleReferenceImage(
reference_id=1,
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/neon.png"),
config=StyleReferenceConfig(style_description="neon sign"),
)

image = client.models.edit_image(
model="imagen-3.0-capability-001",
prompt="generate an image of a neon sign [1] with the words: have a great day",
reference_images=[style_reference_image],
config=EditImageConfig(
edit_mode="EDIT_MODE_DEFAULT",
number_of_images=1,
seed=1,
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
person_generation="ALLOW_ADULT",
output_gcs_uri=output_gcs_uri,
),
)

# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)
# [END googlegenaisdk_imggen_style_reference_with_txt_img]
return image.generated_images[0].image.gcs_uri


if __name__ == "__main__":
style_customization(output_gcs_uri="gs://your-bucket/your-prefix")
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def subject_customization(output_gcs_uri: str) -> str:
# [START googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
from google import genai
from google.genai.types import (
ControlReferenceConfig,
ControlReferenceImage,
EditImageConfig,
Image,
SubjectReferenceConfig,
SubjectReferenceImage
)

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

# Create subject and control reference images of a photograph stored in Google Cloud Storage
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/person.png
subject_reference_image = SubjectReferenceImage(
reference_id=1,
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/person.png"),
config=SubjectReferenceConfig(
subject_description="a headshot of a woman", subject_type="SUBJECT_TYPE_PERSON"
),
)
control_reference_image = ControlReferenceImage(
reference_id=2,
reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/person.png"),
config=ControlReferenceConfig(control_type="CONTROL_TYPE_FACE_MESH"),
)

image = client.models.edit_image(
model="imagen-3.0-capability-001",
prompt="""
a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by a professional artist,
light and low-contrast stokes, bright pastel colors, a warm atmosphere, clean background, grainy paper,
bold visible brushstrokes, patchy details
""",
reference_images=[subject_reference_image, control_reference_image],
config=EditImageConfig(
edit_mode="EDIT_MODE_DEFAULT",
number_of_images=1,
seed=1,
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
person_generation="ALLOW_ADULT",
output_gcs_uri=output_gcs_uri,
),
)

# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)
# [END googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
return image.generated_images[0].image.gcs_uri


if __name__ == "__main__":
subject_customization(output_gcs_uri="gs://your-bucket/your-prefix")
42 changes: 42 additions & 0 deletions genai/image_generation/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": True,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# If you need to use a specific version of pip,
# change pip_version_override to the string representation
# of the version number, for example, "20.2.4"
"pip_version_override": None,
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
3 changes: 3 additions & 0 deletions genai/image_generation/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
google-api-core==2.24.0
google-cloud-storage==2.19.0
pytest==8.2.0
2 changes: 2 additions & 0 deletions genai/image_generation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-genai==1.11.0
pillow==11.1.0
Loading