Generate images using Imagen


The Firebase AI Logic SDKs give you access to the Imagen 3 models (via the Imagen API) so that you can generate images from a text prompt. With this capability, you can do things like:

  • Generate images from prompts written in natural language
  • Generate images in a wide range of formats and styles
  • Render text in images

Note that Firebase AI Logic doesn't yet support all the features available for the Imagen models. Learn more in Supported capabilities and features later on this page.

Jump to code for text-only input

Before you begin

Click your Gemini API provider to view provider-specific content and code on this page.

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

Image generation is supported by the Imagen 3 models.

Note that Firebase AI Logic also supports image generation by Gemini models—documentation is coming soon!

Generate images from text-only input

You can ask an Imagen model to generate images by prompting with text. You can generate one image or multiple images.

Generate one image from text-only input

Before trying this sample, complete the Before you begin section of this guide to set up your project and app.
In that section, you'll also click a button for your chosen Gemini API provider so that you see provider-specific content on this page.

You can ask an Imagen model to generate a single image by prompting with text.

Make sure to create an ImagenModel instance and call generateImages.

Swift


import FirebaseAI

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())


// Create an `ImagenModel` instance with a model that supports your use case
let model = ai.imagenModel(modelName: "imagen-3.0-generate-002")

// Provide an image generation prompt
let prompt = "An astronaut riding a horse"

// To generate an image, call `generateImages` with the text prompt
let response = try await model.generateImages(prompt: prompt)

// Handle the generated image
guard let image = response.images.first else {
  fatalError("No image in the response.")
}
let uiImage = UIImage(data: image.data)

Kotlin


// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun generateImage() {
  // Initialize the Gemini Developer API backend service
  // Create an `ImagenModel` instance with an Imagen model that supports your use case
  val imagenModel = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel("imagen-3.0-generate-002")

  // Provide an image generation prompt
  val prompt = "An astronaut riding a horse"

  // To generate an image, call `generateImages` with the text prompt
  val imageResponse = imagenModel.generateImages(prompt)

  // Handle the generated image
  val image = imageResponse.images.first()

  val bitmapImage = image.asBitmap()
}

Java


// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModel imagenModel = FirebaseAI.getInstance(GenerativeBackend.googleAI()).imagenModel(
        /* modelName */ "imagen-3.0-generate-002");

ImagenModelFutures model = ImagenModelFutures.from(imagenModel);

// Provide an image generation prompt
String prompt = "An astronaut riding a horse";

// To generate an image, call `generateImages` with the text prompt
Futures.addCallback(model.generateImages(prompt), new FutureCallback<ImagenGenerationResponse<ImagenInlineImage>>() {
    @Override
    public void onSuccess(ImagenGenerationResponse<ImagenInlineImage> result) {
        if (result.getImages().isEmpty()) {
            Log.d("TAG", "No images generated");
        }
        Bitmap bitmap = result.getImages().get(0).asBitmap();
        // Use the bitmap to display the image in your UI
    }

    @Override
    public void onFailure(Throwable t) {
        // ...
    }
}, Executors.newSingleThreadExecutor());

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });


// Create an `ImagenModel` instance with an Imagen 3 model that supports your use case
const imagenModel = getImagenModel(ai, { model: "imagen-3.0-generate-002" });

// Provide an image generation prompt
const prompt = "An astronaut riding a horse.";

// To generate an image, call `generateImages` with the text prompt
const response = await imagenModel.generateImages(prompt)

// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (response.filteredReason) {
  console.log(response.filteredReason);
}

if (response.images.length == 0) {
  throw new Error("No images in the response.")
}

const image = response.images[0];

Dart

import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);


// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final model =
  FirebaseAI.googleAI().imagenModel(model: 'imagen-3.0-generate-002');

// Provide an image generation prompt
const prompt = 'An astronaut riding a horse.';

// To generate an image, call `generateImages` with the text prompt
final response = await model.generateImages(prompt);

if (response.images.isNotEmpty) {
  final image = response.images[0];
  // Process the image
} else {
  // Handle the case where no images were generated
  print('Error: No images were generated.');
}

