Describe any picture, then let a pc create it for you. What sounded futuristic just a few years in the past has change into actuality with advances in neural networks and latent diffusion fashions (LDM). DALL·E by OpenAI has made a splash by means of the superb generative art and reasonable pictures that folks create with it.
OpenAI now permits entry to DALL·E by means of their API, which suggests that you may incorporate its performance into your Python purposes.
You’ll want some expertise with Python, JSON, and file operations to breeze by means of this tutorial. You too can examine up on these subjects when you go alongside, as you’ll discover related hyperlinks all through the textual content.
In case you haven’t performed with the web user interface (UI) of DALL·E earlier than, then strive it out earlier than coming again to learn to use it programmatically with Python.
Full the Setup Necessities
In case you’ve seen what DALL·E can do and also you’re desperate to make its performance a part of your Python purposes, then you definitely’re in the fitting spot! On this first part, you’ll rapidly stroll by means of what it is advisable do to get began utilizing DALL·E’s picture creation capabilities in your personal code.
Set up the OpenAI Python Library
Affirm that you simply’re running Python version 3.7.1 or greater, create and activate a virtual environment, and set up the OpenAI Python library:
The openai
package deal provides you entry to the total OpenAI API. On this tutorial, you’ll concentrate on the Picture
class, which you should use to work together with DALL·E to create and edit pictures from textual content prompts.
Get Your OpenAI API Key
You want an API key to make profitable API calls. Join the OpenAI API and create a brand new API key by clicking on the dropdown menu in your profile and selecting View API keys:

On this web page, you possibly can handle your API keys, which let you entry the service that OpenAI gives by means of their API. You possibly can create and delete secret keys.
Click on on Create new secret key to create a brand new API key, and replica the worth proven within the pop-up window:

At all times hold this key secret! Copy the worth of this key so you possibly can later use it in your venture. You’ll solely see the important thing worth as soon as.
Save Your API Key as an Atmosphere Variable
A fast technique to save your API key and make it obtainable to your Python scripts is to reserve it as an surroundings variable. Choose your working system to find out how:
With this command, you make the API key accessible underneath the surroundings variable OPENAI_API_KEY
in your present terminal session. Remember the fact that you’ll lose it for those who shut your terminal.
You possibly can identify your variable nonetheless you want, however for those who use OPENAI_API_KEY
, which is the identify steered by the OpenAI documentation, then you definitely’ll be capable to use the offered code examples while not having to do any extra setup.
Perceive Pricing for DALL·E and Different OpenAI API Merchandise
OpenAI assigns your API utilization by means of the distinctive key values, so make sure that to maintain your API key non-public solely to your self. The corporate calculates pricing of requests to the Photos API on a per-image foundation that relies on the decision of the output picture:
Decision | Value per picture |
---|---|
256×256 | $0.016 |
512×512 | $0.018 |
1024×1024 | $0.020 |
In case you signed up with OpenAI’s API not too long ago, then you definitely’ll profit from the free trial that lets you use $18 of free credit inside your first three months. That lets you generate lots of pictures for those who’re simply right here to discover!
Nonetheless, remember that it’s a single free trial price range throughout all OpenAI API companies, so that you may not need to spend all of it on creating stunning images. Additionally word that you may’t use the credits from the DALL·E internet interface for API calls.
Be aware: OpenAI’s API companies are altering quickly. You must verify their web page for up-to-date details about pricing and gives.
With the pricing and logistics out of the best way, and your API key safely saved, you’re now able to create some pictures from textual content prompts.
Create an Picture From a Textual content Immediate With OpenAI’s DALL·E
Begin by confirming that you simply’re arrange and able to go through the use of the openai
library by means of its command-line interface:
(venv) $ openai api picture.create -p "a vaporwave pc"
This command will ship a request to OpenAI’s Photos API and create one picture from the textual content immediate "a vaporwave pc"
. Because of this, you’ll obtain a JSON response that incorporates a URL that factors to your freshly created picture:
"created": 1668073562,
"knowledge": [
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org
⮑ -QANMxYn3BsMeuAbRT8X3iiu3/user-xSuQTJ0IIVj3dHM4DPymXTg4/img-5GqtVx
⮑ L86Retwi282RbE8HzA.png?st=2022-11-10T08%3A46%3A02Z&se=2022-11-10T1
⮑ 0%3A46%3A02Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&sk
⮑ oid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-
⮑ a814-9c849652bcb3&skt=2022-11-09T14%3A20%3A19Z&ske=2022-11-10T14%3
⮑ A20%3A19Z&sks=b&skv=2021-08-06&sig=yorbHuIy/qHhWvGPmJrZ8apJptorzpI
⮑ 0/62VH2lmhcg%3D"
]
Click on your URL or copy and paste it into your browser to view the picture. Right here’s the picture that DALL·E dreamt up for my request:

