To avoid service disruption, update to a newer model (for example,
gemini-2.5-flash-lite
). Learn more.
Generate images using Imagen
Stay organized with collections
Save and categorize content based on your preferences.
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
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
importFirebaseAI
// Initialize the Gemini Developer API backend service
letai=FirebaseAI.firebaseAI(backend:.googleAI())
// Create an `ImagenModel` instance with a model that supports your use case
letmodel=ai.imagenModel(modelName:"imagen-4.0-generate-001")
// Provide an image generation prompt
letprompt="An astronaut riding a horse"
// To generate an image, call `generateImages` with the text prompt
letresponse=tryawaitmodel.generateImages(prompt:prompt)
// Handle the generated image
guardletimage=response.images.firstelse{
fatalError("No image in the response.")
}
letuiImage=UIImage(data:image.data)
Kotlin
suspendfungenerateImage(){
// Initialize the Gemini Developer API backend service
valai=Firebase.ai(backend=GenerativeBackend.googleAI())
// Create an `ImagenModel` instance with an Imagen model that supports your use case
valmodel=ai.imagenModel("imagen-4.0-generate-001")
// Provide an image generation prompt
valprompt="An astronaut riding a horse"
// To generate an image, call `generateImages` with the text prompt
valimageResponse=model.generateImages(prompt)
// Handle the generated image
valimage=imageResponse.images.first()
valbitmapImage=image.asBitmap()
}
Java
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModelimagenModel=FirebaseAI.getInstance(GenerativeBackend.googleAI())
.imagenModel(
/* modelName */"imagen-4.0-generate-001");
ImagenModelFuturesmodel=ImagenModelFutures.from(imagenModel);
// Provide an image generation prompt
Stringprompt="An astronaut riding a horse";
// To generate an image, call `generateImages` with the text prompt
Futures.addCallback(model.generateImages(prompt),newFutureCallback<ImagenGenerationResponse<ImagenInlineImage>>(){
@Override
publicvoidonSuccess(ImagenGenerationResponse<ImagenInlineImage>result){
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
}
@Override
publicvoidonFailure(Throwablet){
// ...
}
},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
constfirebaseConfig={
// ...
};
// Initialize FirebaseApp
constfirebaseApp=initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
constai=getAI(firebaseApp,{backend:newGoogleAIBackend()});
// Create an `ImagenModel` instance with an Imagen model that supports your use case
constmodel=getImagenModel(ai,{model:"imagen-4.0-generate-001"});
// Provide an image generation prompt
constprompt="An astronaut riding a horse.";
// To generate an image, call `generateImages` with the text prompt
constresponse=awaitmodel.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){
thrownewError("No images in the response.")
}
constimage=response.images[0];
Dart
import'package:firebase_ai/firebase_ai.dart';
import'package:firebase_core/firebase_core.dart';
import'firebase_options.dart';
// Initialize FirebaseApp
awaitFirebase.initializeApp(
options:DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
finalmodel=FirebaseAI.googleAI();
// Create an `ImagenModel` instance with an Imagen model that supports your use case
finalmodel=ai.imagenModel(model:'imagen-4.0-generate-001');
// Provide an image generation prompt
constprompt='An astronaut riding a horse.';
// To generate an image, call `generateImages` with the text prompt
finalresponse=awaitmodel.generateImages(prompt);
if(response.images.isNotEmpty){
finalimage=response.images[0];
// Process the image
}else{
// Handle the case where no images were generated
print('Error: No images were generated.');
}
Unity
usingFirebase.AI;
// Initialize the Gemini Developer API backend service
varai=FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create an `ImagenModel` instance with a model that supports your use case
varmodel=ai.GetImagenModel(modelName:"imagen-4.0-generate-001");
// Provide an image generation prompt
varprompt="An astronaut riding a horse";
// To generate an image, call `generateImages` with the text prompt
varresponse=awaitmodel.GenerateImagesAsync(prompt:prompt);
// Handle the generated image
if(response.Images.Count==0){
thrownewException("No image in the response.");
}
varimage=response.Images[0].AsTexture2D();
Learn how to choose a model appropriate for your use case and app.
Generate multiple images from text-only input
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
importFirebaseAI
// Initialize the Gemini Developer API backend service
letai=FirebaseAI.firebaseAI(backend:.googleAI())
// Create an `ImagenModel` instance with a model that supports your use case
letmodel=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
letprompt="An astronaut riding a horse"
// To generate images, call `generateImages` with the text prompt
letresponse=tryawaitmodel.generateImages(prompt:prompt)
// If fewer images were generated than were requested,
// then `filteredReason` will describe the reason they were filtered out
ifletfilteredReason=response.filteredReason{
print(filteredReason)
}
// Handle the generated images
letuiImages=response.images.compactMap{UIImage(data:0ドル.data)}
Kotlin
suspendfungenerateImage(){
// Initialize the Gemini Developer API backend service
valai=Firebase.ai(backend=GenerativeBackend.googleAI())
// Create an `ImagenModel` instance with an Imagen model that supports your use case
valmodel=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
valprompt="An astronaut riding a horse"
// To generate images, call `generateImages` with the text prompt
valimageResponse=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(imageinimageResponse.images){
valbitmap=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
ImagenGenerationConfigimagenGenerationConfig=newImagenGenerationConfig.Builder()
.setNumberOfImages(4)
.build();
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance with an Imagen model that supports your use case
ImagenModelimagenModel=FirebaseAI.getInstance(GenerativeBackend.googleAI())
.imagenModel(
/* modelName */"imagen-4.0-generate-001",
/* imageGenerationConfig */imagenGenerationConfig);
ImagenModelFuturesmodel=ImagenModelFutures.from(imagenModel);
// Provide an image generation prompt
Stringprompt="An astronaut riding a horse";
// To generate images, call `generateImages` with the text prompt
Futures.addCallback(model.generateImages(prompt),newFutureCallback<ImagenGenerationResponse<ImagenInlineImage>>(){
@Override
publicvoidonSuccess(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(ImagenInlineImageimage:images){
Bitmapbitmap=image.asBitmap();
// Use the bitmap to display the image in your UI
}
}
@Override
publicvoidonFailure(Throwablet){
// ...
}
},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
constfirebaseConfig={
// ...
};
// Initialize FirebaseApp
constfirebaseApp=initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
constai=getAI(firebaseApp,{backend:newGoogleAIBackend()});
// Create an `ImagenModel` instance with an Imagen model that supports your use case
constmodel=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
constprompt="An astronaut riding a horse.";
// To generate images, call `generateImages` with the text prompt
constresponse=awaitmodel.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){
thrownewError("No images in the response.")
}
constimages=response.images[0];
Dart
import'package:firebase_ai/firebase_ai.dart';
import'package:firebase_core/firebase_core.dart';
import'firebase_options.dart';
// Initialize FirebaseApp
awaitFirebase.initializeApp(
options:DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
finalai=FirebaseAI.googleAI();
// Create an `ImagenModel` instance with an Imagen model that supports your use case
finalmodel=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
constprompt='An astronaut riding a horse.';
// To generate images, call `generateImages` with the text prompt
finalresponse=awaitmodel.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){
finalimages=response.images;
for(varimageinimages){
// Process the image
}
}else{
// Handle the case where no images were generated
print('Error: No images were generated.');
}
Unity
usingFirebase.AI;
// Initialize the Gemini Developer API backend service
varai=FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create an `ImagenModel` instance with a model that supports your use case
varmodel=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:newImagenGenerationConfig(numberOfImages:4)
);
// Provide an image generation prompt
varprompt="An astronaut riding a horse";
// To generate an image, call `generateImages` with the text prompt
varresponse=awaitmodel.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
varimages=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:
Generating people, faces, and text within generated images
Editing images or including images in the request when using the Vertex AI Gemini API (currently only for Android and Flutter)
Adding a watermark to generated images
Verifying digital watermarks when using the Vertex AI Gemini API
If you want to verify that an image has a watermark, you can upload the image into Vertex AI Studio using its Media tab.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 models:
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) |
|
What else can you do?
-
Start thinking about preparing for production (see the
production checklist),
including:
- Setting up Firebase App Check to protect the Gemini API from abuse by unauthorized clients.
- Integrating Firebase Remote Config to update values in your app (like model name) without releasing a new app version.
Learn how to control content generation
- Understand prompt design, including best practices, strategies, and example prompts.
- Configure Imagen model parameters like aspect ratio, person generation, and watermarking.
- Use safety settings to adjust the likelihood of getting responses that may be considered harmful.
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