Skip to main content

Overview

Nano Banana Image Edit is based on Google’s Gemini image generation model, with two available versions:
  • Formal Release: gemini-2.5-flash-image (Recommended, supports 10 aspect ratios and custom resolution)
  • Preview Version: gemini-2.5-flash-image-preview (Still available)
Both models support image generation, editing, composition, modification, and more, with identical pricing and calling methods.
October 3, 2025 Update: Google released the formal version gemini-2.5-flash-image, adding 10 aspect ratio support and custom resolution functionality. Recommended to use the formal version for the best experience, while the old version remains available.

📚 Latest Documentation

Feishu Complete Usage Guide - Updated fastest, supports comment interactionVisit Feishu documentation for the latest usage tutorials, tips sharing, and Q&A. Documentation is continuously updated, and you can comment directly on Feishu for discussions.

Editing Features
  • 🖼️ Multi-Image Composition: Supports compositing multiple images into one
  • ✏️ Text Instruction Editing: Describe editing needs through natural language
  • 🔄 Image Modification: Add, remove, or modify elements in images
  • 🎨 Style Conversion: Change image style, color, effects
  • Fast Processing: Complete editing tasks in an average of 10 seconds

Editing Capabilities

Supported Edit Types

  • Image Composition: Merge multiple images into one
  • Element Addition: Add new objects or elements to images
  • Style Modification: Change artistic style or color tone of images
  • Background Replacement: Replace image backgrounds
  • Size Adjustment: Change image ratio or dimensions
  • Detail Optimization: Enhance image quality or fix defects

API Usage

Basic Structure

Uses chat completion endpoint with multimodal input support (text + images):
POST /v1/chat/completions

Request Format

{
  "model": "gemini-2.5-flash-image",
  "stream": false,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Edit instruction text"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "Image URL 1"
          }
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "Image URL 2"
          }
        }
      ]
    }
  ]
}

Complete Python Example

#!/usr/bin/env python3
import requests
import json
import base64
import re
from datetime import datetime
import sys

# Configuration
API_KEY = "sk-"  # Replace with your actual key
API_URL = "https://api.apiyi.com/v1/chat/completions"

def edit_images():
    """
    Main image editing function
    Supports multi-image composition and editing
    """
    # Set request headers
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    # Request data - multi-image composition example
    data = {
        "model": "gemini-2.5-flash-image",
        "stream": False,
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Combine 2 images and add a Corgi dog image"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://github.com/dianping/cat/raw/master/cat-home/src/main/webapp/images/logo/cat_logo03.png"
                        }
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://raw.githubusercontent.com/leonindy/camel/master/camel-admin/src/main/webapp/assets/images/camel_logo_blue.png"
                        }
                    }
                ]
            }
        ]
    }

    print("🚀 Requesting image edit API...")
    print(f"📝 Edit instruction: {data['messages'][0]['content'][0]['text']}")
    print(f"🖼️  Input image count: {len([c for c in data['messages'][0]['content'] if c['type'] == 'image_url'])}")

    try:
        # Send request
        response = requests.post(API_URL, headers=headers, json=data)
        response.raise_for_status()

        print("✅ API request successful, processing response...")

        # Parse response
        result = response.json()

        # Extract content
        content = result['choices'][0]['message']['content']
        print(f"📄 Received content length: {len(content)} characters")
        print(f"📄 Content preview: {content[:200]}...")

        # Find Base64 image data
        base64_data = None

        # Method 1: Find standard format data:image/type;base64,data
        base64_match = re.search(r'data:image/[^;]+;base64,([A-Za-z0-9+/=]+)', content)

        if base64_match:
            base64_data = base64_match.group(1)
            print("✅ Found standard format Base64 data")
        else:
            # Method 2: Find pure Base64 data (long string)
            base64_match = re.search(r'([A-Za-z0-9+/=]{100,})', content)
            if base64_match:
                base64_data = base64_match.group(1)
                print("✅ Found pure Base64 data")
            else:
                print("❌ Error: Cannot find Base64 image data")
                print("📄 Full response content:")
                print(json.dumps(result, indent=2, ensure_ascii=False))
                return False

        # Generate filename
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"edited_image_{timestamp}.png"

        print("💾 Saving edited image...")

        # Decode and save image
        try:
            image_data = base64.b64decode(base64_data)
            with open(filename, 'wb') as f:
                f.write(image_data)

            print(f"🎉 Image editing complete!")
            print(f"📁 File saved as: {filename}")
            print(f"📊 File size: {len(image_data):,} bytes")
            return True

        except Exception as e:
            print(f"❌ Error: Problem saving image: {e}")
            return False

    except requests.exceptions.RequestException as e:
        print(f"❌ Error: API request failed: {e}")
        return False
    except KeyError as e:
        print(f"❌ Error: Response format incorrect, missing field: {e}")
        if 'response' in locals():
            print("📄 Full response content:")
            print(json.dumps(response.json(), indent=2, ensure_ascii=False))
        return False
    except Exception as e:
        print(f"❌ Error: Unknown error: {e}")
        return False

