元渊 API元渊 API
Best Practices

Text-to-Image Example Code

Complete example code for generating images from text descriptions using Nano Banana Pro

Overview

This document provides complete example code for generating images from text using Nano Banana Pro (gemini-3-pro-image-preview), including both cURL and Python implementations.

Quick Test (cURL)

Quickly test text-to-image functionality with cURL:

curl -s -X POST "https://api.ai.soraliststudio.com/v1beta/models/gemini-3-pro-image-preview:generateContent" \
  -H "Authorization: Bearer sk-YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "parts": [
        {"text": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"}
      ]
    }]
  }' | python3 -c "import sys, json, base64; data=json.load(sys.stdin); img_data=data['candidates'][0]['content']['parts'][0]['inlineData']['data']; sys.stdout.buffer.write(base64.b64decode(img_data))" > gemini-native-image.png

This command will:

  1. Send an API request to generate an image
  2. Extract base64 image data from the response
  3. Decode and save as gemini-native-image.png

Complete Python Example

Features

  • ✅ Support custom text prompts
  • ✅ Support multiple aspect ratios (1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, 21:9, 5:4, 4:5)
  • ✅ Support three resolutions (1K, 2K, 4K)
  • ✅ Automatically add timestamps to filenames
  • ✅ Comprehensive error handling
  • ✅ Progress display and time statistics

Complete Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Gemini 3 Pro Image - Text-to-Image Example

