In this article, we will see how to use and call OpenAI DALL-E API in Python. We will cover how to use Google Colab notebook to call DALL-E API in Python.
We will demonstrate calling DALL-E API in Python for all its features, like new image generation, editing an image, and generating image variations.
Let’s dive in.
Table of Contents
Requirements for calling DALL-E API in Python
You could run all the code from this article locally on your machine too.
However, for demonstration assumes you are using a Google Colab Notebook.
If you are going to run it locally, you need to make sure you have Python 3.x or higher installed.
You also need an OpenAI Account.
Open up a new Google Colab Notebook.
The rest of the steps below require you to type the code in the Google Colab and execute that particular cell before going to the next step.
Install OpenAI’s Python module by running this code
!pip install openai
import os import openai from IPython.display import Image from IPython import display from base64 import b64decode
openai.api_key = ""
Calling DALL-E API in Python to generate a new image
response = openai.Image.create( prompt="A cute baby sea otter", n=1, size="256x256", response_format="b64_json" )
This code generates a single variant image for the prompt “a cute sea otter”. You can go ahead and change the prompt to whatever you like.
the response is a JSON object that is stored in the response variable.
If you print the response, you will output it like this.
We will now use the base64 string that was returned to us by the DALL-E API to display the image in the Google Colab Notebook itself.
First, we extract the base64 string from the response JSON object
image_b64 = response['data']['b64_json']