Skip to content

Commit 50901ac

Browse files
authored
feat: update to latest nano banana model (#14627)
1 parent 13c3c2f commit 50901ac

5 files changed

Lines changed: 24 additions & 29 deletions

File tree

‎genai/image_generation/imggen_mmflash_edit_img_with_txt_img.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def generate_content() -> str:
1717
# [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
1818
from google import genai
19-
from google.genai.types import GenerateContentConfig, Modality
19+
from google.genai.types import GenerateContentConfig
2020
from PIL import Image
2121
from io import BytesIO
2222

@@ -26,19 +26,17 @@ def generate_content() -> str:
2626
image = Image.open("test_resources/example-image-eiffel-tower.png")
2727

2828
response = client.models.generate_content(
29-
model="gemini-3.1-flash-image",
29+
model="gemini-nano-banana-2.1",
3030
contents=[image, "Edit this image to make it look like a cartoon."],
31-
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
31+
config=GenerateContentConfig(response_modalities=["IMAGE"]),
3232
)
3333
for part in response.candidates[0].content.parts:
34-
if part.text:
35-
print(part.text)
36-
elif part.inline_data:
34+
if part.inline_data:
3735
image = Image.open(BytesIO((part.inline_data.data)))
38-
image.save("output_folder/bw-example-image.png")
36+
image.save("output_folder/cartoon-image.png")
3937

4038
# [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
41-
return "output_folder/bw-example-image.png"
39+
return "output_folder/cartoon-image.png"
4240

4341

4442
if __name__ == "__main__":

‎genai/image_generation/imggen_mmflash_img_with_vid.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def generate_content() -> str:
1717
# [START googlegenaisdk_imggen_mmflash_img_with_vid]
1818
from google import genai
19-
from google.genai.types import GenerateContentConfig, Modality, Part
19+
from google.genai.types import GenerateContentConfig, Part
2020
from PIL import Image
2121
from io import BytesIO
2222

@@ -26,20 +26,18 @@ def generate_content() -> str:
2626
video = "https://www.youtube.com/watch?v=rjoMZyxncUI"
2727

2828
response = client.models.generate_content(
29-
model="gemini-3.1-flash-image",
29+
model="gemini-nano-banana-2.1",
3030
contents=[
3131
Part.from_uri(
3232
file_uri=video,
3333
mime_type="video/mp4"
3434
),
3535
"Generate an infographic of the topics covered in this video."
3636
],
37-
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
37+
config=GenerateContentConfig(response_modalities=["IMAGE"]),
3838
)
3939
for part in response.candidates[0].content.parts:
40-
if part.text:
41-
print(part.text)
42-
elif part.inline_data:
40+
if part.inline_data:
4341
image = Image.open(BytesIO((part.inline_data.data)))
4442
image.save("output_folder/video-image.png")
4543

‎genai/image_generation/imggen_mmflash_txt_and_img_with_txt.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@
1616
def generate_content() -> int:
1717
# [START googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]
1818
from google import genai
19-
from google.genai.types import GenerateContentConfig, Modality
19+
from google.genai.types import GenerateContentConfig
2020
from PIL import Image
2121
from io import BytesIO
2222

2323
client = genai.Client()
2424

2525
response = client.models.generate_content(
26-
model="gemini-3.1-flash-image",
27-
contents=(
28-
"Generate an illustrated recipe for a paella."
29-
"Create images to go alongside the text as you generate the recipe"
30-
),
31-
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
26+
model="gemini-nano-banana-2.1",
27+
contents="Generate an illustrated recipe for paella. Create images to go alongside the text as you generate the recipe.",
28+
config=GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
3229
)
3330
with open("output_folder/paella-recipe.md", "w") as fp:
3431
for i, part in enumerate(response.candidates[0].content.parts):

‎genai/image_generation/imggen_mmflash_with_txt.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@ def generate_content() -> str:
1919
from io import BytesIO
2020

2121
from google import genai
22-
from google.genai.types import GenerateContentConfig, Modality
22+
from google.genai.types import GenerateContentConfig, ImageConfig
2323
from PIL import Image
2424

2525
client = genai.Client()
2626

2727
response = client.models.generate_content(
28-
model="gemini-3.1-flash-image",
29-
contents=("Generate an image of the Eiffel tower with fireworks in the background."),
28+
model="gemini-nano-banana-2.1",
29+
contents="Generate a high-contrast, grainy black and white street photography shot.",
3030
config=GenerateContentConfig(
31-
response_modalities=[Modality.TEXT, Modality.IMAGE],
31+
response_modalities=["IMAGE"],
32+
image_config=ImageConfig(
33+
aspect_ratio="3:2",
34+
image_size="1K",
35+
),
3236
),
3337
)
3438
for part in response.candidates[0].content.parts:
35-
if part.text:
36-
print(part.text)
37-
elif part.inline_data:
39+
if part.inline_data:
3840
image = Image.open(BytesIO((part.inline_data.data)))
3941
# Ensure the output directory exists
4042
output_dir = "output_folder"
4143
os.makedirs(output_dir, exist_ok=True)
42-
image.save(os.path.join(output_dir, "example-image-eiffel-tower.png"))
44+
image.save(os.path.join(output_dir, "example-image.png"))
4345

4446
# [END googlegenaisdk_imggen_mmflash_with_txt]
4547
return True

genai/image_generation/output_folder/bw-example-image.png renamed to genai/image_generation/output_folder/cartooon-image.png

File renamed without changes.

0 commit comments

Comments
 (0)