← Tech

Using Gemini Native API for Image Generation Instead of Third-Party Aggregators: Zero Cost

2026-03-14 AI Tools GeminiImagen图片生成google-genai

Problem

Third-party image generation platforms charge $0.02–0.05 per image. At scale, costs add up fast. The underlying models are Google's Imagen/Gemini — so why not call the native API directly?

Cost Comparison

| Method | Per image | 1,000 images/day |

|--------|-----------|-----------------|

| Third-party aggregator | $0.02–0.05 | $20–50 |

| Gemini native API (free tier) | $0 | $0 |

| Gemini native API (paid) | ~$0.004 | ~$4 |

Free tier limit: ~1,500 images/day (as of 2026-03, subject to change).

Implementation

Install the SDK:

pip install google-genai pillow

Generate and save an image:

import os

from google import genai

from google.genai import types

from PIL import Image

import io

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

response = client.models.generate_images(

model="imagen-4.0-generate-001",

prompt="A red lobster wearing slippers, cartoon style, white background",

config=types.GenerateImagesConfig(number_of_images=1)

)

image_bytes = response.generated_images[0].image.image_bytes

img = Image.open(io.BytesIO(image_bytes))

img.save("/tmp/output.png")

print(f"Saved: {img.size}, mode: {img.mode}")

Notes

No transparency channel: The returned image is in RGB mode. If you need a transparent background, you'll need post-processing:

import numpy as np

img_rgba = img.convert("RGBA")

data = np.array(img_rgba)

# Set near-white pixels to transparent

white_mask = (data[:,:,0] > 240) & (data[:,:,1] > 240) & (data[:,:,2] > 240)

data[white_mask, 3] = 0

result = Image.fromarray(data)

result.save("/tmp/output_transparent.png")

Model options:

API key: Get one at [aistudio.google.com](https://aistudio.google.com). Set as GEMINI_API_KEY environment variable.

When Not to Use

For standard product/illustration/icon generation, the native API is sufficient.