Function: Generate images from text descriptions, supporting 1K/2K/4K resolutions
Model: gemini-3-pro-image-preview (Nano Banana Pro)
Price: Approximately $0.05 per image
"""

import requests
import base64
import time
from datetime import datetime

# ============================================================================
# Configuration - Modify your settings here
# ============================================================================

# 1. API Key (required) - Get from https://ai.soraliststudio.com
API_KEY = "sk-YOUR_API_KEY"

# 2. API Endpoint (no need to modify)
API_URL = "https://api.ai.soraliststudio.com/v1beta/models/gemini-3-pro-image-preview:generateContent"

# 3. Generation Configuration (modify as needed)
CONFIG = {
    "prompt": "A cute kitten sitting in a garden, oil painting style, high definition, rich in detail",
    "aspect_ratio": "16:9",      # Aspect ratio: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, 21:9, 5:4, 4:5
    "resolution": "2K",          # Resolution: 1K, 2K, 4K (2K recommended)
    "output_file": f"NanoBananaPro_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
}

# 4. Timeout (seconds) - Auto-select based on resolution
TIMEOUT = {
    "1K": 360,   # 6 minutes - Quick preview
    "2K": 600,   # 10 minutes - Recommended
    "4K": 1200,  # 20 minutes - Ultra HD
}

# ============================================================================
# Core Generation Function
# ============================================================================

def generate_image(prompt, aspect_ratio="1:1", resolution="2K"):
    """
    Core image generation function
    
    Parameters:
        prompt: Image description text, e.g., "a cute cat"
        aspect_ratio: Image aspect ratio, e.g., "1:1" (square), "16:9" (landscape)
        resolution: Image resolution, "1K" (fast), "2K" (recommended), "4K" (ultra HD)
    
    Returns:
        Success: {"success": True, "image_data": "base64 data"}
        Failure: {"success": False, "error": "error message"}
    """
    
    print(f"\n{'='*60}")
    print(f"🎨 Starting image generation")
    print(f"{'='*60}")
    print(f"📝 Prompt: {prompt}")
    print(f"📐 Aspect Ratio: {aspect_ratio}")
    print(f"🔍 Resolution: {resolution}")
    print(f"⏱️  Estimated Time: {TIMEOUT[resolution] // 60} minutes")
    
    # Build request parameters
    payload = {
        "contents": [
            {
                "parts": [
                    {"text": prompt}
                ]
            }
        ],
        "generationConfig": {
            "responseModalities": ["IMAGE"],
            "imageConfig": {
                "aspectRatio": aspect_ratio,
                "image_size": resolution
            }
        }
    }
    
    # Set request headers
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }
    
    # Send API request
    print(f"\n🚀 Requesting API...")
    start_time = time.time()
    
    try:
        response = requests.post(
            API_URL,
            headers=headers,
            json=payload,
            timeout=TIMEOUT[resolution]
        )
        
        elapsed = time.time() - start_time
        print(f"✅ Request completed, took {elapsed:.1f} seconds")
        
        # Parse response data
        if response.status_code == 200:
            data = response.json()
            
            try:
                parts = data["candidates"][0]["content"]["parts"]
                
                # Find image data
                for part in parts:
                    if "inlineData" in part:
                        image_base64 = part["inlineData"]["data"]
                        return {
                            "success": True,
                            "image_data": image_base64,
                            "elapsed_time": elapsed
                        }
                    elif "inline_data" in part:
                        image_base64 = part["inline_data"]["data"]
                        return {
                            "success": True,
                            "image_data": image_base64,
                            "elapsed_time": elapsed
                        }
                
                return {
                    "success": False,
                    "error": "No image data found in response",
                    "response": data
                }
                
            except (KeyError, IndexError) as e:
                return {
                    "success": False,
                    "error": f"Response data format error: {e}",
                    "response": data
                }
        else:
            return {
                "success": False,
                "error": f"HTTP {response.status_code}: {response.text}"
            }
    
    except requests.exceptions.Timeout:
        return {
            "success": False,
            "error": f"Request timeout (exceeded {TIMEOUT[resolution]} seconds)"
        }
    except Exception as e:
        return {
            "success": False,
            "error": f"Error occurred: {str(e)}"
        }

def save_image(image_data, output_file):
    """
    Save image to local file
    
    Parameters:
        image_data: base64 encoded image data
        output_file: output file path
    
    Returns:
        True on success, False on failure
    """
    try:
        print(f"\n💾 Saving image...")
        decoded_data = base64.b64decode(image_data)
        
        with open(output_file, 'wb') as f:
            f.write(decoded_data)
        
        file_size = len(decoded_data) / 1024  # KB
        print(f"✅ Image saved: {output_file}")
        print(f"📊 File size: {file_size:.2f} KB")
        
        return True
    except Exception as e:
        print(f"❌ Save failed: {str(e)}")
        return False

# ============================================================================
# Main Program
# ============================================================================

def main():
    """Main function"""
    print("="*60)
    print("Gemini 3 Pro Image - Text-to-Image")
    print("="*60)
    
    # Generate image
    result = generate_image(
        prompt=CONFIG["prompt"],
        aspect_ratio=CONFIG["aspect_ratio"],
        resolution=CONFIG["resolution"]
    )
    
    # Process result
    if result["success"]:
        # Save image
        if save_image(result["image_data"], CONFIG["output_file"]):
            print(f"\n{'='*60}")
            print(f"🎉 Generation successful!")
            print(f"⏱️  Total time: {result['elapsed_time']:.1f} seconds")
            print(f"{'='*60}\n")
        else:
            print(f"\n❌ Failed to save image")
    else:
        print(f"\n❌ Generation failed: {result['error']}")
        if "response" in result:
            print(f"Response data: {result['response']}")

if __name__ == "__main__":
    main()

Usage Instructions

1. Install Dependencies

pip install requests

2. Configure API Key

Replace API_KEY in the code with your actual API key:

API_KEY = "sk-YOUR_API_KEY"  # Replace with your key

3. Customize Configuration

Modify the CONFIG dictionary according to your needs:

CONFIG = {
    "prompt": "Your image description",     # Modify to your desired content
    "aspect_ratio": "16:9",                 # Choose appropriate aspect ratio
    "resolution": "2K",                     # Choose resolution level
    "output_file": "my_image.png"          # Custom output filename
}

4. Run Script

python3 text_to_image.py

Output Example

============================================================
Gemini 3 Pro Image - Text-to-Image
============================================================

============================================================
🎨 Starting image generation
============================================================
📝 Prompt: A cute kitten sitting in a garden, oil painting style, high definition, rich in detail
📐 Aspect Ratio: 16:9
🔍 Resolution: 2K
⏱️  Estimated Time: 10 minutes

🚀 Requesting API...
✅ Request completed, took 45.3 seconds

💾 Saving image...
✅ Image saved: NanoBananaPro_20260129_143052.png
📊 File size: 1234.56 KB

============================================================
🎉 Generation successful!
⏱️  Total time: 45.3 seconds
============================================================

FAQ

What to do if request times out?

If you encounter timeout errors, you can:

  1. Increase timeout in TIMEOUT configuration
  2. Lower resolution (e.g., from 4K to 2K)
  3. Check network connection

How to improve generation quality?

  1. Detailed prompts: Provide more detailed, specific descriptions
  2. Style keywords: Add art styles (e.g., "oil painting style", "realistic style")
  3. Quality keywords: Use words like "high definition", "rich in detail"
  4. Choose higher resolution: Use 2K or 4K resolution

Can't find image data in response?

This may be because:

  1. API refused to process the request (violating content)
  2. Response format changed
  3. Network transmission error

You can print the complete response data for debugging:

print(json.dumps(data, indent=2, ensure_ascii=False))

Price Reference

  • Approximately $0.05 per image
  • Prices may vary by resolution
  • Recommend checking latest pricing documentation

How is this guide?