Your picture will look totally different. That’s as a result of the diffusion mannequin creates every of those pictures solely once you submit the request.
Be aware: The URL together with your generated picture is barely legitimate for one hour, so make sure that to avoid wasting the picture to your pc for those who prefer it and need to hold it round.
The API additionally follows the identical content policy as the net interface. In case you ship textual content prompts that battle with the content material coverage, you received’t obtain a outcome, and also you would possibly get blocked after repeated violations.
Now that you simply’ve confirmed that all the things is ready up accurately and you bought a glimpse of what you are able to do with the OpenAI Photos API, you’ll subsequent learn to combine it right into a Python script.
Name the API From a Python Script
It’s nice that you may create a picture from the command-line interface (CLI), however it’d be even higher to include this performance into your Python purposes. There’s lots of thrilling stuff you would construct!
Be aware: Be aware that the Images API is in public beta. Which means that the API will nonetheless evolve, would possibly change considerably, and would possibly subsequently not be very best for constructing manufacturing purposes. It additionally presently enforces a price restrict of ten pictures per minute and twenty-five pictures per 5 minutes.
Open your favorite code editor and write a script that you simply’ll use to create a picture from a textual content immediate identical to you probably did utilizing the command-line earlier than:
1# create.py
2
3import os
4
5import openai
6
7PROMPT = "An eco-friendly pc from the 90s within the type of vaporwave"
8
9openai.api_key = os.getenv("OPENAI_API_KEY")
10
11response = openai.Picture.create(
12 immediate=PROMPT,
13 n=1,
14 dimension="256x256",
15)
16
17print(response["data"][0]["url"])
Similar to earlier than, this code sends an authenticated request to the API that generates a single picture primarily based on the textual content in PROMPT
. Be aware that this code provides some tweaks that’ll enable you to construct extra performance into the script:
-
Line 7 defines the textual content immediate as a constant. For extra particular outcomes, you added extra textual content to better describe the picture that you simply need to get. Moreover, placing this textual content into a relentless on the prime of your script lets you rapidly refactor your code to gather the textual content from consumer enter as an alternative, as a result of its worth is faster to seek out and edit.
-
Line 9 will get your API key from the surroundings variable that you simply saved it to earlier. Since you’ve named the surroundings variable
OPENAI_API_KEY
, you don’t even want this line of code. Theopenai
library routinely accesses the API key worth out of your surroundings so long as you caught to the steered identify. With this line of code, you would additionally load it from a in a different way named surroundings variable. -
Line 11 creates an occasion of
openai.Picture
and calls.create()
on it. The subsequent couple of traces comprise a few of the parameters that you may go to the tactic. -
Line 12 passes the worth of
PROMPT
to the fittingly namedimmediate
parameter. With that, you give DALL·E the textual content that it’ll use to create the picture. Be aware that you simply additionally handed a textual content immediate once you referred to as the API from the command-line interface. -
Line 13 is a parameter that you simply haven’t used earlier than. It passes the integer
1
to the parametern
. This parameter helps you to outline what number of new pictures you need to create with the immediate. The worth ofn
must be between one and ten and defaults to1
. -
Line 14 reveals you one other new parameter that you simply haven’t used when calling the API out of your CLI. With
dimension
, you possibly can outline the size of the picture that DALL·E ought to generate. The argument must be a string—both"256x256"
,"512x512"
, or"1024x1024"
. Every string represents the size in pixels of the picture that you simply’ll obtain. It defaults to the most important attainable setting, 1024×1024.
Lastly, you additionally need to get the URL to be able to take a look at the generated picture on-line. For this, you step by means of the JSON response to the "url"
key in line 17 and print its worth to your terminal.
If you run this script, you’ll get output that’s much like earlier than, however now you received’t see the entire JSON response, solely the URL:
(venv) $ python create.py
https://oaidalleapiprodscus.blob.core.home windows.web/non-public/org-QANMxYn3BsMe
⮑ uAbRT8X3iiu3/user-xSuQTJ0IIVj3dHM4DPymXTg4/img-4AMS4wJJLFsu6ClQmGDppAeV
⮑ .png?st=2022-11-10T12percent3A22percent3A46Z&se=2022-11-10T14percent3A22percent3A46Z&sp=r&sv=20
⮑ 21-08-06&sr=b&rscd=inline&rsct=picture/png&skoid=6aaadede-4fb3-4698-a8f6-
⮑ 684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-11-10T
⮑ 10percent3A55percent3A29Z&ske=2022-11-11T10percent3A55percent3A29Z&sks=b&skv=2021-08-06&sig=xJW
⮑ imMiA1/nGmFMYKUTsJq7G1u4xSL652r/MrzTH0Nkpercent3D
Click on the hyperlink or paste it in your browser to view the generated picture. Your picture will once more look totally different, however it’s best to see a picture that resembles the immediate that you simply utilized in PROMPT
:

You could discover that this picture is way smaller than the one you created with the CLI name. That’s since you requested the API for a 256×256 pixel picture by means of the dimension
parameter. Smaller are cheaper, so that you simply saved some cash! As a profitable saver, possibly you’d like to avoid wasting one thing else—your picture knowledge.
Save the Picture Knowledge to a File
Whereas it’s nice that you simply’re creating pictures from textual content utilizing Python, DALL·E, and the OpenAI API, the responses are presently fairly fleeting. If you wish to proceed to work with the generated picture inside your Python script, it’s in all probability higher to skip the URL and entry the picture knowledge straight as an alternative:
1# create.py
2
3import os
4
5import openai
6
7PROMPT = "An eco-friendly pc from the 90s within the type of vaporwave"
8
9openai.api_key = os.getenv("OPENAI_API_KEY")
10
11response = openai.Picture.create(
12 immediate=PROMPT,
13 n=1,
14 dimension="256x256",
15 response_format="b64_json",
16)
17
18print(response["data"][0]["b64_json"][:50])
The API lets you swap the response format from a URL to the Base64-encoded picture knowledge. In line 15, you set the worth of response_format
to "b64_json"
. The default worth of this parameter is "url"
, which is why you’ve acquired URLs within the JSON responses thus far.
Whereas the JSON response that you simply get after making use of this transformation appears much like earlier than, the dictionary key to entry the picture knowledge is now "b64_json"
as an alternative of "url"
. You utilized this transformation within the call to print()
on line 18 and restricted the output to the primary fifty characters.
In case you run the script with these settings, then you definitely’ll get the precise knowledge of the generated picture. However don’t run the script but, as a result of the picture knowledge can be misplaced instantly after the script runs, and also you’ll by no means get to see the picture!
To keep away from dropping the one good picture that acquired away, you possibly can retailer the JSON responses in a file as an alternative of printing them to the terminal:
1# create.py
2
3import json
4import os
5from pathlib import Path
6
7import openai
8
9PROMPT = "An eco-friendly pc from the 90s within the type of vaporwave"
10DATA_DIR = Path.cwd() / "responses"
11
12DATA_DIR.mkdir(exist_ok=True)
13
14openai.api_key = os.getenv("OPENAI_API_KEY")
15
16response = openai.Picture.create(
17 immediate=PROMPT,
18 n=1,
19 dimension="256x256",
20 response_format="b64_json",
21)
22
23file_name = DATA_DIR / f"PROMPT[:5]-response['created'].json"
24
25with open(file_name, mode="w", encoding="utf-8") as file:
26 json.dump(response, file)
With just a few extra traces of code, you’ve added file dealing with to your Python script utilizing pathlib
and json
:
-
Strains 10 and 12 outline and create a knowledge listing referred to as
"responses/"
that’ll maintain the API responses as JSON recordsdata. -
Line 23 defines a variable for the file path the place you need to save the information. You utilize the start of the immediate and the timestamp from the JSON response to create a singular file identify.
-
Strains 25 and 26 create a brand new JSON file within the knowledge listing and write the API response as JSON to that file.
With these additions, now you can run your script and generate pictures, and the picture knowledge will stick round in a devoted file inside your knowledge listing.
Did you run the script and examine the generated JSON file? Seems like gibberish, doesn’t it? So the place’s that lovely picture that you understand with certainty is the perfect picture ever created by DALL·E?
It’s proper there, solely it’s presently represented as Base64-encoded bits, which doesn’t make for a fantastic viewing expertise for those who’re a human. Within the subsequent part, you’ll study how one can convert Base64-encoded picture knowledge right into a PNG file that you may take a look at.
Decode a Base64 JSON Response
You simply saved a PNG picture as a Base64-encoded string in a JSON file. That’s nice as a result of it implies that your picture received’t get misplaced within the ether of the Web after one hour, prefer it does for those who hold producing URLs together with your API calls.
Nonetheless, now you possibly can’t take a look at your picture—until you learn to decode the information. Luckily, this doesn’t require lots of code in Python, so go forward and create a brand new script file to perform this conversion:
1# convert.py
2
3import json
4from base64 import b64decode
5from pathlib import Path
6
7DATA_DIR = Path.cwd() / "responses"
8JSON_FILE = DATA_DIR / "An ec-1667994848.json"
9IMAGE_DIR = Path.cwd() / "pictures" / JSON_FILE.stem
10
11IMAGE_DIR.mkdir(mother and father=True, exist_ok=True)
12
13with open(JSON_FILE, mode="r", encoding="utf-8") as file:
14 response = json.load(file)
15
16for index, image_dict in enumerate(response["data"]):
17 image_data = b64decode(image_dict["b64_json"])
18 image_file = IMAGE_DIR / f"JSON_FILE.stem-index.png"
19 with open(image_file, mode="wb") as png:
20 png.write(image_data)
The script convert.py
will learn a JSON file with the filename that you simply outlined in JSON_FILE
. Keep in mind that you’ll have to adapt the worth of JSON_FILE
to match the filename of your JSON file, which can be totally different.
The script then fetches the Base64-encoded string from the JSON knowledge, decodes it, and saves the ensuing picture knowledge as a PNG file in a listing. Python will even create that listing for you, if vital.
Be aware that this script may also work for those who’re fetching a couple of picture at a time. The for
loop will decode every picture and reserve it as a brand new file.
Be aware: You possibly can generate JSON recordsdata with Base64-encoded knowledge of a number of pictures by operating create.py
after passing a price greater than 1
to the n
parameter.
A lot of the code on this script is about reading and writing files from and into the right folders. The true star of the code snippet is b64decode()
. You import the perform in line 4 and put it to work in line 17. It decodes the Base64-encoded string to be able to save the precise picture knowledge as a PNG file. Your pc will then be capable to acknowledge it as a PNG picture and know easy methods to show to you.
After operating the script, you possibly can head into the newly created folder construction and open the PNG file to lastly see the perfect generated picture that you simply’ve been ready for thus lengthy:

Is it all the things you’ve ever hoped for? In that case, then rejoice! Nonetheless, if the picture you bought appears type of like what you’re searching for however not fairly, then you can also make one other name to the API the place you go your picture as enter and create a few variations of it.
Create Variations of an Picture
When you have a picture—whether or not it’s a machine-generated picture or not—that’s comparable to what you’re searching for however doesn’t fairly match the invoice, then you possibly can create variations of it utilizing OpenAI’s DALL·E latent diffusion mannequin.
Primarily based on the code that you simply wrote earlier on this tutorial, you possibly can create a brand new file that you simply’ll name differ.py
:
1# differ.py
2
3import json
4import os
5from base64 import b64decode
6from pathlib import Path
7
8import openai
9
10DATA_DIR = Path.cwd() / "responses"
11SOURCE_FILE = DATA_DIR / "An ec-1667994848.json"
12
13openai.api_key = os.getenv("OPENAI_API_KEY")
14
15with open(SOURCE_FILE, mode="r", encoding="utf-8") as json_file:
16 saved_response = json.load(json_file)
17 image_data = b64decode(saved_response["data"][0]["b64_json"])
18
19response = openai.Picture.create_variation(
20 picture=image_data,
21 n=3,
22 dimension="256x256",
23 response_format="b64_json",
24)
25
26new_file_name = f"vary-SOURCE_FILE.stem[:5]-response['created'].json"
27
28with open(DATA_DIR / new_file_name, mode="w", encoding="utf-8") as file:
29 json.dump(response, file)
On this script, you ship the Base64-encoded picture knowledge from the earlier JSON response to the Photos API and ask for 3 variations of the picture. You save the picture knowledge of all three pictures in a brand new JSON file in your knowledge listing:
-
Line 11 defines a relentless that holds the identify of the JSON file the place you collected the Base64-encoded knowledge of the picture that you simply need to generate variations of. If you wish to create variations of a unique picture, then you definitely’ll have to edit this fixed earlier than rerunning the script.
-
Line 17 decodes the picture knowledge utilizing
b64decode()
in the identical approach you probably did inconvert.py
, and saves it toimage_data
. Be aware that the code picks the first picture out of your JSON file withsaved_response["data"][0]
. In case your saved response incorporates a number of pictures and also you need to base your variations off one other picture, then you definitely’ll have to adapt the index accordingly. -
Line 20 passes
image_data
as an argument toopenai.Picture.create_variation()
. Be aware that thepicture
parameter of the tactic requires legitimate PNG picture knowledge, which is why it is advisable decode the string from the JSON response earlier than passing it to the tactic. -
Line 21 defines what number of variation pictures of the unique picture you need to obtain. On this case, you set
n
to3
, which implies that you’ll obtain three new pictures.
In case you have a look in your responses/
listing, then you definitely’ll see a brand new JSON file whose identify begins with vary-
. This file holds the picture knowledge out of your new picture variations. You possibly can copy the filename and set it as JSON_FILE
in convert.py
, run the conversion script, and check out your picture variations.
Be aware: You don’t want to make use of Base64-encoded picture knowledge as a supply. As a substitute, you possibly can open a square PNG file no larger than four megabytes in binary mode and go the picture knowledge like that to picture
:
IMAGE_PATH = "pictures/instance.png"
response = openai.Picture.create_variation(
picture=open(IMAGE_PATH, mode="rb"),
n=3,
dimension="256x256",
response_format="b64_json",
)
You too can discover this strategy within the official API documentation on image variations.
Nonetheless, for those who’re planning to incorporate the performance in a Python app, then it’s possible you’ll need to skip saving a PNG file solely to later load the file once more. Due to this fact, it may be helpful to know easy methods to deal with the picture knowledge if it doesn’t come straight from studying a picture file.
How do your picture variations look? Perhaps one in all them stands out as the perfect match for what you have been searching for:

In case you like one of many pictures, however it’s nonetheless not fairly what you’re searching for, then you possibly can adapt differ.py
by altering the worth for SOURCE_FILE
and run it once more. If you wish to base the variations on a picture apart from the primary one, then you definitely’ll additionally want to vary the index of the picture knowledge that you simply need to use.
Conclusion
It’s enjoyable to dream of eco-friendly computer systems with nice AESTHETICS—however it’s even higher to create these pictures with Python and OpenAI’s Photos API!
On this tutorial, you’ve realized easy methods to:
- Arrange the OpenAI Python library domestically
- Use the picture technology capabilities of the OpenAI API
- Create pictures from textual content prompts utilizing Python
- Create variations of your generated picture
- Convert Base64 JSON responses to PNG picture recordsdata
Most significantly, you gained sensible expertise with incorporating API calls to DALL·E into your Python scripts, which lets you convey gorgeous picture creation capabilities into your personal purposes.
Subsequent Steps
The OpenAI image generation API has yet one more characteristic that you may discover subsequent. With an identical API name, you possibly can edit elements of your picture, thereby implementing inpainting and outpainting performance out of your Python scripts.
Search for a script referred to as edit.py
within the offered code examples to provide it a strive:
You would possibly need to do additional post-processing of your pictures with Python. For that, you would learn up on image processing with pillow
.
To enhance the dealing with and group of the code that you simply wrote on this tutorial, you would change the script constants with entries in a TOML settings file. Alternatively, you would create a command-line interface with argparse
that lets you go the variables straight out of your CLI.
You is perhaps curious to dive deeper into latent diffusion models. On this tutorial, you realized to work together with the mannequin by means of an API, however to study extra concerning the logic that powers this performance, you would possibly need to set it up by yourself pc. Nonetheless, for those who wished to run DALL·E in your native pc, then you definitely’re out of luck as a result of OpenAI hasn’t made the mannequin publicly obtainable.
However there are different latent diffusion fashions that obtain equally gorgeous outcomes. As a subsequent step, you would set up a venture referred to as Stable Diffusion domestically, dig into the codebase, and use it to generate pictures with none content material restrictions.
Or you would simply proceed to create lovely and bizarre pictures together with your Python scripts, DALL·E, and the OpenAI API! Which fascinating textual content immediate did you strive? What unusual or lovely picture did DALL·E generate for you? Share your expertise within the feedback under, and hold dreaming!