Generate images using Imagen


The Firebase AI Logic SDKs give you access to the Imagen 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

This guide describes how to generate images using Imagen by only providing a text prompt.

Note, though, that Imagen can also generate images based on a reference image using its customization capability (currently only for Android and Flutter). In the request, you provide a text prompt and a reference image that guides the model to generate a new image based on the specified style, subject (like a product, person, or animal), or a control. For example, you can generate a new image from a photo of a cat or a drawing of a rocket and the moon.

Jump to code for text-only input

Choosing between Gemini and Imagen models

The Firebase AI Logic SDKs support image generation and editing using either a Gemini model or an Imagen model.

For most use cases, start with Gemini, and then choose Imagen only for specialized tasks where image quality is critical.

Choose Gemini when you want:

  • To use world knowledge and reasoning to generate contextually relevant images.
  • To seamlessly blend text and images or to interleave text and image output.
  • To embed accurate visuals within long text sequences.
  • To edit images conversationally while maintaining context.

Choose Imagen when you want:

  • To prioritize image quality, photorealism, artistic detail, or specific styles (for example, impressionism or anime).
  • To infuse branding, style, or generation of logos and product designs.
  • To explicitly specify the aspect ratio or format of generated images.

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

The Gemini Developer API supports image generation by the latest stable Imagen models. This limitation of supported Imagen models is applicable regardless of how you access the Gemini Developer API.

  • imagen-4.0-generate-001
  • imagen-4.0-fast-generate-001
  • imagen-4.0-ultra-generate-001
  • imagen-3.0-generate-002

Generate images from text-only input

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

You can also set many different configuration options for image generation, like aspect ratio and image format.

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 only 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-4.0-generate-001") // 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  val ai = Firebase.ai(backend = GenerativeBackend.googleAI())  // Create an `ImagenModel` instance with an Imagen model that supports your use case  val model = ai.imagenModel("imagen-4.0-generate-001")  // Provide an image generation prompt  val prompt = "An astronaut riding a horse"  // To generate an image, call `generateImages` with the text prompt  val imageResponse = model.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-4.0-generate-001"); 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 model that supports your use case const model = getImagenModel(ai, { model: "imagen-4.0-generate-001" }); // 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 model.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 final model = FirebaseAI.googleAI(); // Create an `ImagenModel` instance with an Imagen model that supports your use case final model = ai.imagenModel(model: 'imagen-4.0-generate-001'); // 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 Firebase.AI; // Initialize the Gemini Developer API backend service var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()); // Create an `ImagenModel` instance with a model that supports your use case var model = ai.GetImagenModel(modelName: "imagen-4.0-generate-001"); // Provide an image generation prompt var prompt = "An astronaut riding a horse"; // To generate an image, call `generateImages` with the text prompt var response = await model.GenerateImagesAsync(prompt: prompt); // Handle the generated image if (response.Images.Count == 0) {  throw new Exception("No image in the response."); } var image = response.Images[0].AsTexture2D(); 

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 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-4.0-generate-001",  // 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  val ai = Firebase.ai(backend = GenerativeBackend.googleAI())  // Create an `ImagenModel` instance with an Imagen model that supports your use case  val model = ai.imagenModel(  modelName = "imagen-4.0-generate-001",  // 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 = model.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-4.0-generate-001",  /* 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 model that supports your use case const model = getImagenModel(  ai,  {  model: "imagen-4.0-generate-001",  // 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 model.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 final ai = FirebaseAI.googleAI(); // Create an `ImagenModel` instance with an Imagen model that supports your use case final model = ai.imagenModel(  model: 'imagen-4.0-generate-001',  // 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 Firebase.AI; // Initialize the Gemini Developer API backend service var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()); // Create an `ImagenModel` instance with a model that supports your use case var model = ai.GetImagenModel(  modelName: "imagen-4.0-generate-001",  // Configure the model to generate multiple images for each request  // See: https://firebase.google.com/docs/ai-logic/model-parameters  generationConfig: new ImagenGenerationConfig(numberOfImages: 4) ); // Provide an image generation prompt var prompt = "An astronaut riding a horse"; // To generate an image, call `generateImages` with the text prompt var response = await model.GenerateImagesAsync(prompt: prompt); // If fewer images were generated than were requested, // then `filteredReason` will describe the reason they were filtered out if (!string.IsNullOrEmpty(response.FilteredReason)) {  UnityEngine.Debug.Log("Filtered reason: " + response.FilteredReason); } // Handle the generated images var images = response.Images.Select(image => image.AsTexture2D()); 

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



Supported features and requirements

The Imagen 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 models:

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

  • Setting the language of the input text

  • Disabling prompt rewriter (the enhancePrompt parameter). This 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

Property (per request) Value
Max number of input tokens 480 tokens
Max number of output 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)



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