Notebooks
G
Google Gemini
Get Started Imagen

Get Started Imagen

quickstartsgemini-cookbookgemini-apigemini
Copyright 2025 Google LLC.
[ ]

Gemini API: Get started with image generation

The Imagen 4 family of models are Google's highest quality text-to-image models, featuring a number of new and improved capabilities. Imagen 4 can do the following:

  • Generate images with fine detail, rich lighting, and few distracting artifact
  • Understand prompts written in natural language
  • Generate images in a wide range of formats and styles
  • Render text effectively

This notebook is using the Python SDK. For the REST API, check out the Get Started with Imagen guide.

⚠️

Image generation is a paid-only feature and won't work if you are on the free tier. Check the pricing page for more details.

Setup

Install SDK

Install the SDK from PyPI.

[ ]
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.5/40.5 kB 1.6 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 226.1/226.1 kB 5.9 MB/s eta 0:00:00

Setup your API key

To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see Authentication image for an example.

[ ]

Initialize SDK client

With the new SDK you now only need to initialize a client with you API key (or OAuth if using Vertex AI). The model is now set in each call.

[ ]

Generate images

Select model

You can choose from a range of available Imagen models:

  • Imagen 4 (imagen-4.0-generate-001) is the new stadard model you should use to generate.
  • Imagen 4 Ultra (imagen-4.0-ultra-generate-001) is the best Imagen model, generating even finer images and is especially good at generating images with text. Note that it can only generate one image at a time.
  • Imagen 4 Fast (imagen-4.0-fast-generate-001) is a faster model and generates at a lower cost compared to Imagen 4 and Ultra. See the pricing page for details.
  • Imagen 3 (imagen-3.0-generate-002) is the previous generation model. It's still available in case you need to rerun old prompts, but it is recommended to use the 4th generation models now.
[ ]
MODEL_ID

Prompt creation

Now, write your prompt and set some optional parameters. The imagen-4.0-generate-001 model is trained on long captions and will provide better results for longer and more descriptive prompts. Note that if you use a short prompt, it may result in low adherence and more random output.

Check the prompt guide for more advice on creating your prompts.

Here are the parameters you can set relating to your prompt:

  • number_of_images: Specifies how many iamges will be generated. The default value is 4, with valid values between 1 to 4, inclusive. In the below code cell, sample_count is used to define this.
  • person_generation: Allows the model to generate images with adults. Kids are always blocked. The supported values are DONT_ALLOW and ALLOW_ADULT. The default value is ALLOW_ADULT.
  • aspect_ratio: Specifies the aspect ratio of the images produces. The supported values are 1:1, 3:4, 4:3, 16:9, and 9:16. The default value is 1:1.
  • image_size: The output resolution of the image, either 1k (default) or 2k, for supported models.
  • output_mime_type: The output type of your image, which will be image/jpeg. This is the only allowed value at the moment.

A non-visible digital SynthID watermark is always added to generated images.

[ ]
prompt
number_of_images
1
person_generation
aspect_ratio
image_size

Generate the images

[ ]

Display the images

Use the code below to inspect the images you generated.

[ ]
Output

Generate images with text

Imagen 3 and 4 models are good at generating images with text. Here's an example with a comic strip:

[ ]
prompt
number_of_images
1
person_generation
aspect_ratio
Output

Here's a second one. This time, the image saved in the notebook has been generated using Imagen 4 Ultra as there was quite a lot of text to process:

[ ]
prompt
number_of_images
1
person_generation
aspect_ratio
Output

Next Steps

Useful documentation references:

To improve your prompting skills, check the prompt guide for great advices on creating your prompts.

Check those cool Imagen examples:

Here are some Imagen examples to get your imagination started on how to use it in creative ways:

  • Illustrate a book: Use Gemini and Imagen to create illustration for an open-source book

Continue your discovery of the Gemini API

Gemini is not only good at generating images, but also at understanding them. Check the Spatial understanding guide for an introduction on those capabilities, and the Video understanding one for video examples.