Imagen 4 Image Upscale

Source notebook

Repo path: vision/getting-started/imagen4_upscale.ipynb · Open on GitHub · intro

Shows how to upscale generated, local, and Cloud Storage images with Imagen 4 using google-genai.

Summary

This notebook teaches Imagen 4 image upscaling through the Google Gen AI SDK for Python on Agent Platform. It sets up project, location, and client configuration, then demonstrates upscaling an Imagen-generated image, a downloaded local image, and an image referenced from Cloud Storage.

Key code patterns

Create GenAI client

from google import genai
 
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)

Configures the Google Gen AI SDK client with enterprise mode, project, and region.

Generate then upscale

image = client.models.generate_images(
    model="imagen-4.0-generate-001",
    prompt="A cartoon logo of a cat in a chef hat",
    config=types.GenerateImagesConfig(
        aspect_ratio="1:1", number_of_images=1, image_size="2K"
    ),
)
upscale = client.models.upscale_image(
    model="imagen-4.0-upscale-preview",
    image=image.generated_images[0].image,
    upscale_factor="x2",
)

Shows the end-to-end flow from Imagen generation output to upscaling.

Upscale local file

image = "boats.jpeg"
upscale = client.models.upscale_image(
    model="imagen-4.0-upscale-preview",
    image=types.Image.from_file(location=image),
    upscale_factor="x3",
)

Uses types.Image.from_file to send a local image into the upscaling model.

Upscale Cloud Storage image

image = "gs://cloud-samples-data/generative-ai/image/daisy.jpg"
upscale = client.models.upscale_image(
    model="imagen-4.0-upscale-preview",
    image=types.Image(gcs_uri=image),
    upscale_factor="x4",
)

Uses a Cloud Storage URI as the source image for upscaling.

Models & APIs used

When to use this

Use this pattern when you need to increase resolution for Imagen outputs or existing images from local disk or Cloud Storage.

Gotchas & caveats

  • Requires an existing Google Cloud project with the Agent Platform API enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID must be set directly or via GOOGLE_CLOUD_PROJECT.
  • LOCATION defaults to us-central1 when GOOGLE_CLOUD_REGION is unset.
  • Generated-image settings shown are aspect ratios 1:1, 9:16, 16:9, 3:4, 4:3; number_of_images 1 to 4; image_size 1K or 2K.
  • Upscale factors shown are x2, x3, and x4.
  • Imagen 4 images get SynthID digital watermarking by default.

Best practices

  • Install or upgrade google-genai before using the notebook.
  • Authenticate only in Colab by checking for google.colab in sys.modules.
  • Keep generation and upscaling model IDs in variables.
  • Use types.GenerateImagesConfig for aspect ratio, image count, and image size.
  • Use types.Image.from_file for local files and types.Image(gcs_uri=…) for Cloud Storage images.