all
Type something to search...
Automated Blog Image Generation with Gemini API (Free Tier)

Automated Blog Image Generation with Gemini API (Free Tier)

The Problem: 138 Images to Create

I needed featured images for every blog article. Manually creating each one would take hours. My options:

  1. Canva/Figma — Manual, ~15 minutes per image = 32+ hours
  2. Midjourney — $10-30/month, still need manual prompting
  3. Google Gemini Free Tier — Free, API-accessible, batchable

I chose option 3. Here’s how to do it yourself.


What You’ll Build

A Python script that:

  • Reads image prompts from a markdown file
  • Calls Google’s Gemini API (gemini-3.1-flash-image-preview)
  • Respects free tier rate limits (2 requests/minute)
  • Saves images automatically with correct filenames
  • Logs progress for resuming interrupted batches

Time saved: ~30 hours of manual work Cost: $0 (free tier)


Prerequisites

1. Google AI Studio Account

Go to Google AI Studio and sign in with your Google account.

2. Get Your API Key

  1. Click “Get API Key” in the top right
  2. Create a new project or use existing
  3. Copy your API key (starts with AIzaSy...)

3. Install Dependencies

pip install google-generativeai pathlib

Or create a virtual environment:

python -m venv .venv-images
source .venv-images/bin/activate
pip install google-generativeai

The Complete Script

Save this as scripts/generate-images-automated.py:

#!/usr/bin/env python3
"""
ctrlman.dev — Automated Image Generator via Gemini API

Uses Gemini API with Imagen to generate blog images.
Respects free tier limit: 2 requests/minute.

Usage:
    python generate-images-automated.py
    python generate-images-automated.py --resume-from 10
    python generate-images-automated.py --dry-run
"""

import subprocess
import time
import json
from pathlib import Path
from datetime import datetime
import google.generativeai as genai

# Configuration
API_KEY = "YOUR_API_KEY_HERE"  # Replace with your key
genai.configure(api_key=API_KEY)

OUTPUT_DIR = Path("/path/to/your/blog/public/images")
LOG_FILE = Path("/path/to/your/workspace/image-generation-log.json")

# Rate limiting (free tier: 2 requests/minute)
REQUESTS_PER_MINUTE = 2
DELAY_BETWEEN_REQUESTS = 60 / REQUESTS_PER_MINUTE  # 30 seconds

# Your prompts list
PROMPTS = [
    {
        "filename": "javascript-fundamentals.png",
        "prompt": "Flat blog illustration for JavaScript programming fundamentals..."
    },
    # Add more prompts here
]

def generate_image(prompt_text, filename):
    """Generate a single image and save it."""
    try:
        model = genai.GenerativeModel('gemini-3.1-flash-image-preview')
        response = model.generate_content(
            prompt_text,
            generation_config={"response_modalities": ["IMAGE"]}
        )
        
        for part in response.parts:
            if part.inline_data:
                img_data = part.inline_data.data
                output_path = OUTPUT_DIR / filename
                with open(output_path, "wb") as f:
                    f.write(img_data)
                print(f"✓ Saved: {filename}")
                return True
        
        print(f"✗ No image data in response: {filename}")
        return False
        
    except Exception as e:
        print(f"✗ Error generating {filename}: {str(e)}")
        return False

