Generate Images With OpenAI in Python
Last Updated :
16 Apr, 2025
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 PythonCreate 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.
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.
python
# 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.
Python
# 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.
Python
# 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 256x256, 512x512, or 1024x1024.
Step 8: Now we generate an Image using the Text Prompt.
Python
# 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 colorHow to Generate Variations of an Image?
Here we are going to use the same image generated above by DALL E and generate its variations.
Since DALL E only accepts square PNG images with sizes less than 4 MB and in RGBA format, we save our image with extension png and in RGBA format using the following code.
Python
response = requests.get(url1)
# saving the image in PNG format
with open("img.png", "wb") as f:
f.write(response.content)
# opening the saved image and converting it into "RGBA" format
# converted image is saved in result
result = Image.open('img.png').convert('RGBA')
# saving the new image in PNG format
result.save('img_rgba.png','PNG')
To generate variations of an existing Image we use the "create_edit" endpoint of the DALL-E API.
Python
# editing image using create_edit endpoint of DALL-E API
response = openai.Image.create_edit(
# opening original image in read mode
image=open("/content/img_rgba.png", "rb"),
# opening mask image in read mode
mask=open("/content/mask.png", "rb"),
# propmt describing the desired image
prompt="gotham city skyline behind batman",
# number of images to be generated
n=3,
# size of each generated image
size="256x256"
)
# saving the URLs of all image in new variable "res"
res = response['data']
# loop to save and display images
for i in range(len(res)):
# saving URL of image in res
image_url = res[i]['url']
# extracting image from URL in bytes form
response = requests.get(image_url, stream=True)
# opening the image
k = Image.open(response.raw)
# displaying the image
k.show()
# saving the image
with open(f"img_variant_{i}.png", "wb") as f:
f.write(response.content)
Output:
How to Edit Images using a Mask Image with DALL E API?
In this section, a mask will be uploaded and a text prompt will be supplied in order to change an image. Where the image should be altered is indicated by the transparent portions of the mask, and the prompt should describe the entire new image rather than just the area that was erased.
Make sure your image and mask are of the same size (square PNG) and less than 4MB in size before passing them as arguments to API. We will be using the following images.
input imageAlso, write a prompt such that it describes the full new image not just the transparent area that needs to be replaced.Use the following lines of code to edit the image.
Python
# using create_edit endpoint of the DALL - E API
response = openai.Image.create_edit(
# opening original image in read mode
image=open("img_rgba.png", "rb"),
# opening mask image in read mode
mask=open("mask.png", "rb"),
# text prompt describing the new image
prompt="gotham city skyline behind batman",
# number of images to be generated
n=1,
#size of each image generated in pixels
size="256x256"
)
# saving the URLs of all image in new variable "res"
res = response['data']
# loop to save and display images
for i in range(len(res)):
# saving URL of image in res
image_url = res[i]['url']
# extracting image from URL in bytes form
response = requests.get(image_url, stream=True)
# opening the image
k = Image.open(response.raw)
# displaying the image
k.show()
# saving the image
with open(f"img_mask_edit_{i}.png", "wb") as f:
f.write(response.content)
Output:

It is not necessary for the non-transparent portions of the mask to match the original image, as in the example above, because they are not used when creating the output.
Conclusion
We covered several ways in the whole article for generating new images and editing existing images using DALL - E API using Python which would help you in achieving your desired outputs. There are numerous use cases of DALL - E like social media content generation, logo creation, or generating stock photos, etc.
To learn more about Chat GPT, you can refer to:
Similar Reads
OpenAI Python API - Complete Guide
OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industry by storm. Everyone has incorporated ChatGPT to do their work more efficiently and those who failed to do so have lost their job
15+ min read
Extract keywords from text with ChatGPT
In this article, we will learn how to extract keywords from text with ChatGPT using Python. ChatGPT is developed by OpenAI. It is an extensive language model based on the GPT-3.5 architecture. It is a type of AI chatbot that can take input from users and generate solutions similar to humans. ChatGPT
4 min read
Pandas AI: The Generative AI Python Library
In the age of AI, many of our tasks have been automated especially after the launch of ChatGPT. One such tool that uses the power of ChatGPT to ease data manipulation task in Python is PandasAI. It leverages the power of ChatGPT to generate Python code and executes it. The output of the generated co
9 min read
Text Manipulation using OpenAI
Open AI is a leading organization in the field of Artificial Intelligence and Machine Learning, they have provided the developers with state-of-the-art innovations like ChatGPT, WhisperAI, DALL-E, and many more to work on the vast unstructured data available. For text manipulation, OpenAI has compil
10 min read
OpenAI Whisper
In today's time, data is available in many forms, like tables, images, text, audio, or video. We use this data to gain insights and make predictions for certain events using various machine learning and deep learning techniques. There are many techniques that help us work on tables, images, texts, a
9 min read
Spam Classification using OpenAI
The majority of people in today's society own a mobile phone, and they all frequently get communications (SMS/email) on their phones. But the key point is that some of the messages you get may be spam, with very few being genuine or important interactions. You may be tricked into providing your pers
6 min read
How to Use chatgpt on Linux
OpenAI has developed an AI-powered chatbot named `ChatGPT`, which is used by users to have their answers to questions and queries. One can access ChatGPT on searchingness easily. But some users want to access this chatbot on their Linux System. It can be accessed as a Desktop application on Ubuntu o
6 min read
PandasAI Library from OpenAI
We spend a lot of time editing, cleaning, and analyzing data using various methodologies in today's data-driven environment. Pandas is a well-known Python module that aids with data manipulation. It keeps data in structures known as dataframes and enables you to alter, clean up, or analyze data by c
9 min read
ChatGPT Prompt to get Datasets for Machine Learning
With the development of machine learning, access to high-quality datasets is becoming increasingly important. Datasets are crucial for assessing the accuracy and effectiveness of the final model, which is a prerequisite for any machine learning project. In this article, we'll learn how to use a Chat
7 min read
How To Implement ChatGPT In Django
Integrating ChatGPT into a Django application allows you to create dynamic and interactive chat interfaces. By following the steps outlined in this article, you can implement ChatGPT in your Django project and provide users with engaging conversational experiences. Experiment with different prompts,
4 min read