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:
- 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 | 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 |
---|
![]() |
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" |
Azure Variant | HuggingSpace Variant |
---|---|
![]() |
![]() |
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
- File Not Found: Verify that the file paths exist and are correct.
- Authentication Errors: Ensure your provider credentials are correct and up to date.
- Provider Limits: Some providers enforce rate limits; monitor usage carefully.
- Model Compatibility: Not all models support all features; check model docs.
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