def main():
    """Main batch generation loop."""
    print(f"Starting image generation at {datetime.now()}")
    print(f"Output directory: {OUTPUT_DIR}")
    print(f"Total images: {len(PROMPTS)}")
    print(f"Rate limit: {REQUESTS_PER_MINUTE} requests/minute")
    print("-" * 60)
    
    # Track progress
    generated = 0
    failed = 0
    start_time = time.time()
    
    for i, image_config in enumerate(PROMPTS, 1):
        filename = image_config["filename"]
        prompt = image_config["prompt"]
        
        # Skip if already exists (for resuming)
        if (OUTPUT_DIR / filename).exists():
            print(f"⊘ Skipping (exists): {filename}")
            continue
        
        print(f"[{i}/{len(PROMPTS)}] Generating: {filename}")
        
        success = generate_image(prompt, filename)
        
        if success:
            generated += 1
        else:
            failed += 1
        
        # Rate limiting
        if i < len(PROMPTS):
            print(f"   Waiting {DELAY_BETWEEN_REQUESTS}s (rate limit)...")
            time.sleep(DELAY_BETWEEN_REQUESTS)
    
    # Final report
    elapsed = time.time() - start_time
    print("-" * 60)
    print(f"Generation complete!")
    print(f"  Generated: {generated} images")
    print(f"  Failed: {failed} images")
    print(f"  Skipped: {len(PROMPTS) - generated - failed} images (already existed)")
    print(f"  Total time: {elapsed/60:.1f} minutes")
    print(f"  Average: {elapsed/len(PROMPTS):.1f}s per image")

if __name__ == "__main__":
    main()

Understanding the Free Tier Limits

Google Gemini Free Tier (as of March 2026)

LimitValue
Requests per minute2
Requests per day1,500
Images per request4 variations
Resolution1024x1024

Why Rate Limiting Matters

Without rate limiting, you’ll hit errors:

429 Too Many Requests
Resource has been exhausted (e.g. check quota)

My script enforces a 30-second delay between requests:

DELAY_BETWEEN_REQUESTS = 60 / REQUESTS_PER_MINUTE  # 30 seconds
time.sleep(DELAY_BETWEEN_REQUESTS)

Math:

  • 2 requests/minute × 60 minutes = 120 requests/hour
  • 120 requests/hour × 24 hours = 2,880 requests/day
  • But daily limit is 1,500, so you’re capped there

For 138 images:

  • 138 images ÷ 2 per minute = 69 minutes
  • Plus error buffer: ~90 minutes total

The Prompts: My Complete List

I created 138 prompts following this formula:

"Flat blog illustration for {TOPIC}, showing {VISUAL_ELEMENTS}, 
modern minimal vector style, limited color palette: 
#1e2b3a #ff4757 #2ed573 #f1f5f9 #0f172a #ffffff, 
no gradients, professional tech blog header, 1024x1024"

Example Prompts

PROMPTS = [
    {
        "filename": "javascript-fundamentals.png",
        "prompt": "Flat blog illustration for JavaScript programming fundamentals tutorial, showing JS shield logo with code blocks and programming icons, modern minimal vector art style, limited color palette: #1e2b3a #ff4757 #2ed573 #f1f5f9 #0f172a #ffffff, clean geometric shapes, no gradients, professional tech blog header, 1024x1024"
    },
    {
        "filename": "intro-to-react.png",
        "prompt": "Flat blog illustration for React.js beginner tutorial, showing component tree structure with nested boxes, modern minimal vector style, limited color palette, clean geometric shapes, no gradients, tech blog header, 1024x1024"
    },
    {
        "filename": "mysql-setup-guide.png",
        "prompt": "Flat blog illustration for MySQL database setup tutorial, showing database server icon with configuration gear, modern minimal vector style, ctrlman.dev brand colors, no gradients, professional tech blog header, 1024x1024"
    },
    # ... 135 more
]

Brand Colors Explained

My six-color palette (from GENERATE-IMAGES-WITH-GOOGLE.md):

Color NameHexUsage
Deep Gunmetal#1e2b3aPrimary text, outlines
Pomodoro Red#ff4757Alerts, important elements
Glacial Blue#2ed573Success states, highlights
Slate 100#f1f5f9Background
Slate 900#0f172aDark text
Pure White#ffffffContrast, space

Why this matters: Consistent colors = cohesive blog design.


Running the Script

Step 1: Test with One Image

# Modify script to only have 1 prompt
python scripts/generate-images-automated.py

Expected output:

