Customize images based on a specified style using Imagen

Preview: Using theFirebase AI Logic SDKs to accessImagen models is a feature that's in Preview, which means that it isn't subject to any SLA or deprecation policy and could change in backwards-incompatible ways.

Editing withImagen is only supported if you're using theVertex AI Gemini API. It's also currently only supported for Android and Flutter apps. Support for other platforms is coming later in the year.


This page describes how to use thecustomization capability fromImagen toedit or generate images based on a specifiedstyleusing theFirebase AI Logic SDKs.

How it works: You provide a text prompt and at least one reference image that shows a specific style (like a pattern, texture, or design style). The model uses these inputs to generate a new image based on the specifiedstyle in the reference images.

For example, you can generate a new image of a kitchen based on an image from a popular retail catalog that you provide.

Important: Review the lists ofintended andunintendeduse cases so that you get better results with customization.

Jump to code



Before you begin

Only available when using theVertex AI Gemini API as your API provider.

If you haven't already, complete thegetting 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 anImagenModel instance.

This guide assumes you're using the latestFirebase AI Logic SDKs. If you're still using the "Vertex AI in Firebase" SDKs, see themigration guide.

Models that support this capability

Imagen offers image editing through itscapability model:

  • imagen-3.0-capability-001

Note that forImagen models, theglobal location isnot supported.

Send a style customization request

