Skip to content
Open
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
14 changes: 6 additions & 8 deletions genai/image_generation/imggen_mmflash_edit_img_with_txt_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def generate_content() -> str:
# [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
from google import genai
from google.genai.types import GenerateContentConfig, Modality
from google.genai.types import GenerateContentConfig
from PIL import Image
from io import BytesIO

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

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

# [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
return "output_folder/bw-example-image.png"
return "output_folder/cartoon-image.png"


if __name__ == "__main__":
Expand Down
10 changes: 4 additions & 6 deletions genai/image_generation/imggen_mmflash_img_with_vid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def generate_content() -> str:
# [START googlegenaisdk_imggen_mmflash_img_with_vid]
from google import genai
from google.genai.types import GenerateContentConfig, Modality, Part
from google.genai.types import GenerateContentConfig, Part
from PIL import Image
from io import BytesIO

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

response = client.models.generate_content(
model="gemini-3.1-flash-image",
model="gemini-nano-banana-2.1",
contents=[
Part.from_uri(
file_uri=video,
mime_type="video/mp4"
),
"Generate an infographic of the topics covered in this video."
],
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
config=GenerateContentConfig(response_modalities=["IMAGE"]),
)
for part in response.candidates[0].content.parts:
if part.text:
print(part.text)
elif part.inline_data:
if part.inline_data:
image = Image.open(BytesIO((part.inline_data.data)))
image.save("output_folder/video-image.png")

Expand Down
11 changes: 4 additions & 7 deletions genai/image_generation/imggen_mmflash_txt_and_img_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
def generate_content() -> int:
# [START googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]
from google import genai
from google.genai.types import GenerateContentConfig, Modality
from google.genai.types import GenerateContentConfig
from PIL import Image
from io import BytesIO

client = genai.Client()

response = client.models.generate_content(
model="gemini-3.1-flash-image",
contents=(
"Generate an illustrated recipe for a paella."
"Create images to go alongside the text as you generate the recipe"
),
config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),
model="gemini-nano-banana-2.1",
contents="Generate an illustrated recipe for paella. Create images to go alongside the text as you generate the recipe.",
config=GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]),
)
with open("output_folder/paella-recipe.md", "w") as fp:
for i, part in enumerate(response.candidates[0].content.parts):
Expand Down
18 changes: 10 additions & 8 deletions genai/image_generation/imggen_mmflash_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ def generate_content() -> str:
from io import BytesIO

from google import genai
from google.genai.types import GenerateContentConfig, Modality
from google.genai.types import GenerateContentConfig, ImageConfig
from PIL import Image

client = genai.Client()

response = client.models.generate_content(
model="gemini-3.1-flash-image",
contents=("Generate an image of the Eiffel tower with fireworks in the background."),
model="gemini-nano-banana-2.1",
contents="Generate a high-contrast, grainy black and white street photography shot.",
config=GenerateContentConfig(
response_modalities=[Modality.TEXT, Modality.IMAGE],
response_modalities=["IMAGE"],
image_config=ImageConfig(
aspect_ratio="3:2",
image_size="1K",
),
Comment thread
katiemn marked this conversation as resolved.
),
)
for part in response.candidates[0].content.parts:
if part.text:
print(part.text)
elif part.inline_data:
if part.inline_data:
image = Image.open(BytesIO((part.inline_data.data)))
# Ensure the output directory exists
output_dir = "output_folder"
os.makedirs(output_dir, exist_ok=True)
image.save(os.path.join(output_dir, "example-image-eiffel-tower.png"))
image.save(os.path.join(output_dir, "example-image.png"))

# [END googlegenaisdk_imggen_mmflash_with_txt]
return True
Expand Down
4 changes: 2 additions & 2 deletions genai/image_generation/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-genai==1.42.0
pillow==12.3.0; python_version >= "3.10"
google-genai==2.28.0
pillow==12.2.0; python_version >= "3.10"
5 changes: 2 additions & 3 deletions genai/image_generation/test_image_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

from google.cloud import storage

import pytest

import imggen_canny_ctrl_type_with_txt_img
import imggen_inpainting_insert_mask_with_txt_img
import imggen_inpainting_insert_with_txt_img
Expand All @@ -40,9 +38,10 @@
import imggen_upscale_with_img
import imggen_virtual_try_on_with_txt_img
import imggen_with_txt
import pytest

os.environ["GOOGLE_GENAI_USE_ENTERPRISE"] = "True"
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
os.environ["GOOGLE_CLOUD_LOCATION"] = "global"
# The project name is included in the CICD pipeline
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"

Expand Down
Loading