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:
- 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
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
![]() |
OpenAI Variant | Together Variant |
---|---|
![]() |
![]() |
Prompt: "Change to green" | Prompt: "Generate a variant" |
Pollinations.AI Variant | Microsoft Copilot 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
- 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 AI providers. Key features include:
- Support for both local and remote images.
- Automatic handling of file uploads.
- Multiple provider options with different capabilities.