Unity

Using Imagen is not yet supported for Unity, but check back soon!

Learn how to choose a model appropriate for your use case and app.

Generate multiple images from text-only input

Before trying this sample, complete the Before you begin section of this guide to set up your project and app.
In that section, you'll also click a button for your chosen Gemini API provider so that you see provider-specific content on this page.

By default, Imagen 3 models generate only one image per request. However, you can ask an Imagen model to generate multiple images per request by providing an ImagenGenerationConfig when creating the ImagenModel instance.

Make sure to create an ImagenModel instance and call generateImages.

Swift


import FirebaseAI

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())


// Create an `ImagenModel` instance with a model that supports your use case
let model = ai.imagenModel(
  modelName: "imagen-3.0-generate-002",
  // Configure the model to generate multiple images for each request
  // See: https://firebase.google.com/docs/ai-logic/model-parameters
  generationConfig: ImagenGenerationConfig(numberOfImages: 4)
)

// Provide an image generation prompt
let prompt = "An astronaut riding a horse"

// To generate images, call `generateImages` with the text prompt
let response = try await model.generateImages(prompt: prompt)

// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if let filteredReason = response.filteredReason {
  print(filteredReason)
}

// Handle the generated images
let uiImages =  response.images.compactMap { UIImage(data: $0.data) }

Kotlin


// Using this SDK to access Imagen models is a Preview release and requires opt-in
@OptIn(PublicPreviewAPI::class)
suspend fun generateImage() {
  // Initialize the Gemini Developer API backend service
  // Create an `ImagenModel` instance with an Imagen model that supports your use case
  val imagenModel = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel(
      modelName = "imagen-3.0-generate-002",
      // Configure the model to generate multiple images for each request
      // See: https://firebase.google.com/docs/ai-logic/model-parameters
      generationConfig = ImagenGenerationConfig(numberOfImages = 4)
  )

  // Provide an image generation prompt
  val prompt = "An astronaut riding a horse"

  // To generate images, call `generateImages` with the text prompt
  val imageResponse = imagenModel.generateImages(prompt)

  // If fewer images were generated than were requested,
  // then `filteredReason` will describe the reason they were filtered out
  if (imageResponse.filteredReason != null) {
    Log.d(TAG, "FilteredReason: ${imageResponse.filteredReason}")
  }

  for (image in imageResponse.images) {
    val bitmap = image.asBitmap()
    // Use the bitmap to display the image in your UI
  }
}

Java


// Configure the model to generate multiple images for each request
// See: https://firebase.google.com/docs/ai-logic/model-parameters
ImagenGenerationConfig imagenGenerationConfig = new ImagenGenerationConfig.Builder()
        .setNumberOfImages(4)
        .build();

// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModel imagenModel = FirebaseAI.getInstance(GenerativeBackend.googleAI()).imagenModel(
        /* modelName */ "imagen-3.0-generate-002",
        /* imageGenerationConfig */ imagenGenerationConfig);

ImagenModelFutures model = ImagenModelFutures.from(imagenModel);

// Provide an image generation prompt
String prompt = "An astronaut riding a horse";

// To generate images, call `generateImages` with the text prompt
Futures.addCallback(model.generateImages(prompt), new FutureCallback<ImagenGenerationResponse<ImagenInlineImage>>() {
    @Override
    public void onSuccess(ImagenGenerationResponse<ImagenInlineImage> result) {
        // If fewer images were generated than were requested,
        // then `filteredReason` will describe the reason they were filtered out
        if (result.getFilteredReason() != null){
            Log.d("TAG", "FilteredReason: " + result.getFilteredReason());
        }

        // Handle the generated images
        List<ImagenInlineImage> images = result.getImages();
        for (ImagenInlineImage image : images) {
            Bitmap bitmap = image.asBitmap();
            // Use the bitmap to display the image in your UI
        }
    }

    @Override
    public void onFailure(Throwable t) {
        // ...
    }
}, Executors.newSingleThreadExecutor());

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });


// Create an `ImagenModel` instance with an Imagen 3 model that supports your use case
const imagenModel = getImagenModel(
  ai,
  {
    model: "imagen-3.0-generate-002",
    // Configure the model to generate multiple images for each request
    // See: https://firebase.google.com/docs/ai-logic/model-parameters
    generationConfig: {
      numberOfImages: 4
    }
  }
);

// Provide an image generation prompt
const prompt = "An astronaut riding a horse.";

// To generate images, call `generateImages` with the text prompt
const response = await imagenModel.generateImages(prompt)

// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (response.filteredReason) {
  console.log(response.filteredReason);
}

if (response.images.length == 0) {
  throw new Error("No images in the response.")
}

const images = response.images[0];

Dart

import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);


// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
final model =
  FirebaseAI.googleAI().imagenModel(
    model: 'imagen-3.0-generate-002',
    // Configure the model to generate multiple images for each request
    // See: https://firebase.google.com/docs/ai-logic/model-parameters
    generationConfig: ImagenGenerationConfig(numberOfImages: 4),
);

// Provide an image generation prompt
const prompt = 'An astronaut riding a horse.';

// To generate images, call `generateImages` with the text prompt
final response = await model.generateImages(prompt);

// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
if (response.filteredReason != null) {
  print(response.filteredReason);
}

if (response.images.isNotEmpty) {
  final images = response.images;
  for(var image in images) {
  // Process the image
  }
} else {
  // Handle the case where no images were generated
  print('Error: No images were generated.');
}

Unity

Using Imagen is not yet supported for Unity, but check back soon!

Learn how to choose a model appropriate for your use case and app.



Supported features and requirements

The Imagen 3 models offer many features related to image generation. This section describes what's supported when using the models with Firebase AI Logic.

Supported capabilities and features

Firebase AI Logic supports these features of Imagen 3 models.

  • Generating people and faces (given that your Firebase project has approval from Google Cloud)

  • Generating text within generated images

  • Adding a watermark to generated images

  • Configuring image generation parameters, like number of generated images, aspect ratio, and watermarking

  • Configuring safety settings

Firebase AI Logic does not support these advanced features of Imagen 3 models.

Note that most of these features require being on an approved list of users even when using Imagen models server-side.

  • Image editing or manipulation features, which includes upscaling images

  • Including images in the request to the model (like for few-shot learning)

  • Verifying digital watermarks using the SDKs
    If you want to verify that an image has a watermark, you can upload the image into Vertex AI Studio using its Media tab.

  • Generating "live images" from text (MP4 generation)

  • Generating images using a predefined style

  • Setting the language of the input text

  • Enabling includeSafetyAttributes, which means that safetyAttributes.categories and safetyAttributes.scores cannot be returned

  • Disabling prompt enhancement (the enhancePrompt parameter), which means that an LLM-based prompt rewriting tool will always automatically add more detail to the provided prompt to deliver higher quality images that better reflect the prompt provided

  • Writing a generated image directly into Google Cloud Storage as part of the response from the model (the storageUri parameter). Instead, images are always returned as base64-encoded image bytes in the response.
    If you want to upload a generated image to Cloud Storage, you can use Cloud Storage for Firebase.

Specifications and limitations

Limits (per request) Imagen 3 Imagen 3 Fast
Max number of input tokens 480 tokens 480 tokens
Max number of output images 4 images 4 images
Supported output image resolutions (pixels)
  • 1024x1024 pixels (1:1 aspect ratio)
  • 896x1280 (3:4 aspect ratio)
  • 1280x896 (4:3 aspect ratio)
  • 768x1408 (9:16 aspect ratio)
  • 1408x768 (16:9 aspect ratio)
  • 1024x1024 pixels (1:1 aspect ratio)
  • 896x1280 (3:4 aspect ratio)
  • 1280x896 (4:3 aspect ratio)
  • 768x1408 (9:16 aspect ratio)
  • 1408x768 (16:9 aspect ratio)



What else can you do?

Learn how to control content generation

Learn more about the supported models

Learn about the models available for various use cases and their quotas and pricing.


Give feedback about your experience with Firebase AI Logic