Skip to content

Conversation

madebymozart
Copy link
Contributor

Description

This pull request introduces the Generative Expand feature, a powerful implementation of outpainting that leverages the advanced capabilities of the Imagen 3.0 model. This new functionality empowers users to creatively extend the canvas of a generated image, seamlessly adding new content that is contextually consistent with the original.

The core objective is to allow users to transform a standard-sized generation into a larger, more comprehensive scene—whether it's widening a landscape, adding more context to a character portrait, or simply exploring what lies beyond the original frame.

Changes Introduced

  • 🖼️ New "Generative Expand" Functionality: Implemented the end-to-end logic for outpainting. The user can now select a generated image and expand its dimensions, with Imagen 3.0 intelligently filling in the new areas.
  • 🧠 Imagen 3.0 Integration: The backend service now calls the Imagen 3.0 model specifically for outpainting tasks, utilizing its state-of-the-art inpainting/outpainting capabilities to ensure high-quality, coherent results.
  • 🔒 Resolution Safeguards: To ensure system stability, manage computational costs, and maintain a high-quality user experience, specific constraints have been implemented. This prevents excessively large or "runaway" expansions that could lead to performance degradation or suboptimal outputs.

Technical Details & Implementation

The outpainting process works by sending the original image along with the desired new canvas dimensions to the model. The original image acts as a contextual prompt, and Imagen 3.0 generates the pixels for the new, empty regions.

A key implementation detail is the introduction of a resolution cap.

  • The final expanded image cannot exceed a maximum resolution of 4096x4096 pixels.
  • This restriction is validated on the backend before processing any request. If a user attempts to expand an image beyond this limit, the request will be gracefully rejected with a user-friendly error message. This prevents API abuse and ensures that generation times remain reasonable.

Note: This limit strikes a balance between offering creative freedom and maintaining the performance and reliability of the service.

How to Test 🧪

  1. Generate any initial image.
  2. In the image viewer/editor UI, locate and click the new "Expand" button.
  3. Choose a direction (e.g., left, right, top, bottom) and a size for the expansion.
  4. Confirm the action and wait for the generation to complete.
  5. Verify that the resulting image is a seamless and logical extension of the original.
  6. Test the limits:
    • Take a large image (e.g., 3000x3000).
    • Attempt to expand it in a way that would cause the total resolution to exceed 4096x4096.
    • Confirm that the UI displays an error message and the request is not sent.

Screenshots

| Before Expansion | After Expansion |

Screenshot 2025-10-06 at 1 40 33 PM * | * Screenshot 2025-10-06 at 1 40 39 PM * |

@madebymozart madebymozart self-assigned this Oct 6, 2025
@madebymozart madebymozart added the enhancement New feature or request label Oct 6, 2025
Copy link
Contributor

Summary of Changes

Hello @madebymozart, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces the "Generative Expand" feature, which empowers users to creatively extend the boundaries of existing images using Imagen 3.0's outpainting capabilities. The core objective is to transform standard-sized generations into larger, more comprehensive scenes by intelligently filling in new areas while maintaining contextual consistency. This functionality is supported by backend integration with Imagen 3.0 and includes resolution safeguards to ensure optimal performance and user experience.

Highlights

  • Generative Expand Feature: Introduces a new outpainting functionality, allowing users to extend the canvas of a generated image by seamlessly adding new, contextually consistent content.
  • Imagen 3.0 Integration: Leverages the advanced capabilities of the Imagen 3.0 model for outpainting tasks, ensuring high-quality and coherent results.
  • Resolution Safeguards: Implements a maximum resolution cap of 4096x4096 pixels for expanded images to maintain system stability, manage computational costs, and prevent performance degradation.
  • UI Enhancements: Adds a new 'Expand' button to the image viewer/editor UI, enabling users to easily initiate the outpainting process.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully introduces the Generative Expand (outpainting) feature using Imagen 3.0. The implementation is well-structured and integrates cleanly with the existing architecture. My review focuses on improving the robustness and maintainability of the new functionality. I've identified a potential user experience issue with the validation logic for the 'Expand' button, and I've also provided suggestions to address magic numbers, hardcoded strings, and minor stylistic improvements to align with best practices.

onClick = {
onOutpaintClick(promptTextField)
},
enabled = enabled && bitmap.width < 4096 && bitmap.height < 4096,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This enabled check is not robust enough. It allows the user to click 'Expand' on an image that will become too large after the operation (which doubles the dimensions), leading to a failed request and a poor user experience.

The validation should be predictive, ensuring the resulting dimensions are valid. For example, it should check if bitmap.width * 2 < 4096.

Additionally, the 4096 limit is a magic number and should be a shared constant.

Suggested change
enabled = enabled && bitmap.width < 4096 && bitmap.height < 4096,
enabled = enabled && (bitmap.width * 2) < 4096 && (bitmap.height * 2) < 4096,

}

fun outpaintImage(sourceImage: Bitmap, dimensions: Dimensions, prompt: String) {
if (dimensions.width > 4096 || dimensions.height > 4096) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The value 4096 is a magic number. It should be defined as a constant (e.g., in a companion object) to improve readability and maintainability. This constant could then be shared with the UI layer (ImagenEditingGenerationInput.kt) to keep the validation logic consistent.

fun outpaintImage(sourceImage: Bitmap, dimensions: Dimensions, prompt: String) {
if (dimensions.width > 4096 || dimensions.height > 4096) {
_uiState.value = ImagenEditingUIState.Error("Dimensions are too large. Maximum dimensions are 4096x4096")
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The semicolon in this return statement is not idiomatic in Kotlin and can be safely removed.

Suggested change
return;
return

contentDescription = "Outpainted image based on prompt: $prompt",
)
} catch (e: Exception) {
_uiState.value = ImagenEditingUIState.Error(e.localizedMessage ?: "An unknown error occurred during outpainting")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This fallback error message is hardcoded. It's recommended to move user-facing strings to res/values/strings.xml to support localization.

Additionally, for better debugging, consider logging the full exception e before setting the error state, as e.localizedMessage can sometimes be null or lack detail.

@madebymozart madebymozart changed the title [✨ Enhanced Smaple] Implement Generative Expand (Outpainting) with Imagen 3.0 [✨ Enhanced Sample] Implement Generative Expand (OutPainting) with Imagen 3.0 Oct 6, 2025
@lethargicpanda
Copy link
Collaborator

Let's wait until the redesign is done before we add new samples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants