Skip to main content
This guide demonstrates how to use Gemini image models via CometAPI using the Google Gen AI SDK. It covers:
  • Text-to-image generation
  • Image-to-image editing
  • Multi-image composition
  • Saving generated images
  • Base URL: https://api.cometapi.com
  • Install the SDK: pip install google-genai (Python) or npm install @google/genai (Node.js)

Setup

Initialize the client with CometAPI’s base URL:

Text-to-image generation

Generate an image from a text prompt and save it to a file.
Save the final image part: The image data is in candidates[0].content.parts, which can contain text and/or image parts. Gemini image models can also return intermediate thought parts before the final image, especially when you request both text and images or explicitly enable thinking output. Do not save the first inlineData blindly; skip parts where thought is true, then save the last remaining image part. Typical response with only the final image:
Response with a text part, an intermediate thought image, and the final image:
Use this parsing rule for every Gemini image response:

Image-to-image generation

Upload an input image and transform it with a text prompt.
  • The Python SDK accepts PIL.Image objects directly — no manual Base64 encoding needed.
  • Do not include the data:image/jpeg;base64, prefix when passing raw Base64 strings.

Multi-image composition

Generate a new image from multiple input images. CometAPI supports two approaches:

Method 1: Single collage image

Combine multiple source images into one collage, then describe the desired output.
Input collage example
Generated output

Method 2: Multiple separate images (up to 14)

Pass multiple images directly. Gemini 3 models support up to 14 reference images (objects + characters):
Multi-image generation result

4K image generation

Specify image_config with aspect_ratio and image_size for high-resolution output:
For high-resolution requests, judge the output by the final non-thought image part. If your integration saves the first inlineData part, it may save an intermediate thought image that is lower resolution than the requested imageSize.

Multi-turn image editing (chat)

Use the SDK’s chat feature to iteratively refine images:

Tips

Specify style keywords (e.g., “cyberpunk, film grain, low contrast”), aspect ratio, subject, background, lighting, and detail level.
When using raw HTTP, do not include data:image/png;base64, prefix — use only the raw Base64 string. The Python SDK handles this automatically with PIL.Image objects.
Set "responseModalities" to ["IMAGE"] only to guarantee image output without text.
Check whether your code saved an intermediate thought image. Gemini image responses may include image parts where thought is true; these are not the final output. Skip thought: true parts and save the last image part where inlineData exists and thought is not true. If you do not need text output, request "responseModalities": ["IMAGE"] to reduce mixed text/image response handling.
For more details, see the API Reference. Official documentation: Nano Banana image generation