Generating Images with the Gemini API
The Gemini API gives developers programmatic access to Google's image generation models. Whether you're building a content tool, automating social media, or creating a product that needs AI-generated visuals, the Gemini API is one of the most capable and cost-effective options available.
Getting Started
Step 1: Get an API keyGo to aistudio.google.com → API keys → Create API key. It's free to start with generous rate limits.
Step 2: Install the SDKnpm install @google/generative-ai
Step 3: Generate your first image
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
async function generateImage(prompt) {
const model = genAI.getGenerativeModel({
model: "gemini-2.0-flash-exp-image-generation"
});
const response = await model.generateContent({
contents: [{
role: "user",
parts: [{ text: prompt }]
}],
generationConfig: {
responseModalities: ["image", "text"]
}
});
const imagePart = response.response.candidates[0]
.content.parts.find(p => p.inlineData);
return imagePart.inlineData; // { mimeType, data (base64) }
}
Available Models for Image Generation
Handling Watermarks Programmatically
All Gemini API image outputs contain the same watermark as the web interface. For applications that need clean images, you have two options:
Option 1: Use iLoveWatermark's tool manuallyFor low-volume use cases, process images through ilovewatermark.com/tool after generation.
Option 2: Implement reverse alpha blending in your backendconst Jimp = require('jimp');
async function removeGeminiWatermark(imageBuffer) {
const image = await Jimp.read(imageBuffer);
const w = image.bitmap.width;
const h = image.bitmap.height;
// Load appropriate alpha mask
const isLarge = w > 1024 && h > 1024;
const maskSize = isLarge ? 96 : 48;
const margin = isLarge ? 64 : 32;
const maskPath = isLarge ? './bg_96.png' : './bg_48.png';
const mask = await Jimp.read(maskPath);
// Watermark position
const wx = w - maskSize - margin;
const wy = h - maskSize - margin;
// Reverse alpha blending
for (let row = 0; row < maskSize; row++) {
for (let col = 0; col < maskSize; col++) {
const maskPixel = Jimp.intToRGBA(mask.getPixelColor(col, row));
const alpha = Math.max(maskPixel.r, maskPixel.g, maskPixel.b) / 255;
if (alpha < 0.002) continue;
const clampedAlpha = Math.min(alpha, 0.99);
const imgPixel = Jimp.intToRGBA(
image.getPixelColor(wx + col, wy + row)
);
const r = Math.max(0, Math.min(255,
Math.round((imgPixel.r - clampedAlpha * 255) / (1 - clampedAlpha))
));
const g = Math.max(0, Math.min(255,
Math.round((imgPixel.g - clampedAlpha * 255) / (1 - clampedAlpha))
));
const b = Math.max(0, Math.min(255,
Math.round((imgPixel.b - clampedAlpha * 255) / (1 - clampedAlpha))
));
image.setPixelColor(
Jimp.rgbaToInt(r, g, b, 255),
wx + col,
wy + row
);
}
}
return await image.getBufferAsync(Jimp.MIME_PNG);
}
You'll need the bg_48.png and bg_96.png alpha mask files. These are available from the open-source gemini-watermark-remover project on GitHub.
Rate Limits and Pricing
Gemini API pricing for image generation (2026):
- Free tier: 15 requests per minute, 1,500 per day
- Pay-as-you-go: varies by model
For most content automation use cases, the free tier is sufficient. For high-volume production use, the pay-as-you-go pricing is very competitive.
Best Practices for Production
Always specify output format: Request PNG for watermark removal compatibility Implement retry logic: API calls occasionally fail — wrap in try-catch with exponential backoff Cache generated images: Don't regenerate the same image twice — store results in your database or object storage Validate outputs: Check that the response contains image data before attempting watermark removal Async processing: For web apps, queue image generation as background jobs rather than blocking user requestsReady to remove your Gemini watermark?
Free tool — no signup required. First 2 images free.
Try iLoveWatermark Free →