Starting image generation at 2026-03-12 14:00:00
Output directory: /path/to/images
Total images: 1
Rate limit: 2 requests/minute
------------------------------------------------------------
[1/1] Generating: test-image.png
✓ Saved: test-image.png
------------------------------------------------------------
Generation complete!
  Generated: 1 images
  Failed: 0 images
  Total time: 0.5 minutes

Step 2: Run Full Batch

# Restore all 138 prompts
python scripts/generate-images-automated.py

Watch it run:

[1/138] Generating: javascript-fundamentals.png
✓ Saved: javascript-fundamentals.png
   Waiting 30.0s (rate limit)...
[2/138] Generating: javascript-fundamentals-2.png
✓ Saved: javascript-fundamentals-2.png
   Waiting 30.0s (rate limit)...
...

Step 3: Resume After Interruption

Script crashed? No problem. It skips existing files:

# Just run again - it auto-resumes
python scripts/generate-images-automated.py

Output:

⊘ Skipping (exists): javascript-fundamentals.png
⊘ Skipping (exists): javascript-fundamentals-2.png
[3/138] Generating: javascript-debugging.png
...

Troubleshooting

Error: 429 Too Many Requests

Cause: You hit the rate limit.

Fix: Increase delay:

DELAY_BETWEEN_REQUESTS = 60  # 60 seconds instead of 30

Error: API Key Not Valid

Cause: Wrong API key or not configured.

Fix:

# Option 1: Hardcode (for testing)
API_KEY = "AIzaSyB_nr8Uozr98sgdjL7xWARKmJFXAsUFcUE"

# Option 2: Environment variable (production)
import os
API_KEY = os.environ.get("GOOGLE_GENAI_API_KEY")

Error: No Image Data in Response

Cause: Prompt was rejected or model returned error.

Fix: Check prompt for:

  • Banned content (violence, copyrighted material)
  • Too vague descriptions
  • Missing style keywords

Good prompt:

"Flat blog illustration for JavaScript tutorial, showing JS logo with code icons, modern minimal vector style, #1e2b3a #ff4757 #2ed573 #f1f5f9 #0f172a #ffffff, no gradients, 1024x1024"

Bad prompt:

"Make a cool JavaScript picture"

Post-Processing: Optimization

Generated images are ~4.2MB PNGs. Too big for web!

Convert to WebP + Optimize

python scripts/batch-optimize-images.py \
  -i public/images-new \
  -o public/images \
  --colors 6 --format webp \
  --quality 85

Results:

StageSizeCountTotal
Raw PNG4.2 MB138579 MB
Optimized WebP80 KB13811 MB
Savings98%568 MB

Cost Breakdown

Free Tier (What I Used)

  • Monthly cost: $0
  • Daily limit: 1,500 requests
  • Speed: 2 requests/minute
  • Time for 138 images: ~90 minutes
  • Google AI Pro: ~$20/month
  • Higher limits: 10-20 requests/minute
  • Time for 138 images: ~10 minutes

My recommendation: Start with free tier. Upgrade only if you need speed.


Alternative: Manual Generation via Web UI

Don’t want to code? Use the web interface:

  1. Go to Gemini
  2. Paste prompt
  3. Click “Generate”
  4. Download best variation
  5. Repeat 137 times 😅

Time estimate: 15 minutes × 138 = 34.5 hours

Script time: 90 minutes (unattended)

Time saved: 33+ hours


What I Learned

✅ What Worked

  1. Free tier is sufficient — 2 requests/minute is slow but workable
  2. Consistent prompts = consistent results — Use a template
  3. Brand colors matter — Specify hex codes in every prompt
  4. Resume capability is essential — Network happens

❌ What Didn’t Work

  1. MCP Server setup — Too complex, switched to direct SDK
  2. No rate limiting — Got 429 errors immediately
  3. Vague prompts — Got photorealistic images instead of vector art

🎯 Key Insight

