Skip to content

Commit f748924

Browse files
feat(genai): Add new image Generation samples (#13549)
* feat(genai): Add new image Generation samples Local aware & Multiple images generation * feat(genai): code formatting
1 parent 8421334 commit f748924

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+314
-115
lines changed

genai/image_generation/imggen_canny_ctrl_type_with_txt_img.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
def canny_edge_customization(output_gcs_uri: str) -> str:
1717
# [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
1818
from google import genai
19-
from google.genai.types import ControlReferenceConfig, ControlReferenceImage, EditImageConfig, Image
19+
from google.genai.types import (
20+
ControlReferenceConfig,
21+
ControlReferenceImage,
22+
EditImageConfig,
23+
Image,
24+
)
2025

2126
client = genai.Client()
2227

genai/image_generation/imggen_inpainting_insert_mask_with_txt_img.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,25 @@
1818
def edit_inpainting_insert_mask(output_file: str) -> Image:
1919
# [START googlegenaisdk_imggen_inpainting_insert_mask_with_txt_img]
2020
from google import genai
21-
from google.genai.types import RawReferenceImage, MaskReferenceImage, MaskReferenceConfig, EditImageConfig
21+
from google.genai.types import (
22+
RawReferenceImage,
23+
MaskReferenceImage,
24+
MaskReferenceConfig,
25+
EditImageConfig,
26+
)
2227

2328
client = genai.Client()
2429

2530
# TODO(developer): Update and un-comment below line
2631
# output_file = "output-image.png"
2732

2833
raw_ref = RawReferenceImage(
29-
reference_image=Image.from_file(location='test_resources/fruit.png'), reference_id=0)
34+
reference_image=Image.from_file(location="test_resources/fruit.png"),
35+
reference_id=0,
36+
)
3037
mask_ref = MaskReferenceImage(
3138
reference_id=1,
32-
reference_image=Image.from_file(location='test_resources/fruit_mask.png'),
39+
reference_image=Image.from_file(location="test_resources/fruit_mask.png"),
3340
config=MaskReferenceConfig(
3441
mask_mode="MASK_MODE_USER_PROVIDED",
3542
mask_dilation=0.01,
@@ -56,4 +63,4 @@ def edit_inpainting_insert_mask(output_file: str) -> Image:
5663

5764

5865
if __name__ == "__main__":
59-
edit_inpainting_insert_mask(output_file="test_resources/fruit_edit.png")
66+
edit_inpainting_insert_mask(output_file="output_folder/fruit_edit.png")

genai/image_generation/imggen_inpainting_insert_with_txt_img.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@
1818
def edit_inpainting_insert(output_file: str) -> Image:
1919
# [START googlegenaisdk_imggen_inpainting_insert_with_txt_img]
2020
from google import genai
21-
from google.genai.types import RawReferenceImage, MaskReferenceImage, MaskReferenceConfig, EditImageConfig
21+
from google.genai.types import (
22+
RawReferenceImage,
23+
MaskReferenceImage,
24+
MaskReferenceConfig,
25+
EditImageConfig,
26+
)
2227

2328
client = genai.Client()
2429

2530
# TODO(developer): Update and un-comment below line
2631
# output_file = "output-image.png"
2732

2833
raw_ref = RawReferenceImage(
29-
reference_image=Image.from_file(location='test_resources/fruit.png'), reference_id=0)
34+
reference_image=Image.from_file(location="test_resources/fruit.png"),
35+
reference_id=0,
36+
)
3037
mask_ref = MaskReferenceImage(
3138
reference_id=1,
3239
reference_image=None,
@@ -56,4 +63,4 @@ def edit_inpainting_insert(output_file: str) -> Image:
5663

5764

5865
if __name__ == "__main__":
59-
edit_inpainting_insert(output_file="test_resources/fruit_edit.png")
66+
edit_inpainting_insert(output_file="output_folder/fruit_edit.png")

genai/image_generation/imggen_inpainting_removal_mask_with_txt_img.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,25 @@
1818
def edit_inpainting_removal_mask(output_file: str) -> Image:
1919
# [START googlegenaisdk_imggen_inpainting_removal_mask_with_txt_img]
2020
from google import genai
21-
from google.genai.types import RawReferenceImage, MaskReferenceImage, MaskReferenceConfig, EditImageConfig
21+
from google.genai.types import (
22+
RawReferenceImage,
23+
MaskReferenceImage,
24+
MaskReferenceConfig,
25+
EditImageConfig,
26+
)
2227

2328
client = genai.Client()
2429

2530
# TODO(developer): Update and un-comment below line
2631
# output_file = "output-image.png"
2732

2833
raw_ref = RawReferenceImage(
29-
reference_image=Image.from_file(location='test_resources/fruit.png'), reference_id=0)
34+
reference_image=Image.from_file(location="test_resources/fruit.png"),
35+
reference_id=0,
36+
)
3037
mask_ref = MaskReferenceImage(
3138
reference_id=1,
32-
reference_image=Image.from_file(location='test_resources/fruit_mask.png'),
39+
reference_image=Image.from_file(location="test_resources/fruit_mask.png"),
3340
config=MaskReferenceConfig(
3441
mask_mode="MASK_MODE_USER_PROVIDED",
3542
mask_dilation=0.01,
@@ -56,4 +63,4 @@ def edit_inpainting_removal_mask(output_file: str) -> Image:
5663

5764

5865
if __name__ == "__main__":
59-
edit_inpainting_removal_mask(output_file="test_resources/fruit_edit.png")
66+
edit_inpainting_removal_mask(output_file="output_folder/fruit_edit.png")

genai/image_generation/imggen_inpainting_removal_with_txt_img.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@
1818
def edit_inpainting_removal(output_file: str) -> Image:
1919
# [START googlegenaisdk_imggen_inpainting_removal_with_txt_img]
2020
from google import genai
21-
from google.genai.types import RawReferenceImage, MaskReferenceImage, MaskReferenceConfig, EditImageConfig
21+
from google.genai.types import (
22+
RawReferenceImage,
23+
MaskReferenceImage,
24+
MaskReferenceConfig,
25+
EditImageConfig,
26+
)
2227

2328
client = genai.Client()
2429

2530
# TODO(developer): Update and un-comment below line
2631
# output_file = "output-image.png"
2732

2833
raw_ref = RawReferenceImage(
29-
reference_image=Image.from_file(location='test_resources/fruit.png'), reference_id=0)
34+
reference_image=Image.from_file(location="test_resources/fruit.png"),
35+
reference_id=0,
36+
)
3037
mask_ref = MaskReferenceImage(
3138
reference_id=1,
3239
reference_image=None,
@@ -55,4 +62,4 @@ def edit_inpainting_removal(output_file: str) -> Image:
5562

5663

5764
if __name__ == "__main__":
58-
edit_inpainting_removal(output_file="test_resources/fruit_edit.png")
65+
edit_inpainting_removal(output_file="output_folder/fruit_edit.png")

genai/image_generation/imggen_mask_free_edit_with_txt_img.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def edit_mask_free(output_file: str) -> Image:
2626
# output_file = "output-image.png"
2727

2828
raw_ref = RawReferenceImage(
29-
reference_image=Image.from_file(location='test_resources/latte.jpg'), reference_id=0)
29+
reference_image=Image.from_file(location="test_resources/latte.jpg"),
30+
reference_id=0,
31+
)
3032

3133
image = client.models.edit_image(
3234
model="imagen-3.0-capability-001",
@@ -48,4 +50,4 @@ def edit_mask_free(output_file: str) -> Image:
4850

4951

5052
if __name__ == "__main__":
51-
edit_mask_free(output_file="test_resources/latte_edit.png")
53+
edit_mask_free(output_file="output_folder/latte_edit.png")

genai/image_generation/imggen_mmflash_edit_img_with_txt_img.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def generate_content() -> str:
2323
client = genai.Client()
2424

2525
# Using an image of Eiffel tower, with fireworks in the background.
26-
image = Image.open("example-image.png")
26+
image = Image.open("test_resources/example-image-eiffel-tower.png")
2727

2828
response = client.models.generate_content(
2929
model="gemini-2.0-flash-preview-image-generation",
@@ -35,15 +35,15 @@ def generate_content() -> str:
3535
print(part.text)
3636
elif part.inline_data:
3737
image = Image.open(BytesIO((part.inline_data.data)))
38-
image.save("bw-example-image.png")
38+
image.save("output_folder/bw-example-image.png")
3939
# Example response:
4040
# Here's the cartoon-style edit of the image:
4141
# Cartoon-style edit:
4242
# - Simplified the Eiffel Tower with bolder lines and slightly exaggerated proportions.
4343
# - Brightened and saturated the colors of the sky, fireworks, and foliage for a more vibrant, cartoonish look.
4444
# ....
4545
# [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
46-
return "bw-example-image.png"
46+
return "output_folder/bw-example-image.png"
4747

4848

4949
if __name__ == "__main__":
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 generate_content() -> str:
17+
# [START googlegenaisdk_imggen_mmflash_locale_aware_with_txt]
18+
from google import genai
19+
from google.genai.types import GenerateContentConfig, Modality
20+
from PIL import Image
21+
from io import BytesIO
22+
23+
client = genai.Client()
24+
25+
response = client.models.generate_content(
26+
model="gemini-2.0-flash-preview-image-generation",
27+
contents=("Generate a photo of a breakfast meal."),
28+
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
29+
)
30+
for part in response.candidates[0].content.parts:
31+
if part.text:
32+
print(part.text)
33+
elif part.inline_data:
34+
image = Image.open(BytesIO((part.inline_data.data)))
35+
image.save("output_folder/example-breakfast-meal.png")
36+
# Example response:
37+
# Generates a photo of a vibrant and appetizing breakfast meal.
38+
# The scene will feature a white plate with golden-brown pancakes
39+
# stacked neatly, drizzled with rich maple syrup and ...
40+
# [END googlegenaisdk_imggen_mmflash_locale_aware_with_txt]
41+
return "output_folder/example-breakfast-meal.png"
42+
43+
44+
if __name__ == "__main__":
45+
generate_content()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 generate_content() -> str:
17+
# [START googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
18+
from google import genai
19+
from google.genai.types import GenerateContentConfig, Modality
20+
from PIL import Image
21+
from io import BytesIO
22+
23+
client = genai.Client()
24+
25+
response = client.models.generate_content(
26+
model="gemini-2.0-flash-preview-image-generation",
27+
contents=("Generate 3 images a cat sitting on a chair."),
28+
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
29+
)
30+
saved_files = []
31+
image_counter = 1
32+
for part in response.candidates[0].content.parts:
33+
if part.text:
34+
print(part.text)
35+
elif part.inline_data:
36+
image = Image.open(BytesIO((part.inline_data.data)))
37+
filename = f"output_folder/example-cats-0{image_counter}.png"
38+
image.save(filename)
39+
saved_files.append(filename)
40+
image_counter += 1
41+
# Example response:
42+
# Image 1: A fluffy calico cat with striking green eyes is perched elegantly on a vintage wooden
43+
# chair with a woven seat. Sunlight streams through a nearby window, casting soft shadows and
44+
# highlighting the cat's fur.
45+
#
46+
# Image 2: A sleek black cat with intense yellow eyes is sitting upright on a modern, minimalist
47+
# white chair. The background is a plain grey wall, putting the focus entirely on the feline's
48+
# graceful posture.
49+
#
50+
# Image 3: A ginger tabby cat with playful amber eyes is comfortably curled up asleep on a plush,
51+
# oversized armchair upholstered in a soft, floral fabric. A corner of a cozy living room with a
52+
# warm lamp in the background can be seen.
53+
# [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
54+
return saved_files
55+
56+
57+
if __name__ == "__main__":
58+
generate_content()

genai/image_generation/imggen_mmflash_txt_and_img_with_txt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ def generate_content() -> int:
3030
),
3131
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
3232
)
33-
with open("paella-recipe.md", "w") as fp:
33+
with open("output_folder/paella-recipe.md", "w") as fp:
3434
for i, part in enumerate(response.candidates[0].content.parts):
3535
if part.text is not None:
3636
fp.write(part.text)
3737
elif part.inline_data is not None:
3838
image = Image.open(BytesIO((part.inline_data.data)))
39-
image.save(f"example-image-{i+1}.png")
40-
fp.write(f"![image](./example-image-{i+1}.png)")
39+
image.save(f"output_folder/example-image-{i+1}.png")
40+
fp.write(f"![image](example-image-{i+1}.png)")
4141
# Example response:
4242
# A markdown page for a Paella recipe(`paella-recipe.md`) has been generated.
4343
# It includes detailed steps and several images illustrating the cooking process.

0 commit comments

Comments
 (0)