Home » Ai Tools » How to Use DALL·E 2 API to Generate Images

How to Use DALL·E 2 API to Generate Images


Andrew NG

| Updated on:

DALL·E 2, an AI tool developed by OpenAI, offers a website where users can create images using prompts. However, many people are unaware that OpenAI also provides an API for DALL·E 2. This API enables users to integrate DALL·E 2 into their own applications. This article will guide you on accessing the DALL·E 2 API and using it to generate images with DALL·E models.

Is DALL·E 2 API Available?

DALL·E 2 indeed offers an API that allows developers to seamlessly incorporate its capabilities into their applications and products. The API was released as a public beta on November 3, 2022, following a preview phase with trusted users. It is built on OpenAI’s platform, which also powers other AI services like GPT-3, Codex, and CLIP.

The DALL·E 2 API provides three primary methods for creating and manipulating images: Generations, Edits, and Variations. These methods enable developers to generate new images based on text prompts, modify images using masks, and create variations based on existing images.

The API supports image generation with sizes up to 1024×1024 pixels, and developers can specify the desired number of images to generate per prompt. OpenAI is committed to advancing the API’s capabilities by continuously improving image quality, reducing latency, enhancing scalability, and ensuring user-friendly functionality.

Cost of DALL·E 2 API

Starting from November 1st, the DALL·E 2 API became a paid service. Each generated 1024×1024 image using the API costs $0.02. The price for 512×512 images is not specified by OpenAI.

The DALL·E API operates separately from the OpenAI website and follows a pay-as-you-go model. Users can try it using the free trial credits ($5) provided by the OpenAI API. It’s important to note that there are usage limits for the API, which can be found in the billing settings. By default, paid accounts have a monthly quota of $120, but users can request an increase in quota if needed.

For the most accurate and up-to-date pricing information, it is recommended to refer to OpenAI’s pricing page. If you have high-volume usage and want to explore potential discounts, you can also reach out to OpenAI’s sales team.

Accessing the DALL·E 2 API

Accessing the DALL·E 2 API involves following a series of formal steps.

Step 1: Firstly, developers need to register for an OpenAI account on the official OpenAI website. This registration process is typically free of charge. Once the account is successfully created, you can proceed to log in to your OpenAI account.

Step 2: To obtain an API key, developers should navigate to their account settings, accessible by selecting their account name located in the top right corner of the screen. From the drop-down menu, make sure to choose the “API Keys” option. 

Step 3: On the “API Keys” page, developers will find the option to generate a new secret key. Selecting this option will initiate the creation of an API key.

It is crucial to exercise caution and securely store the API key since it will not be displayed again. It is recommended to copy the key and store it in a secure location. Alternatively, developers can keep the API Keys page open temporarily for easy reference. The API key serves as a means of authentication for API requests.

How to Use DALL·E 2 API to Generate Images?

OpenAI now provides access to DALL·E 2 through their API, enabling the integration of its functionality into Python applications. To generate images using the DALL·E 2 API in Python, follow these steps:

Environment setup:

  • Make sure you have the latest version of Python installed.
  • Create a new project folder and navigate to it in the command line.

Package installation:

  • Use the ‘pip’ command to install the required packages: ‘openai’, ‘python-dotenv’, and ‘pillow’.
  • Run the following command in your terminal: ‘pip install openai python-dotenv pillow’.

Code setup:

  • Import the necessary modules and libraries: ‘openai’, ‘os’, ‘requests’, ‘dotenv’, and ‘Image’ from ‘PIL’.
  • Load the environment variables using ‘dotenv’.
  • Create a class called ‘ImageGenerator’ to encapsulate the functionality.

Image generation:

  • Within the ‘ImageGenerator’ class, define a method called ‘generateImage’ that takes ‘Prompt’, ‘ImageCount’, and ‘ImageSize’ as parameters.
  • Initialize the OpenAI API key using ‘os.getenv(“OPENAI_API_KEY”)’.
  • Make an API call to the image generation endpoint using the ‘openai.Image.create()’ method, passing the prompt, image count, and size as arguments.
  • Retrieve the generated image URLs from the API response and store them in a list.
  • Return the list of image URLs.

Image downloading:

  • Define a method called ‘downloadImage’ within the ‘ImageGenerator’ class that takes a list of names as input.
  • Iterate over each image URL and use the ‘requests’ library to download the corresponding image.
  • Save each downloaded image with the provided name in PNG format.

Code utilization:

  • Instantiate the ‘ImageGenerator’ class.
  • Call the ‘generateImage’ method with the desired prompt, image count, and size.
  • Call the ‘downloadImage’ method with a list of names for the downloaded images.

Image editing:

  • Expand the ‘ImageGenerator’ class by adding a method called ‘editImage’.
  • Inside the ‘editImage’ method, use the ‘convert’ method from the ‘Image’ module to convert a mask image to the RGBA format.
  • Make an API call to the editing endpoint using the ‘openai.Image.create_edit()’ method, passing the image, mask, prompt, image count, and size as arguments.
  • Retrieve the edited image URLs from the API response and store them in a list.
  • Return the list of edited image URLs.

Generating image variations:

  • Further expand the ‘ImageGenerator’ class by adding a method called ‘imageVariations’.
  • Use the ‘openai.Image.create_variation()’ method to generate alternative images from an existing image.
  • Pass the image, variation count, and size as arguments to the API call.
  • Retrieve the generated image variation URLs from the API response and store them in a list.
  • Return the list of image variation URLs.

By following these steps and utilizing the provided code, you can use the DALL·E 2 API to generate, edit, and create variations of images using natural language prompts in Python. Remember to handle any errors or exceptions that may occur during the process.

For a more detailed guide on using the DALL·E 2 API, you can refer to the provided link. Enjoy exploring the possibilities of image generation with the DALL·E 2 API!

Leave a Comment