def custom_edit_example():
    """
    Custom edit example
    """
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    # Custom edit task
    data = {
        "model": "gemini-2.5-flash-image",
        "stream": False,
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Please remove the background and make it transparent, then add a sunset background"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "your_image_url_here"  # Replace with your image URL
                        }
                    }
                ]
            }
        ]
    }

    print("🎨 Executing custom edit task...")
    # Call edit logic...

def main():
    """
    Main function - Demonstrates different editing features
    """
    print("=" * 60)
    print("Nano Banana Image Editor - Python Version")
    print("=" * 60)
    print(f"🕐 Start time: {datetime.now()}")

    # Check API Key
    if API_KEY == "sk-":
        print("⚠️  Please set the correct API key first")
        return

    print("\n🔧 Available editing features:")
    print("1. Multi-image composition")
    print("2. Background replacement")
    print("3. Element addition")
    print("4. Style conversion")

    print("\n🚀 Executing multi-image composition example...")
    success = edit_images()

    print("\n" + "=" * 60)
    if success:
        print("🎉 Edit task executed successfully!")
        print("✅ Image saved locally")
    else:
        print("❌ Edit task execution failed!")
        print("💡 Please check API key and network connection")

    print(f"🕐 End time: {datetime.now()}")
    print("=" * 60)

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)

Edit Scenario Examples

1. Multi-Image Composition

# Combine two logos and add new elements
{
  "type": "text",
  "text": "Combine these two logos and add a cute animal"
}

2. Background Replacement

# Replace image background
{
  "type": "text",
  "text": "Remove the background and add a sunset landscape"
}

3. Style Conversion

# Change image style
{
  "type": "text",
  "text": "Convert this photo to anime/cartoon style"
}

4. Element Modification

# Add or remove elements
{
  "type": "text",
  "text": "Add sunglasses to the person and change hair color to blue"
}

Best Practices

Editing Instruction Optimization

Clear and Specific

Use specific, explicit descriptions✅ “Change background to blue ocean scene” ❌ “Change background”

Step-by-Step Description

Break down complex edits into steps✅ “First remove background, then add forest scene, finally add sunlight effect”

Image Input Recommendations

  • Format Support: PNG, JPEG, GIF, and other mainstream formats
  • Resolution: Recommended not to exceed 4096x4096
  • File Size: Single image recommended not to exceed 10MB
  • URL Requirements: Ensure image URLs are publicly accessible

Editing Quality Optimization

  1. High-Quality Input: Use clear, high-resolution original images
  2. Reasonable Instructions: Avoid overly complex or contradictory editing requirements
  3. Multiple Iterations: Complex edits can be done in multiple steps
  4. Reference Examples: Refer to successful editing cases

Error Handling

Common Errors and Solutions

Cause: Image URL is inaccessible or requires authenticationSolution:
  • Use publicly accessible image URLs
  • Check if URL is correct
  • Ensure image server allows external access
Cause: Edit instructions not clear enough or input image quality is lowSolution:
  • Use more specific, explicit edit instructions
  • Provide high-quality input images
  • Break down complex edits into steps
Cause: Edit task failed or response format is abnormalSolution:
  • Check if edit instructions are reasonable
  • Confirm input images are valid
  • View complete API response content

Pricing Information

  • Per-Request Billing: $0.025/edit task
  • Official Comparison: Official pricing $0.04/request
  • Actual Cost: Combined with top-up discounts, approximately ¥0.14/request
  • Savings: Save approximately 50% compared to official pricing

Advanced Features

Batch Editing

def batch_edit_images(image_urls, edit_instruction):
    """Batch edit multiple images"""
    results = []
    for i, url in enumerate(image_urls):
        data = {
            "model": "gemini-2.5-flash-image",
            "messages": [{
                "role": "user",
                "content": [
                    {"type": "text", "text": edit_instruction},
                    {"type": "image_url", "image_url": {"url": url}}
                ]
            }]
        }
        # Execute edit...
        results.append(result)
    return results

Edit History Tracking

def save_edit_history(original_url, instruction, result_path):
    """Save edit history record"""
    history = {
        "timestamp": datetime.now().isoformat(),
        "original_image": original_url,
        "edit_instruction": instruction,
        "result_path": result_path
    }
    # Save to file or database...
Editing Tip: For complex editing needs, it’s recommended to test effects with simple instructions first, then gradually increase complexity. This approach helps achieve better editing results.