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:

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.

3

Get image links from the response

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

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)
Generated Image

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.

2

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.