The difference between “good enough” and “perfect” is 30 hours of manual work. For a solo developer, automation isn’t lazy—it’s survival.


Your Turn

Quick Start Checklist

# 1. Get API key
https://aistudio.google.com/app/apikey

# 2. Install dependencies
pip install google-generativeai

# 3. Create script
# Copy the script above into generate-images-automated.py

# 4. Add your prompts
# Use the template formula

# 5. Run it
python generate-images-automated.py

# 6. Optimize results
python batch-optimize-images.py -i input -o output --format webp

Prompt Template (Copy-Paste Ready)

Flat blog illustration for {TOPIC}, showing {VISUAL_ELEMENTS}, 
modern minimal vector style, limited color palette: 
#1e2b3a #ff4757 #2ed573 #f1f5f9 #0f172a #ffffff, 
no gradients, professional tech blog header, 1024x1024

Resources


This article is part of the “My AI Journey” series—real lessons from building real projects with AI assistance. No theory, just what actually worked (and what spectacularly failed).

Comments

Log in to join the conversation

Loading comments...

Related Posts

Beyond No-Code: The Rise of AI-Assisted Application Creation

Beyond No-Code: The Rise of AI-Assisted Application Creation

Introduction: The Third Wave of Software Creation In the rapidly evolving landscape of software development, a new transformative approach has emerged, transcending the traditional barriers of coding…

Read more...
Spaghetti or Modular? How to Assess Your Code Quality in 5 Minutes

Spaghetti or Modular? How to Assess Your Code Quality in 5 Minutes

The Question That Started It All I've been developing trading bots for three months. One strategy is profitable. The rest? Not so much. Looking at my repository, I had a nagging question: Is my code…

Read more...
Code Rewritten: How AI Is Transforming Software Development

Code Rewritten: How AI Is Transforming Software Development

Introduction: The Day Everything Changed The software industry is on the brink of a revolution, driven by advances in artificial intelligence and large language models (LLMs). By examining historical…

Read more...
Building PurpleDeepCode: Your Open-Source AI-Powered Code Editor

Building PurpleDeepCode: Your Open-Source AI-Powered Code Editor

Building PurpleDeepCode: Your Open-Source AI-Powered Code Editor 1. Introduction In today’s fast-paced world of software development, AI-powered code editors like Cursor and PearAI have gained…

Read more...
Navigating the Clock: Productivity Philosophies for Developers

Navigating the Clock: Productivity Philosophies for Developers

Introduction: The Developer's Time Dilemma In the intricate dance of software development, productivity rhythms vary as wildly as the individuals coding the future. Some developers thrive on rigid…

Read more...
Running Local LLMs on a Budget Laptop: A Complete Guide for 2024

Running Local LLMs on a Budget Laptop: A Complete Guide for 2024

Running Local LLMs on a Budget Laptop: A Complete Guide Want to run AI locally without breaking the bank? Whether you're a developer, student, or curious tinkerer, running large language models on a…

Read more...
Evaluating Work and Payment Models in Developer Productivity

Evaluating Work and Payment Models in Developer Productivity

Introduction: The Hidden Productivity Killer While the core of a developer's productivity might often revolve around the adoption of time management techniques like the Pomodoro Technique, another…

Read more...
Why I Failed as an AI Pomodoro TODOer Web App Developer (And What I Learned)

Why I Failed as an AI Pomodoro TODOer Web App Developer (And What I Learned)

Introduction: The Failure I Didn't Expect In the world of tech startups, failure is often seen as a stepping stone to success. My journey as an AI Pomodoro TODOer web app developer was no exception. I…

Read more...
Innovation in the Age of AI and Entrepreneurship

Innovation in the Age of AI and Entrepreneurship

Introduction: Two Icons, One Transformation In the panorama of human creativity and innovation, two figures stand out for their contributions, albeit in starkly different ways: Nikola Tesla, the…

