Image Generation

Generate stunning images using the power of AI with G4F’s image generation capabilities.

Introduction

G4F provides a simple and intuitive way to generate images using different image generation providers. With just a few lines of code, you can create visually appealing images based on text prompts.

To unleash the full potential of image generation, ensure you have installed the necessary dependencies by running:

pip install g4f[image]

Supported Providers

G4F supports the following image generation providers:

Bing

Utilizes Microsoft’s Image Creator for image generation.

Google Gemini

Available for free accounts with IP addresses outside Europe.

OpenAI Chat with GPT-4

Accessible for users with a Plus subscription.

Generating Images

To generate images using G4F, follow these steps:

1

Import the necessary modules

from g4f.client import Client
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini

client = Client(
    image_provider=Gemini,
    api_key='api-key if needed'
    ...
)

When using the OpenaiChat provider, you need to provide an access token from a Plus user.

2

Set up the request for image creation

response = client.images.generate(
    model="dall-e-3",
    prompt="a white siamese cat",
    ...
)

Request Parameters

model
str
default: "g4f.models.default"

The model to use for image generation. Defaults to the default model.

prompt
text

The text prompt to use for image generation.

kwargs
json

Additional keyword arguments to pass to the image generation provider.

3

Get image links from the response

image_url = response.data[0].url
print(image url) 
4

Image Variations

response = client.images.create_variation(
    image=open("cat.jpg", "rb"),
    model="bing",
    ...
)

image_url = response.data[0].url

Request Parameters

image
file

The image to use for creating variations.

model
model
default: "ImageType"

The model to use for image generation. Defaults to the default model.

kwargs
json

Additional keyword arguments to pass to the image generation provider.

Experiment with different providers and prompts to generate a variety of images!

Example

Here’s an example of generating an image using the Bing provider:

from g4f.client import Client

client = Client()

response = client.images.generate(
    model="dall-e-3",
    prompt="a white siamese cat",
    ...
)

print(response.data[0].url)

Chat with images

1

Import the necessary modules

from g4f.client import Client
from g4f.Provider.GeminiPro import GeminiPro

client = Client(
  api_key="...",
  provider=GeminiPro
)

When using the GeminiPro provider, you need to provide an api-key.

1

Chat with your image

response = client.chat.completions.create(
    model="gemini-pro-vision",
    messages=[{"role": "user", "content": "What can you see on this image"}],
    image=open("image.png", "rb"))

print(response.choices[0].message.content)
user@mac-air:~$ python3 main.py
>>> I can see a bird flying in the tropical forest

Conclusion

G4F makes it easy to generate images using various providers. With just a few lines of code, you can create stunning visuals based on text prompts. Explore the different providers and unleash your creativity!

Remember to handle the generated images appropriately and respect copyright and usage rights.