Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat: new image model sample update #13382

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 2 commits into from
May 20, 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
27 changes: 12 additions & 15 deletions 27 genai/image_generation/imggen_with_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from google.genai.types import Image

def generate_images(output_gcs_uri: str) -> str:

def generate_images(output_file: str) -> Image:
# [START googlegenaisdk_imggen_with_txt]
from google import genai
from google.genai.types import GenerateImagesConfig

client = genai.Client()

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

image = client.models.generate_images(
model="imagen-3.0-generate-002",
model="imagen-4.0-generate-preview-05-20",
prompt="A dog reading a newspaper",
config=GenerateImagesConfig(
aspect_ratio="1:1",
number_of_images=1,
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
person_generation="DONT_ADULT",
output_gcs_uri=output_gcs_uri,
),
)
katiemn marked this conversation as resolved.
Show resolved Hide resolved

image.generated_images[0].image.save(output_file)
katiemn marked this conversation as resolved.
Show resolved Hide resolved

print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
katiemn marked this conversation as resolved.
Show resolved Hide resolved
# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)
# Created output image using 1234567 bytes

# [END googlegenaisdk_imggen_with_txt]
return image.generated_images[0].image.gcs_uri
return image.generated_images[0].image


if __name__ == "__main__":
generate_images(output_gcs_uri="gs://your-bucket/your-prefix")
generate_images(output_file="test_resources/dog_newspaper.png")
4 changes: 3 additions & 1 deletion 4 genai/image_generation/test_image_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"

GCS_OUTPUT_BUCKET = "python-docs-samples-tests"
RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources")
OUTPUT_FILE = os.path.join(RESOURCES, "dog_newspaper.png")


@pytest.fixture(scope="session")
Expand All @@ -58,7 +60,7 @@ def output_gcs_uri() -> str:

def test_img_generation(output_gcs_uri: str) -> None:
katiemn marked this conversation as resolved.
Show resolved Hide resolved
response = imggen_with_txt.generate_images(
output_gcs_uri=output_gcs_uri
OUTPUT_FILE
)
assert response
katiemn marked this conversation as resolved.
Show resolved Hide resolved
katiemn marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.