How to Generate Images Using OpenAI DALL.E-2 in Python

With advancements in the field of artificial intelligence, it is now capable of doing some outstanding creative work like generating images that never existed before. The exciting part is that these pictures look extremely realistic. This can be considered another major milestone in the field of artificial intelligence.

In this article, we will learn how DALL.E-2 can be used to transfer text into hyper-accurate images. Below are a few images generated using DALL.E 2.

What is DALL.E-2?

DALL.E-2 is an artificial intelligence tool developed by OpenAI which is used to generate realistic images and art from natural text. Technically it is an encoder-decoder model which takes text as input to the encoder which is then fed to the decoder that returns the image as an output.

What Can DALL.E-2 Do?

  1. It is capable of generating images that never existed before.
  2. It can be used to manipulate images such as the addition or deletion of some object.
  3. It can create a different version of the input image.

Later in this article, we will also learn python code to implement the above-mentioned functionality.

Related Read: How to Generate AI Images and Art With Midjourney: Step-by-step Guide

How to Get OpenAI Free API Key

We need to use openai API key to access any of its models. It is extremely easy to get your API, you just have to follow the below steps.

Step1. Login to OpenAI

Login to the OpenAI portal using your google account or your credentials. If you don’t have an existing account, then create one.

Step 2. View API Keys

On the right side, click on your profile. You will see an option ‘View API Keys’. It will redirect you to the API key page.


Step 3: Create New Secret Key

Click on the +Create new secret key button to get your openai api keys.

Python Code For Image Generation

You can use the below command to install openai library in python.

pip install openai

The below python code is used to generate images from the prompt text.

import openai
openai.api_key = Your API-Key
response = openai.Image.create(
    prompt="A girl in black dress and open hair standing on table",


  • prompt = description of the image you want to generate.
  • n = number of images you want to generate.
  • size = size of the image.



The image generated is so close to our input prompt. You can try creating an image of your imagination.

The output will be a link to the image generated. You can follow the 
link and download the image.

Image Manipulation Using DALL-E 2

It can be used to manipulate an existing image i.e addition or deletion of some component. You can ask it to make specific edits to an image. It needs two inputs: image and masked image. The masked image will have the part marked that you want to manipulate.

  1. Input Image
  2. Masked Image
import openai

openai.api_key = Your API-Key

response = openai.Image.create_edit(
    image=open("garden.png", "rb"),
    mask=open("garden_masked.png", "rb"),
    prompt="A red colour car in the garden",
image_url = response['data'][0]['url']


  • image = The image to be manipulated.
  • mask = The masked image.
  • prompt = Text description of the new image to be generated.
  • n = no of images to be generated.
  • size = size of the new image.
The input and masked image should be sqaure i.e equal height and width. The format of images should be png.


The output of the code will be a URL to the new image. Below is the image generated from the existing image and prompt A red colour car in the garden.

Image Variation Using OpenAI

You can also generate a different variety of input images using OpenAI. Below is the python code to generate the new image.

response = openai.Image.create_variation(
    image=open("garden.png", "rb"),
image_variation_url = response['data'][0]['url']

Can Anyone Use DALL-E?

Yes DALLE can be used by anyone. You just need to OpenAI’s API access key. We have already seen how to access the keys above in this article.

Will DALL-E 2 Take Away Jobs?

After knowing the power of DALLE, it is very much clear that the work and earning power of many professions will be significantly affected. Some of these jobs could be designer, photographer or visual content creator.

Many different creative professions ranging from AR designers, landscape artists, tattoo artists, clothing designers, sound designers, dancers etc. are already taking support of DALLE in their work.

It is not bad news though. There are assumptions that DALLE can be used to make some of these professions much more creative.

Concerns About DALL-E 2

As we have seen in this article, DALLE 2 has the capability to generate a significantly realistic image. This power can be used for creating inappropriate content. Therefore, OpenAI has set up some rules for DALLE 2 that prevent how this technology can be used. DALLE cannot be used to create adult, offensive or violent content.

Other Tools to Generate AI Images

End Notes

In this article, we have seen how artificial intelligence has revolutionized the way we create digital art and designs. At last, I would like to say that DALLE is a powerful platform for creating AI images with simple python code.

The most interesting part is that anyone can use these technologies and convert their imagination into reality. Now, I hope you will use DALLE and use your imagination to create amazing images and share them with your friends.

Feel free to give your feedback or ask for any query in the comment box below.

Happy learning 🙂

Related Read: OpenAI DALL.E-2