Site icon TestingDocs.com

OpenAI Image Generation API

OpenAI Image Generation API

In this tutorial, we will learn the OpenAI image generation API. The API picks the DALL.E model to generate an image from the user prompt. (The textual description of the image that needs to be generated by the model.)

DALL.E is a GAN and works with two networks:

DALL.E has many advantages. There are listed here.

Pre-requisites

Things you need for this tutorial:

Example

Creating an image using OpenAI API. We need to issue a POST call on the following endpoint with the request. The API Endpoint is:

HTTP POST on https://api.openai.com/v1/images/generations

Launch the Python-supported IDE. Create a file with a .py file extension to add the program and the following code.

Sample Code

# import
from openai import OpenAI
client = OpenAI()

# invoke the Image API
response = client.images.generate(
    model=”dall-e-3″,
    prompt=”a computer in the 90s”,
    size=”1024×1024″,
    quality=”standard”,
n=1,
)

image_url = response.data[0].url

print(image_url)

Output

DALL.E AI model generated image output.

 

Run the code in the IDE. The user prompt is to generate an image of a computer in the 1990s. The other parameters in the API request are the size and quality of the image. In this example, we have requested for standard quality image of size: 1024×1024

The API response is a JSON that will contain the following:

revised prompt, i.e., coherent image description by the model
generated image url.

OpenAI API Tutorials

OpenAI tutorials on this website can be found at:

Exit mobile version