Expand the content of an image using outpainting with Imagen


This page describes how to use outpainting using Imagen to expand the content of an image beyond its original borders using the Firebase AI Logic SDKs.

Outpainting is a type of mask-based editing. A mask is a digital overlay defining the specific area you want to edit.

How it works: You provide an original image and a corresponding masked image — either auto-generated or provided by you — that defines a mask of the new, expanded area. You can also optionally provide a text prompt describing what you want in the expanded area, or the model can intelligently decide what will logically continue the existing scene. The model generates the new content and fills in the masked area.

For example, you can change an image's aspect ratio or add more background context.

Jump to code

Before you begin

Only available when using the Vertex AI Gemini API as your API provider.

If you haven't already, complete the getting started guide, which describes how to set up your Firebase project, connect your app to Firebase, add the SDK, initialize the backend service for your chosen API provider, and create an ImagenModel instance.

Models that support this capability

Imagen offers image editing through its capability model:

  • imagen-3.0-capability-001

Note that for Imagen models, the global location is not supported.

Expand content of an image

Before trying this sample, complete the Before you begin section of this guide to set up your project and app.

The following sample shows how to expand an image beyond its original borders — using a mask defined in an image that you provide. You provide the original image, a text prompt, and the masked image. Note the following about the original and masked image:

  • The masked image must have the pixel dimensions of the targeted size of the final outpainted image.

  • The original image must include additional padding to match the pixel dimensions of the masked image.

Providing a text prompt is optional if you want the model to intelligently decide what will logically continue the existing scene. If you want specific content within the expanded area, you need to specify that in a text prompt.

Swift

Image editing with Imagen models isn't supported for Swift. Check back later this year!

Kotlin

To expand an image, use editImage() and set the editing config to use ImagenEditMode.OUTPAINT.
Note that you can optionally use outpaintImage() instead of editImage(), and you don't need to specify the editing mode.

Check out the quickstart for sample code for outpainting.

Java

To expand an image, use editImage() and set the editing config to use ImagenEditMode.OUTPAINT.
Note that you can optionally use outpaintImage() instead of editImage(), and you don't need to specify the editing mode.

Check out the quickstart for sample code for outpainting.

Web

Image editing with Imagen models isn't supported for Web apps. Check back later this year!

Dart

To expand an image, use editImage() and set the editing config to use ImagenEditMode.OUTPAINT.

Check out the quickstart for sample code for outpainting.

Unity

Image editing with Imagen models isn't supported for Unity. Check back later this year!

Best practices and limitations

We recommend dilating the mask when editing an image. This can help smooth the borders of an edit and make it seem more convincing. Generally, a dilation value of 1% or 2% is recommended (0.01 or 0.02).


Give feedback about your experience with Firebase AI Logic