Product

Tuesday, 9 May 2023

Complete Guide to using DALL-E API in Python

 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

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.

Setup

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
Next, we will import the required Python modules. Type and execute the below code in another cell in the Colab.
import os
import openai
from IPython.display import Image
from IPython import display
from base64 import b64decode
Let’s set up the OpenAI API or secret key.
Copy your OpenAI API or secret key and paste it between the quotes in the below line
openai.api_key = ""

Now you are ready to make your first call to DALL-E API in Python.

Calling DALL-E API in Python to generate a new image

Run the below code to make your first call to DALL-E API in Python.
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'][0]['b64_json']
Now we use this base64 string to display the image in our Google Colab.
display.Image(b64decode(image_b64))

I will be updating this guide with how to play around with the various parameters in the API calls as well as will cover other features of the API.

Stay tuned.

No comments:

Post a Comment

Connect broadband

AI:List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with trauma for kin ocd formed after forced to do rituals, family tribal values honor specific activities, utmost religious activities occult witchcraft enforced to do by elder sibling his wife and mother thrice with great celibacy turned the kundalini activated after deep celibacy penance shouting physical Mental financial Extortion by sibling with toxic behaviours and the wife mother and then mockery suppression asking about who’re your worth backbiting among relatives sector toon negligence of intelligence intuitive behaviour- all religious psychological traps when not work and lead to loss of consciousness of kin trance state recovered by governance with technocrats using AI - kin was asked by mother that he’s to go the male should not be seen in house he’s many absurd thought forcefully in brain mind and outsiders already feel awkward of this kin unresponsive behaviour due to TBI PTSD ocd cvt and tell at workplace social gathering he’s to go and asked for sector toon extortion and mockery in various sense due to un practical behaviour negligence of body building due to neglect ion if essential food nutrition and stubbed suppressed in every sense in religious psychological traps by family members AI humanoid using various neural networks and LLMs for kin and required steps.

  List AI views on when for a naive kin after prolonged life suppression coercive behaviour by family members after demise of father with tr...