Read more...
Mastering Productivity: The Science Behind the Pomodoro Technique and Pomodoro TODOer

Mastering Productivity: The Science Behind the Pomodoro Technique and Pomodoro TODOer

Mastering Productivity with the Pomodoro Technique and Pomodoro TODOer In today's fast-paced world, managing time effectively is crucial. One method that has gained popularity for its simplicity and…

Read more...
The Necessity of Keeping Documentation Soup Repository Locally and Updated

The Necessity of Keeping Documentation Soup Repository Locally and Updated

Introduction: The Documentation Problem Every Developer Faces In today's fast-paced technological landscape, developers rely on a vast array of libraries and frameworks to build robust applications.…

Read more...
Budget-Friendly Power: Running Linux on Windows 11 Home Laptops

Budget-Friendly Power: Running Linux on Windows 11 Home Laptops

Running a Linux Environment on Your Budget Laptop: A Comprehensive Guide for Windows 11 Home Users Introduction As technology evolves, the boundaries between operating systems are blurring. For…

Read more...
Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers

Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers

Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers Introduction Playing basketball might seem like an unlikely activity for web developers, but this fast-paced…

Read more...
Mastering Modularization to Handle Spaghetti Code in Game Development

Mastering Modularization to Handle Spaghetti Code in Game Development

Mastering Modularization to Handle Spaghetti Code in Game Development Introduction In the realm of software development, especially in game development, effectively managing complexity is crucial. A…

Read more...
Boosting Productivity: The Taurine Advantage for Solopreneurs and Startup Founders

Boosting Productivity: The Taurine Advantage for Solopreneurs and Startup Founders

Boosting Productivity: The Taurine Advantage for Solopreneurs and Startup Founders Introduction As solopreneurs and startup founders, we're always looking for ways to stay focused, energized, and…

Read more...
Unlocking Peak Performance: The Mamba Mentality in the Workplace

Unlocking Peak Performance: The Mamba Mentality in the Workplace

Introduction: More Than a Catchphrase In the high-stakes world of professional sports, few legacies are as profound and inspiring as Kobe Bryant's "Mamba Mentality." This mindset, coined by the late…

Read more...
Mastering MySQL Integration in Modern Application Development

Mastering MySQL Integration in Modern Application Development

Mastering MySQL Integration in Modern Application Development Connecting to MySQL from Different Programming Languages Introduction In today's fast-paced world of application development, seamlessly…

Read more...

Related Posts

You may also enjoy these articles

Beyond No-Code: The Rise of AI-Assisted Application Creation

Beyond No-Code: The Rise of AI-Assisted Application Creation

Introduction: The Third Wave of Software Creation In the rapidly evolving landscape of software development, a new transformative approach has emerged, transcending the traditional barriers of coding…

Read more...
Spaghetti or Modular? How to Assess Your Code Quality in 5 Minutes

Spaghetti or Modular? How to Assess Your Code Quality in 5 Minutes

The Question That Started It All I've been developing trading bots for three months. One strategy is profitable. The rest? Not so much. Looking at my repository, I had a nagging question: Is my code…

Read more...
Code Rewritten: How AI Is Transforming Software Development

Code Rewritten: How AI Is Transforming Software Development

Introduction: The Day Everything Changed The software industry is on the brink of a revolution, driven by advances in artificial intelligence and large language models (LLMs). By examining historical…

Read more...
Building PurpleDeepCode: Your Open-Source AI-Powered Code Editor

Building PurpleDeepCode: Your Open-Source AI-Powered Code Editor

Building PurpleDeepCode: Your Open-Source AI-Powered Code Editor 1. Introduction In today’s fast-paced world of software development, AI-powered code editors like Cursor and PearAI have gained…

Read more...
Navigating the Clock: Productivity Philosophies for Developers

Navigating the Clock: Productivity Philosophies for Developers

