GPT4Free Image Editing Variation Documentation

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



Overview

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

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


Providers and Models

Supported providers, input types, and notes
Provider Input Type Notes
PollinationsAI URL Uses flux-kontext model
Together URL Uses flux-kontext-pro model
HuggingSpace Local file Uses flux-kontext-dev model
Azure Local file Requires authentication
OpenaiAccount Local file Requires authentication
CopilotAccount Local file Requires 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 strawberry image
Image Variants generated by OpenAI and Together providers
OpenAI Variant Together Variant
OpenAI generated strawberry variant Together provider strawberry variant
Prompt: "Change to green" Prompt: "Generate a variant"
Image Variants generated by Pollinations.AI and Microsoft Copilot providers
Pollinations.AI Variant Microsoft Copilot Variant
Pollinations.AI strawberry variant Microsoft Copilot strawberry variant
Prompt: "Remove background" Prompt: "Add nature background"
Image Variants generated by Azure and HuggingSpace providers
Azure Variant HuggingSpace Variant
Azure strawberry variant with added text HuggingSpace strawberry variant in black and white
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("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("docs/images/strawberry.jpg"),
        provider=CopilotAccount,
        prompt="Generate a variant of this image",
        response_format="url"
    )
    print(result)

asyncio.run(main_with_openai())

Exmaples 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("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("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://argentinaservers.net/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://argentinaservers.net/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


Conclusion

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

Return to Documentation