The following sample shows a style customization request that asks the modelto generate a new image with the style of the provided reference image(in this example, Van Gogh's "Starry Night").

Review theprompt templates later on this page tolearn about writing prompts and how to use reference images within them.

Swift

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

Kotlin

// Using this SDK to access Imagen models is a Preview release and requires opt-in@OptIn(PublicPreviewAPI::class)suspendfuncustomizeImage(){// Initialize the Vertex AI Gemini API backend service// Optionally specify the location to access the model (for example, `us-central1`)valai=Firebase.ai(backend=GenerativeBackend.vertexAI(location="us-central1"))// Create an `ImagenModel` instance with an Imagen "capability" modelvalmodel=ai.imagenModel("imagen-3.0-capability-001")// This example assumes 'referenceImage' is a pre-loaded Bitmap.// In a real app, this might come from the user's device or a URL.valreferenceImage:Bitmap=TODO("Load your reference image Bitmap here")// Define the style reference using the reference image.valstyleReference=ImagenStyleReference(image=referenceImage,referenceID=1,description="Van Gogh style")// Provide a prompt that describes the final image.// The "[1]" links the prompt to the style reference with ID 1.valprompt="A cat flying through outer space, in the Van Gogh style[1]"// Use the editImage API to perform the style customization.// Pass the list of references, the prompt, and an editing configuration.valeditedImage=model.editImage(references=listOf(styleReference),prompt=prompt,config=ImagenEditingConfig(editSteps=50// Number of editing steps, a higher value can improve quality))// Process the result}

Java

// Initialize the Vertex AI Gemini API backend service// Optionally specify the location to access the model (for example, `us-central1`)// Create an `ImagenModel` instance with an Imagen "capability" modelImagenModelimagenModel=FirebaseAI.getInstance(GenerativeBackend.vertexAI("us-central1")).imagenModel(/* modelName */"imagen-3.0-capability-001");ImagenModelFuturesmodel=ImagenModelFutures.from(imagenModel);// This example assumes 'referenceImage' is a pre-loaded Bitmap.// In a real app, this might come from the user's device or a URL.BitmapreferenceImage=null;// TODO("Load your image Bitmap here");// Define the style reference using the reference image.ImagenStyleReferencesubjectReference=newImagenStyleReference.Builder().setImage(referenceImage).setReferenceID(1).setDescription("Van Gogh style").build();// Provide a prompt that describes the final image.// The "[1]" links the prompt to the style reference with ID 1.Stringprompt="A cat flying through outer space, in the Van Gogh style[1]";// Define the editing configuration.ImagenEditingConfigimagenEditingConfig=newImagenEditingConfig.Builder().setEditSteps(50)// Number of editing steps, a higher value can improve quality.build();// Use the editImage API to perform the style customization.// Pass the list of references, the prompt, and the editing configuration.Futures.addCallback(model.editImage(Collections.singletonList(styleReference),prompt,imagenEditingConfig),newFutureCallback<ImagenGenerationResponse>(){@OverridepublicvoidonSuccess(ImagenGenerationResponseresult){if(result.getImages().isEmpty()){Log.d("TAG","No images generated");}Bitmapbitmap=result.getImages().get(0).asBitmap();// Use the bitmap to display the image in your UI}@OverridepublicvoidonFailure(Throwablet){// ...}},Executors.newSingleThreadExecutor());

Web

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

Dart

import'dart:typed_data';import'package:firebase_ai/firebase_ai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';// Initialize FirebaseAppawaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Vertex AI Gemini API backend service// Optionally specify a location to access the model (for example, `us-central1`)finalai=FirebaseAI.vertexAI(location:'us-central1');// Create an `ImagenModel` instance with an Imagen "capability" modelfinalmodel=ai.imagenModel(model:'imagen-3.0-capability-001');// This example assumes 'referenceImage' is a pre-loaded Uint8List.// In a real app, this might come from the user's device or a URL.finalUint8ListreferenceImage=Uint8List(0);// TODO: Load your reference image data here// Define the style reference using the reference image.finalstyleReference=ImagenStyleReference(image:referenceImage,referenceId:1,description:'Van Gogh style',);// Provide a prompt that describes the final image.// The "[1]" links the prompt to the style reference with ID 1.finalprompt="A cat flying through outer space, in the Van Gogh style[1]";try{// Use the editImage API to perform the style customization.// Pass the list of references, the prompt, and an editing configuration.finalresponse=awaitmodel.editImage([styleReference],prompt,config:ImagenEditingConfig(editSteps:50,// Number of editing steps, a higher value can improve quality),);// Process the result.if(response.images.isNotEmpty){finaleditedImage=response.images.first.bytes;// Use the editedImage (a Uint8List) to display the image, save it, etc.print('Image successfully generated!');}else{// Handle the case where no images were generated.print('Error: No images were generated.');}}catch(e){// Handle any potential errors during the API call.print('An error occurred:$e');}

Unity

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

Prompt templates

In the request, you provide reference images (up to 4 images) by defining anImagenStyleReference in which you specify a reference ID for an image(and optionally a style description, as well). Note that multiple images canhave the same reference ID (for example, multiple photos of the same pattern).

Then, when writing the prompt, you refer to these IDs. For example, you use[1] in the prompt to refer to images with the reference ID1. If you providea subject description, you can also include it in the prompt so that the promptis easier for a human to read.

Important: Review the lists ofintended andunintendeduse cases so that you get better results with customization.

The following table provides prompt templates that can be a startingpoint for writing prompts for customization based on style.

Use caseReference imagesPrompt templateExample
Object styleSubject image (1-4) Generate an image inSTYLE_DESCRIPTION [1] based on the following caption:IMAGE_DESCRIPTION. Generate an image inneon sign style [1] based on the following caption:a sign saying have a great day.
Person image stylization without face mesh inputSubject image (1-4) Create an image aboutSUBJECT_DESCRIPTION [1] to match the description: a portrait ofSUBJECT_DESCRIPTION [1]${PROMPT} Create an image abouta woman with short hair[1] to match the description: a portrait ofa woman with short hair[1]in 3d-cartoon style with blurred background. A cute and lovely character, with a smiling face, looking at the camera, pastel color tone ...
Person image stylization with face mesh input Subject image (1-3)

Facemesh control image (1)
Create an image aboutSUBJECT_DESCRIPTION [1] in the pose of theCONTROL_IMAGE [2] to match the description: a portrait ofSUBJECT_DESCRIPTION [1]${PROMPT} Create an image abouta woman with short hair [1] in the pose of thecontrol image [2] to match the description: a portrait ofa woman with short hair [1]in 3d-cartoon style with blurred background. A cute and lovely character, with a smiling face, looking at the camera, pastel color tone ...



Best practices and limitations

Use cases

The customization capability offers free-style prompting, which can give the impression that the model can do more than it's trained to do. The following sections describeintended use cases for customization, and non-exhaustiveexamples ofunintended use cases.

We recommend using this capability for the intended use cases, since we've trained the model on those use cases and expect good results for them. Conversely, if you push the model to do things outside of the intended use cases, you should expect poor results.

Intended use cases

The following areintended use cases for customization based on astyle:

  • Generate an image from text input that follows the specific style provided bya reference image.

  • Alter a photo of a person.

  • Alter a photo of a person and preserve their facial expression.

Examples of unintended use cases

The following is a non-exhaustive list ofunintended use cases forcustomization based on astyle. The model isn't trained for these use cases,and will likely produce poor results.

  • Generate an image from text and using a reference image, with the intent tohave some level of control of the generated composition from the referenceimage.

  • Generate an image of a person from a reference image that has a person with aparticular facial expression.

  • Place two people in a different scene, preserve their identities, and whilespecifying the style of the output image (such as an oil painting) using areference image.

  • Stylize a photo of a pet and turn it into a drawing, while preserving orspecifying the composition of the image.

  • Place a product, such as a cookie or a couch, into different scenes withdifferent product angles, and following a specific image style (such asphotorealistic with specific colors, lighting styles, or animation).

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-03 UTC.