How to generate image variations using DALL.E API

DALL·E, an AI model from OpenAI, possesses the extraordinary ability to craft images from text descriptions. Moreover, it is equipped with the capability to generate alternate versions of an existing image, providing a robust resource for creative innovation and design. In this Answer, we are going to provide a detailed guide on how to produce image variations using the DALL·E API in Python. The Answer covers every step from setting up the environment, preparing the image for input, executing the necessary code, and eventually storing the output.

Initial setup

The initial step requires you to have Python installed on your machine. Additionally, openai and requests libraries are necessary. You can get these installed using pip:

pip install openai requests

Subsequently, you need to configure your OpenAI API key. This key serves the purpose of authenticating your requests to the OpenAI API.

import os
import openai
openai.api_key = os.environ["SECRET_KEY"]

Image preparation

In order to produce variations of an image using DALL·E, you require an input image. This must be a square PNG file of which you intend to generate variations and should be less than 4 MB.

Sample input image

Here is a sample image used as input in this answer:

Input Image
Input Image

Generating variations

With the input image prepared, we are ready to employ the Image.create_variation method to generate variations of the image. This method accepts the following parameters:

  • image: This is the original image for which variations are to be generated. It should be a file object opened in binary mode.

  • n: This indicates the number of variations to be generated.

  • size: This is the size of the resultant images.

Here’s how you can call the Image.create_variation method:

response = openai.Image.create_variation(
image=open("/dalle/image.png", "rb"),
n=1,
size="256x256"
)

The Image.create_variation method yields a response containing a URL to the generated image. This URL can be extracted from the response as shown:

url = response["data"][0]["url"]

Storing the final output

Having retrieved the URL of the generated image, we can proceed to download and save it. We will be using the requests library to download the image, following which the image data is written to a file.

import requests
data = requests.get(url).content
f = open('output/img.png','wb')
f.write(data)
f.close()

The following code will download the image and save it as img.png in the output directory.

Executable code

Having dissected each part of the code, we can now compile it all. Here’s the full executable code for generating image variations using DALL·E in Python:

import os
import openai
import requests
openai.api_key = os.environ["SECRET_KEY"]
response = openai.Image.create_variation(
image=open("/dalle/image.png", "rb"),
n=1,
size="256x256"
)
url = response["data"][0]["url"]
data = requests.get(url).content
f = open('output/img.png','wb')
f.write(data)
f.close()

Note: This code will only be executable when you enter your API key. To learn how to obtain OpenAI's API key, click here.

Example output

In case you're facing difficulties in executing the code due to the absence of an API key, below is the result obtained from a previous successful code execution:

Generated variation of input image
Generated variation of input image

Conclusion

You’ve successfully learned how to generate image variations using DALL·E in Python. This ability of DALL·E unlocks countless opportunities for creative exploration and design. As with all AI tools, it’s crucial to handle it responsibly.

Copyright ©2024 Educative, Inc. All rights reserved