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 AI 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 the following input types:

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


Providers and Models

List of AI providers supported by GPT4Free, input types, and special notes.
Provider Input Type Notes
PollinationsAI URL Supports transparent background with transparent=True.
Has gpt-image and flux-kontext models.
Together URL Uses flux-kontext-pro model.
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 used to generate variants.
Original image of strawberry used for variations
Image variants of strawberry created 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 generated strawberry variant Microsoft Copilot strawberry variant
Prompt: "Remove background" Prompt: "Add nature background"

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())

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="gpt-image",
        response_format="url",
        transparent=True
    )
    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


Conclusion

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

Return to Documentation