Introduction: The Developer's Time Dilemma In the intricate dance of software development, productivity rhythms vary as wildly as the individuals coding the future. Some developers thrive on rigid…

Read more...
Running Local LLMs on a Budget Laptop: A Complete Guide for 2024

Running Local LLMs on a Budget Laptop: A Complete Guide for 2024

Running Local LLMs on a Budget Laptop: A Complete Guide Want to run AI locally without breaking the bank? Whether you're a developer, student, or curious tinkerer, running large language models on a…

Read more...
Evaluating Work and Payment Models in Developer Productivity

Evaluating Work and Payment Models in Developer Productivity

Introduction: The Hidden Productivity Killer While the core of a developer's productivity might often revolve around the adoption of time management techniques like the Pomodoro Technique, another…

Read more...
Why I Failed as an AI Pomodoro TODOer Web App Developer (And What I Learned)

Why I Failed as an AI Pomodoro TODOer Web App Developer (And What I Learned)

Introduction: The Failure I Didn't Expect In the world of tech startups, failure is often seen as a stepping stone to success. My journey as an AI Pomodoro TODOer web app developer was no exception. I…

Read more...
Innovation in the Age of AI and Entrepreneurship

Innovation in the Age of AI and Entrepreneurship

Introduction: Two Icons, One Transformation In the panorama of human creativity and innovation, two figures stand out for their contributions, albeit in starkly different ways: Nikola Tesla, the…

Read more...
Mastering Productivity: The Science Behind the Pomodoro Technique and Pomodoro TODOer

Mastering Productivity: The Science Behind the Pomodoro Technique and Pomodoro TODOer

Mastering Productivity with the Pomodoro Technique and Pomodoro TODOer In today's fast-paced world, managing time effectively is crucial. One method that has gained popularity for its simplicity and…

Read more...
The Necessity of Keeping Documentation Soup Repository Locally and Updated

The Necessity of Keeping Documentation Soup Repository Locally and Updated

Introduction: The Documentation Problem Every Developer Faces In today's fast-paced technological landscape, developers rely on a vast array of libraries and frameworks to build robust applications.…

Read more...
Budget-Friendly Power: Running Linux on Windows 11 Home Laptops

Budget-Friendly Power: Running Linux on Windows 11 Home Laptops

Running a Linux Environment on Your Budget Laptop: A Comprehensive Guide for Windows 11 Home Users Introduction As technology evolves, the boundaries between operating systems are blurring. For…

Read more...
Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers

Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers

Slam Dunk Your Productivity: How Playing Basketball Can Boost Efficiency for Web Developers Introduction Playing basketball might seem like an unlikely activity for web developers, but this fast-paced…

Read more...
Mastering Modularization to Handle Spaghetti Code in Game Development

Mastering Modularization to Handle Spaghetti Code in Game Development

Mastering Modularization to Handle Spaghetti Code in Game Development Introduction In the realm of software development, especially in game development, effectively managing complexity is crucial. A…

Read more...
Boosting Productivity: The Taurine Advantage for Solopreneurs and Startup Founders

Boosting Productivity: The Taurine Advantage for Solopreneurs and Startup Founders

Boosting Productivity: The Taurine Advantage for Solopreneurs and Startup Founders Introduction As solopreneurs and startup founders, we're always looking for ways to stay focused, energized, and…

Read more...
Unlocking Peak Performance: The Mamba Mentality in the Workplace

Unlocking Peak Performance: The Mamba Mentality in the Workplace

Introduction: More Than a Catchphrase In the high-stakes world of professional sports, few legacies are as profound and inspiring as Kobe Bryant's "Mamba Mentality." This mindset, coined by the late…

Read more...
Mastering MySQL Integration in Modern Application Development

Mastering MySQL Integration in Modern Application Development

Mastering MySQL Integration in Modern Application Development Connecting to MySQL from Different Programming Languages Introduction In today's fast-paced world of application development, seamlessly…

Read more...