Product

Thursday, 11 May 2023

DALL-E API Hands-on guide with 5 practical code examples

 OpenAI has made its text-to-image AI model DALL-E available via DALL-E API.

App Developers can start using DALL-E API to integrate text-to-image functionality in their existing or new web or mobile applications.

In this article, let’s take a look at how to call DALL-E API from your code.

Getting access to DALL-E API

In order to use DALL-E API you need to get an OpenAI API key. If you have an OpenAI account, you should already have access to YOUR API key.

If not, create a new OpenAI account.

Login and Visit the OpenAI API page.

You should see your API key listed there.

Using DALL-E API to generate a new image

In order to generate a new image using DALL-E API, you need to call the OpenAI images/generations endpoint.

Open command line program like Terminal(Mac, Linux) or PowerShell(Windows)

Setup your OpenAI API key as an environment variable OPENAI_API_KEY

Run the below command to create your first image using DALL API.

curl https://api.openai.com/v1/images/generations -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{ "prompt": "a photo of a happy corgi puppy sitting and facing forward, studio light, longshot","n":1, "size":"1024x1024",  }'

Make sure to replace $OPENAI_API_KEY it with your own API key or set up the environment variable.

DALL-E API response will turn the image in either of these two formats

  1. a temporary URL pointing to the image (expires in 1 hour)
  2. and a base64 string that you can use to display the image or do some processing on it.

A typical response will look like this.

{
"created": 1667535394,
"data": [
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-ijavQk2ZbRYXuXDmFYAvdb43/user-KMVVQrU7JmET33KPBfD2i9mB/img-bejG7RRQdZFQyR6shxtDxCIg.png?st=2022-11-04T03%3A16%3A34Z&se=2022-11-04T05%3A16%3A34Z&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=2022-11-04T00%3A57%3A32Z&ske=2022-11-05T00%3A57%3A32Z&sks=b&skv=2021-08-06&sig=Jdq2HUseDiaOcFeI94c3lXESy/a9FEdE1AzHoT7iscw%3D"
}
]
}

Since we didn’t specify whether we want the image in the URL or base64 string, the API returns the URL by default. Remember this is a temporary URL and expires soon.

In order to get the image in base64 string format, you need to mention another parameter to the API call called response_format. Let’s see an example.

curl https://api.openai.com/v1/images/generations -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{ "prompt": "a photo of a happy corgi puppy sitting and facing forward, studio light, longshot","n":1, "size":"1024x1024", "response_format":"b64_json"  }'

This returns the image to us as a base64 string that you can use a lot of different to convert into an actual viewable image.

Note that we have a new parameter "response_format":"b64_json" towards the end of the command

You can also control the number of images that you want by passing the number as “n”.

Also, controllable is the output image size by passing a parameter size with values as "1024x1024", "512x512" or "256x256".

Do note though each API call costs you and that cost depend upon the number of images you want and the image size. Pricing is covered in detail later in the article.

Using DALL-E API to edit an image

OpenAI has also made available the edit endpoint for DALL-E. This is how you call it using CURL.

#edits
curl https://api.openai.com/v1/images/edits \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="@/Users/openai/happy_corgi.png" \
  -F mask="@/Users/openai/mask.png" \
  -F prompt="a photo of a happy corgi puppy with fancy sunglasses on sitting and facing forward, studio light, longshot" \
  -F n=1 \
  -F size="1024x1024"

Generate variations of an image using DALL-E API

One can also generate variations of an image using DALL-E API.

#variations
curl https://api.openai.com/v1/images/variations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="@/Users/openai/corgi_with_sunglasses.png" \
  -F n=4 \
  -F size="1024x1024"

make sure to change the path to the image to the actual image you want to use from your computer.

Calling DALL-E API from Python

This is how you call DALL-E API in Python to generate a new image from a text prompt

response = openai.Image.create(
prompt="a white siamese cat",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']

 

Calling DALL-E API from JavaScript (NodeJS)

Calling DALL-E API from JavaScript (NodeJS) works like this

const response = await openai.createImage({
prompt: "a white siamese cat",
n: 1,
size: "1024x1024",
});
image_url = response.data.data[0].url;

DALL-E API Pricing

DALL-E API is charged to your account by OpenAI on a per-API call basis. That means each successful API call gets charged

This charge is based on two things:

  1. How many images you wanted to create in that API call, and
  2. what was the size of the image(s) you wanted?

Below is the pricing per image.

RESOLUTIONPRICE
1024×1024$0.020 / image
512×512$0.018 / image
256×256$0.016 / image

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...