Introduction

In recent times, there has been a boom in the field of image generation, with models like Midjourney, Stable Diffusion, and DALL-E leading the charge. Gone are the days when people had to struggle to find high-quality, non-copyrighted images. Now, you can simply provide a description of the image you wish to generate through a prompt to a generative model and receive realistic, high-definition images in return. In this article, we will explore how you can easily create images using the OpenAI API.

Step 1: Create an OpenAI API Key

  • First, you need to create an OpenAI account to gain access to the creation of API keys.
  • Once you have created an OpenAI account, navigate to the “User API Keys” section by clicking on “Your Profile” on the OpenAI site.
Creation of OpenAI API keys

Create an API key and ensure you store it somewhere safe, as we will need it soon.

Step 2: Python Implementation

  • Firstly, the Python implementation of DALL-E requires the installation of the “requests” module. Navigate to your terminal and run the code below:

pip install requests

Next, let us look at the Python code you can use to generate images with DALL-E.

  • First, we need to make some basic imports to run the Python code.
import requests import json
  • Next, we need to paste our API key that we created in Step 1 and call the OpenAI endpoint.
api_key = 'YOUR_API_KEY' 
endpoint = 'https://api.openai.com/v1/images/generations'
  • We then define the HTTP headers required for making the request.
  • “Content-Type” indicates that the request body contains JSON data.
  • “Authorization” provides the API key for authentication using the Bearer token method.
headers = {
  'Content-Type': 'application/json',
  'Authorization': f'Bearer {api_key}'
  }
  • After declaring the headers, we move on to the most important part of the code.
  • “model” specifies the version of the DALL-E model to use for generating the image. In this case, ‘dall-e-3’ refers to the third version of the DALL-E model.
  • “prompt” is the textual description that the DALL-E model will use to generate the image. The model interprets the prompt and creates an image that matches the description. Here, the prompt is asking for an image of ‘a flying car.’
  • “n” specifies the number of images to generate. In this case, ‘1’ means that the API will return one generated image based on the provided prompt.
  • “size” defines the dimensions of the generated image in pixels. Here, ‘1024x1024’ means the image will be 1024 pixels wide and 1024 pixels high, resulting in a square image.
data = {
'model': 'dall-e-3',
'prompt': 'a flying car',
'n': 1,
'size': '1024x1024'
}
  • Finally, we send the request and receive a response with the generated image URL. You can further download the image to your desired location by specifying the path.
# Make the POST request
response = requests.post(endpoint, headers=headers, data=json.dumps(data))

# Check if the request was successful
if response.status_code == 200:
    # Print the response JSON
    response_json = response.json()
    image_url = response_json['data'][0]['url']
    print("Generated image URL:", image_url)
    # Download the image
    image_response = requests.get(image_url)
    if image_response.status_code == 200:
        # Save the image to a file
        local_image_path = 'YOUR_PATH'
        with open(local_image_path, 'wb') as file:
            file.write(image_response.content)
        print(f"Image downloaded and saved to {local_image_path}")
    else:
        print(f"Failed to download image. Status code: {image_response.status_code}")
else:
    print(f"Error: {response.status_code}")
    print(response.text)

Result

DALL-E 3 Image Generation Example

As you can see, the generated image is exemplary! We saw that through a few simple steps, we can create images using the OpenAI API call. Below is the entire code compiled in a single block:

import requests
import json

# Set up your API key and endpoint
api_key = 'YOUR_API_KEY'
endpoint = 'https://api.openai.com/v1/images/generations'
# Define the headers and data for the request
headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_key}'
}
data = {
    'model': 'dall-e-3',
    'prompt': 'a flying car',
    'n': 1,
    'size': '1024x1024'
}
# Make the POST request
response = requests.post(endpoint, headers=headers, data=json.dumps(data))
# Check if the request was successful
if response.status_code == 200:
    # Print the response JSON
    response_json = response.json()
    image_url = response_json['data'][0]['url']
    print("Generated image URL:", image_url)
    # Download the image
    image_response = requests.get(image_url)
    if image_response.status_code == 200:
        # Save the image to a file
        local_image_path = 'YOUR_PATH'
        with open(local_image_path, 'wb') as file:
            file.write(image_response.content)
        print(f"Image downloaded and saved to {local_image_path}")
    else:
        print(f"Failed to download image. Status code: {image_response.status_code}")
else:
    print(f"Error: {response.status_code}")
    print(response.text)

Conclusion

The advent of advanced image generation models like DALL-E 3 has revolutionised the way we create and interact with visual content. By simply providing a textual description, you can now generate high-quality, realistic images tailored to your needs. This capability opens up a world of possibilities for creative professionals, marketers, educators, and anyone needing unique visuals.

In this article, we saw the straightforward process of setting up an OpenAI API key, installing the necessary Python modules, and writing a script to generate and download images using DALL-E 3. With these tools at your disposal, you can effortlessly bring your imaginative ideas to life.

Whether you’re looking to enhance your presentations, create compelling marketing materials, or explore new creative avenues, DALL-E 3 provides an accessible and powerful solution. As technology continues to evolve, we can only anticipate even more remarkable advancements in the field of artificial intelligence and image generation.

References

  • Cappellini, Stefano. “How to Use ChatGPT and DALL-E3 for Image Generation: Complete Tutorial Guide for Text-Based Image….” _Medium_, 21 Apr. 2024, medium.com/@toptalenticalcio/how-to-use-chatgpt-and-dall-e3-for-image-generation-complete-tutorial-guide-for-text-based-image-9fb3c6fa8495. Accessed 31 July. 2024.
  • Gonsalves, Robert A. “Using AI to Create New Comic Strips without Writing Any Code.” _Medium_, 5 Sept. 2023, medium.com/towards-data-science/using-ai-to-create-new-comic-strips-without-writing-any-code-cc669bb317a7. Accessed 1 Aug. 2024.

Catch the latest version of this article over on Medium.com. Hit the button below to join our readers there.

Learn more on Medium