Read cookies and environment variables

August 7, 2025 ยท View on GitHub

GPT4Free Image Editing Variation Documentation

This guide explains how to use the image editing variation feature in GPT4Free (g4f.dev). It covers generating variations of images using different providers, handling local file uploads, and showcasing example results.


Table of Contents

  1. Overview
  2. Providers and Models
  3. Using Local File Uploads
  4. Example Results
  5. Code Examples
  6. Troubleshooting
  7. Conclusion

Overview

The g4f library allows you to generate variations of an input image based on a textual prompt. You can use either:

  • A URL to a remote image
  • A local file path
  • Uploaded files from your local server

The library supports multiple providers, including those that require local file access.


Providers and Models

ProviderInput TypeNotes
PollinationsAIURLUses flux-kontext model
TogetherURLUses flux-kontext-pro model
HuggingSpaceLocal fileUses flux-kontext-dev model
AzureLocal fileRequires authentication
OpenaiAccountLocal fileRequires authentication
CopilotAccountLocal fileRequires authentication

Using Local File Uploads

GPT4Free allows you to upload image files to your local server. These uploaded files can be used with all providers, including those that normally require local files.

JavaScript Upload Example:

async function upload_files(fileInput) {
    const bucket_id = generateUUID();
    
    const formData = new FormData();
    Array.from(fileInput.files).forEach(file => {
        formData.append('files', file);
    });
    
    const response = await fetch(
        `${framework.backendUrl}/backend-api/v2/files/${bucket_id}`, 
        {
            method: 'POST',
            body: formData
        }
    );
    
    const result = await response.json();
    if (result.media) {
        result.media.forEach((part) => {
            part = part.name ? part : {name: part};
            const url = `${framework.backendUrl}/files/${bucket_id}/media/${part.name}`;
            console.log("Uploaded media:", url);
        });
    }
}

Example Results

Original Image
Original
OpenAI VariantTogether Variant
OpenAITogether
Prompt: "Change to green"Prompt: "Generate a variant"
Pollinations.AI VariantMicrosoft Copilot Variant
Pollinations.AICopilot
Prompt: "Remove background"Prompt: "Add nature background"
Azure VariantHuggingSpace Variant
AzureHuggingSpace
Prompt: "Add text 'Hello World'"Prompt: "Change to black & white"

Code Examples

Basic Usage with Local File

import asyncio
from pathlib import Path
from g4f.client import AsyncClient
from g4f.Provider import OpenaiAccount, CopilotAccount

client = AsyncClient()

async def main_with_openai():
    result = await client.images.create_variation(
        image=Path("g4f.dev/docs/images/strawberry.jpg"),
        provider=OpenaiAccount,
        prompt="Change food color to green",
        response_format="url"
    )
    print(result)

async def main_with_copilot():
    result = await client.images.create_variation(
        image=Path("g4f.dev/docs/images/strawberry.jpg"),
        provider=CopilotAccount,
        prompt="Generate a variant of this image",
        response_format="url"
    )
    print(result)

asyncio.run(main_with_openai())

Examples with Azure and HuggingSpace provider

import asyncio
from pathlib import Path
from g4f.client import AsyncClient
from g4f.Provider import HuggingSpace, Azure
from g4f.cookies import read_cookie_files

# Read cookies and environment variables
read_cookie_files()

client = AsyncClient()

async def main_with_hugging_space():
    result = await client.images.create_variation(
        image=Path("g4f.dev/docs/images/strawberry.jpg"),
        provider=HuggingSpace,
        model="flux-kontext-dev",
        prompt="Change color to black and white",
        response_format="url"
    )
    print(result)

async def main_with_azure():
    result = await client.images.create_variation(
        image=Path("g4f.dev/docs/images/strawberry.jpg"),
        provider=Azure,
        model="flux-kontext",
        prompt="Add text 'Hello World' in the center",
        response_format="url"
    )
    print(result)

asyncio.run(main_with_azure())

URL-based Providers

import asyncio
from g4f.client import AsyncClient
from g4f.Provider import PollinationsAI, Together

client = AsyncClient()

async def main_pollinations():
    result = await client.images.create_variation(
        image="https://g4f.dev/docs/images/strawberry.jpg",
        provider=PollinationsAI,
        prompt="Remove background",
        model="kontext",
        response_format="url"
    )
    print(result)

async def main_with_together():
    result = await client.images.create_variation(
        image="https://g4f.dev/docs/images/strawberry.jpg",
        provider=Together,
        model="flux-kontext-pro",
        prompt="Add nature background",
        response_format="url"
    )
    print(result)

asyncio.run(main_with_together())

Troubleshooting

  • File Not Found: Verify file paths exist
  • Authentication Errors: Check provider credentials
  • Provider Limits: Some providers have rate limits
  • Model Compatibility: Not all models support all features

Conclusion

GPT4Free offers flexible image variation generation through multiple providers. Key features include:

  • Support for both local and remote images
  • Automatic handling of file uploads
  • Multiple provider options with different capabilities

Return to Documentation