Skip to content

Text-to-Image

Introduction

OpenAI DALL-E is a powerful image generation model that uses a combination of deep learning and generative adversarial networks (GANs) to create stunning and realistic images. It is trained on a large dataset of diverse images and can generate new images based on given prompts or concepts.

With DALL-E, you can unleash your creativity and explore the possibilities of image generation. Whether you want to create unique artwork, design new products, or simply have fun experimenting with visuals, DALL-E provides a versatile and intuitive platform.

In this guide, we will explore the fundamentals of DALL-E and learn how to generate images using this cutting-edge technology. We will cover the basic concepts, the process of generating images, and provide examples to help you get started on your own image generation journey.

Let's dive in and discover the fascinating world of OpenAI DALL-E image generation!

Best Practices

When you want to use api to generate images, you can use the following code to generate images for your text data:

curl --request POST \
  --url https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyE/images/generations \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'content-type: application/json' \
  --data '{
    "prompt": "A cute baby sea otter",
    "n": 1,
    "quality":"hd",
    "size": "1024x1024" 
}'

The parameters you can specify in the request are:

  • prompt: The input text or concept for which you want to generate images.
  • n(Optional): The number of images to generate, default value is 1. For dall-e-3, only n=1 is supported.
  • quality(Optional): The quality of the generated images (e.g., "standard" or "hd"). Thi is an optional parameter, the default value is "standard". "hd" is only supported for dall-e-3.
  • response_format(Optional): The format of the response (e.g., "url" or "b64_json"). This is an optional parameter, the default value is "url".
  • size(Optional): The size of the generated images (e.g., "256x256", "512x512", "1024x1024" for dall-e-2, "1024x1024", "1792x1024", "1024x1792" for dall-e-3).
  • style(Optional): The style of the generated images (e.g., "vivid" or "natural"). This is an optional parameter, the default value is "vivid". This parameter is only for dall-e-3
  • user(Optional): The user id of the user who is making the request.

After sending the request, you will receive a response containing the generated images based on the input text or concept. You can then use these images for various creative projects, design tasks, or visual exploration.

Seamless Integration

You can also integrate the image generation process into your applications using the Python OpenAI library. Here is an example code snippet:

from openai import OpenAI

client = OpenAI(
    base_url="https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyE/images/generations",
    api_key='YOUR_API_KEY'
)

response = client.images.generate(
    prompt="generate a superman.",
    n=1
)

print(response)

The url response will be:

{
    "created": 1632345678,
    "data": [
        {
            "url": "https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN/images/1" // maybe the native url of OpenAI or Azure OpenAI
        }
    ]
}

The url will the native url of OpenAI or Azure OpenAI, so you shold confirm that you can access the url in your network environment.

If you want to use bson format, you can set the response_format parameter to "b64_json" in the request.

from openai import OpenAI
import base64
from PIL import Image
from io import BytesIO

client = OpenAI(
    base_url="https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyE/images/generations",
    api_key='YOUR_API_KEY'
)

response = client.images.generate(
    prompt="generate a superman.",
    n=1,
    response_format="b64_json"
)

# print(response)
# Get b64_json data from response
b64_json_data = response.data[0].b64_json

# Convert b64_json data to png image
image_data = base64.b64decode(b64_json_data)
image = Image.open(BytesIO(image_data))

# Save the image as png
image.save('./image.png')

The image will saved as image.png in the current directory. But you should know that bs64_json format data will consume more data transfer than url format.