Generate Images With OpenAI in Python

Here we are going to see the steps to use DALL – E API in Python. Using DALL – E API we are able to generate and edit images using Python code.

Step 1: Log in to your OpenAI account after creating one.

Step 2: As shown in the figure below, after logging in, select Personal from the top-right menu, and then select “View API keys”.

 

Step 3: After completing step 2, a page containing API keys is displayed, and the button “Create new secret key” is visible. A secret key is generated when you click on that, copy it and save it somewhere else because it will be needed in further steps.

 

Step 4: Now launch any text editor or online notebook such as Google Colab or Jupyter Notebook. Here, we’re using a Google Colab notebook to install the Open AI library in Python with the command listed below.

pip install -q openai

Step 5: Import the openai library, and then do as follows. Store the created key in the below-mentioned variable.

python3




# importing openai module
import openai
# assigning API KEY to the variable
  
openai.api_key = 'API_KEY'


Step 6: Import the requests library and Image module from PIL library.

Python3




# importing other libraries
import requests
from PIL import Image


Step 7: Now we define a function to generate an Image using the “create” endpoint of DALL E API.

Python3




# function for text-to-image generation 
# using create endpoint of DALL-E API
# function takes in a string argument
def generate(text):
  res = openai.Image.create(
    # text describing the generated image
    prompt=text,
    # number of images to generate 
    n=1,
    # size of each generated image
    size="256x256",
  )
  # returning the URL of one image as 
  # we are generating only one image
  return res["data"][0]["url"]


The above function takes a string as an argument and passes it to the API endpoint. The other are parameters used are n = “number of images generated using that prompt” and size = “size of the image generated”. The API can give generate the image in either Base64 format or URL. We return the URL of the generated image as the output.

Note: The size of the generated images must be one of 256×256, 512×512, or 1024×1024.

Step 8: Now we generate an Image using the Text Prompt.

Python3




# prompt describing the desired image
text = "batman art in red and blue color"
# calling the custom function "generate"
# saving the output in "url1"
url1 = generate(text)
# using requests library to get the image in bytes
response = requests.get(url1)
# using the Image module from PIL library to view the image
Image.open(response.raw)


Output:

batman art in red and blue color

Generate Images With OpenAI in Python

We are currently living in the age of AI. Images to automate processes including image generation for logos, advertisements, stock images, etc. So here we will use OpenAI to generate Images with Python [ChatGPT API]. There are numerous uses of the DALL – E model and today we will be discussing how one can use its Python ChatGPT API [OpenAI API] to generate new images and edit existing images. But, before moving ahead let’s know a little about what DALL E is.

Create AI Image Using Python

Similar Reads

Create AI Image Using Python

DALL – E is developed by OpenAI. It is based on a modified version of the GPT-3 model that allows the AI model to generate images from textual or image input. DALL – E is trained on 3.5 billion parameters which allows it to perform a wide range of tasks on images seamlessly. DALL – E has many use cases like social media content creation, logo creation, editing images, advertisement generation, and many others, thus making it a valuable tool in today’s time....

Generate Images With OpenAI in Python

Here we are going to see the steps to use DALL – E API in Python. Using DALL – E API we are able to generate and edit images using Python code....

How to Generate Variations of an Image?

...

How to Edit Images using a Mask Image with DALL E API?

...

Frequently Asked Questions on Image Generate with AI

...

Conclusion

...

Contact Us