These are the steps to generate images with DALL-E using Python:
- Get an OpenAI API key.
- Install the
openaiPython module. - Create a new
.pyfile and import theopenaimodule in your code. - Add your OpenAI API key to the
openaimodule. - Create an image generation request using the
openaimodule’screate()method. - Print the response which contains an image URL, and view it in a browser.
Let’s go!
Create a basic image
First of all make sure you have an OpenAI API key. Then install the openai Python module with pip if you don’t have it already.
pip install openai
Now we’re ready to create a new Python file and import the openai module so we can tell it our API key. Firstly here’s the wrong way to do it, because your key is visible to others if you share your code online, even by accident.
import openai
openai.api_key = "YOUR-API-KEY"
Instead, we should use environment variables to hide the API key in a separate file, just in case our program gets shared publicly. This is a good way to do it:
import os
from dotenv import load_dotenv
import openai
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
The result is the same, but this is much safer. Now on to generating the images.
We’re going to request images from the API and then receive a response, so let’s create a response variable. That will hold the output of an openai.Image.create() method, like this:
response = openai.Image.create()
The create() method takes three arguments as follows:
- prompt: Not surprisingly the description of the image(s) you want.
- n: The number of images you want. Can range from 1 to 10.
- size: The dimensions of the image(s) you want. There are three choices:
- 256×256
- 512×512
- 1024×1024
Only square images are possible (at the time of writing this), and smaller is a little bit cheaper, so I recommend using 256×256 at first while you tweak your prompt, and then increase the size when you’re happy with the output. Incidentally check the OpenAI pricing page for the latest image costs.
Here’s how the request looks with those create() arguments added:
response = openai.Image.create(
prompt = "A caricature of a cute happy puppy.",
n = 1,
size = "256x256"
)
print(response)
When we print the response, this is what we get:
{
"created": 1693889011,
"data": [
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-DEz3mTrycX3ukdSMPHvMwFpx/user-G29BcUhw975rRTTSkrJ6pyw7/img-PxP5vWAKo9A8zcuA3TCEXEPy.png?st=2023-09-05T03%3A43%3A30Z&se=2023-09-05T05%3A43%3A30Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-09-05T01%3A32%3A02Z&ske=2023-09-06T01%3A32%3A02Z&sks=b&skv=2021-08-06&sig=jbnRryAZmkVlZXpF7OKZogb9dZSVq0FyoCTXUb/qedw%3D"
}
]
}
See that url property? That, my friends, is our hot-off-the-press image! Paste it into a browser window and this is what you’ll see:

Create variations of an image
Let’s say the image generation is looking good but we want to see some new-but-similar variations. We can do this in two steps:
- Replace the
create()method with acreate_variation()method. - Replace the
promptargument with an existing image.
For the image argument, it’s not enough just to use an image filename. We need to open the file using Python’s built-in open() function. As well as specifying the filename, we also need an argument to set the mode for opening the file – readonly (“r”) and binary (“b”).
response = openai.Image.create_variation(
image = open("puppy.png", "rb"),
n = 1,
size = "256x256"
)
And the result is even cuter.

Opening the images in a browser
When we print the response from the API, we see a JSON object with a data array containing image URLs. Wouldn’t it be cool if we could automatically open those URLs in browser tabs rather than copying and pasting them? Well we can!
Python has a built-in module for opening a web browser, and it’s called (you guessed it) webbrowser – so human-friendly! To use it, you just import it and then call its open() method, like this:
import webbrowser
webbrowser.open("https://example.com")
In the case of our image generation code, we get up to 10 image URLs as an array (list) inside the data property of the API’s response. We can loop through them and open each one in a browser tab, like this:
images = response["data"]
for image in images:
url = image["url"]
webbrowser.open(url)
The final code
Putting all that together using the original create() function rather than create_variation(), here’s the final code for generating images from scratch using the OpenAI API.
import openai
import os
from dotenv import load_dotenv
import webbrowser
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Image.create(
prompt = "A caricature of a cute happy puppy.",
n = 2,
size = "256x256"
)
images = response["data"]
for image in images:
url = image["url"]
webbrowser.open(url)
For more information and examples, I recommend taking a look at the OpenAI Images API documentation.

Leave a comment