How To Generate And Image Variations Using DALL-E 2 OpenAI Model?

What is DALL-E 2 model?

DALL-E 2 is a Generative AI model developed by OpenAI, and it is used to generate and understand user prompts into images. Generative AI models can create both natural and artificial images by changing the style of the images and providing different variations of the images.

DALL-E 2 Capabilities

The DALL-E 2 model can generate images and image variations for natural and artificial images.

Open AI Key Generation

Login into the Open AI portal  https://openai.com/ and then click API Keys.

DALL·E 2-Open AI

Click View API Keys. You can find this option on the right side of your profile, and it appears as follows.

DALL·E 2-Open AI

And then create a new secret key. 

DALL·E 2-Open AI

Python Open AI package installation

First, install the OpenAI package using the following command.

pip install OpenAI

Python code for Image Generation

import os
import openai
openai.api_key='sk-WuFw3tNKFEpJ*****BBOT3BlbkFJJzEO7p9u8uYWHehCu44y'
response=openai.Image.create(
    prompt="santa claus in mars space",
    n=1,
    size="1024x1024"
)
print(response["data"][0]["url"])

The above code snippet describes Image Generation using the DALL-E 2 model.

  • prompt- it describes the image the user wants to generate it.
  • n- number of images the user wants to generate.
  • size- the size of the image.

The output of the image returns the URL, click it and download.

Image by DALL·E 2 Open AI

Python code for Image Variations

The below code is used to generate varieties of images using the DALL-E 2 model.

import os
import openai
openai.api_key='sk-WuFw3tNKFEpJ*****BBOT3BlbkFJJzEO7p9u8uYWHehCu44y'
response=openai.Image.create_variation(
    image=open("C:\\Projects\santaclaus.png","rb"),
    n=1,
    size="1024x1024"
)
print(response["data"][0]["url"])

Output

Image by DALL·E 2 Open AI

Summary

In this article, we successfully learned and created DALL-E 2 image generation and image variations using Python Code. For more updates, please stay tuned.

Happy Learning and Coding!!!