Generate Images With OpenAI in Python
In this world where Generative AI is getting pretty popular, it should be of no surprise that we can generate images as well using AI. One such way to do so is to use the popular OpenAI, the creator of ChatGPT. Developers, writers, and designers are leveraging the ChatGPT API to get their job done faster.
So, in this article, we will try to understand DALL-E by OpenAI which can be used to generate images. We will implement the ChatGPT API Python in a code.
How to Generate Images with OpenAI in Python?
To generate images with OpenAI and edit images with Python, we will have to use the DALL-E API by OpenAI. So, DALL-E is a popular service by OpenAI that generates realistic images based on the prompt that we give.
Prerequisites needed for following this tutorial −
What is DALL-E from OpenAI?
Before getting into the tutorial, it's at least a little important to understand the Generative AI service we will be using.
DALL·E is an AI model to generate images by OpenAI. It has a good task to create realistic images and art from natural language prompts. It is based on studies in neural networks and latent diffusion models (LDM). This allows it to generate images from human-given prompts.
This system uses a modified version of the GPT-3 model. For those who don’t know, OpenAI GPT-3 is OpenAI's widely known language model.
DALL-E can be to interpret human-generated input and generate needed images. Since it went through a huge dataset of image-text pairs while training. DALL·E learns to understand textual descriptions with visual concepts. This lets it generate images that match the given prompts.
Setting Up Your Environment
In order to get started with the implementation of the Python code, first we need to set up the API keys and environment in Python. After this, proceed to write the Python code and complete the image generation through our prompt.
The following are the steps we need to follow to generate images using OpenAI in Python.
Step 1: OpenAI account and API key
Step 2: Install the OpenAI library.
python --version
In Windows −
PS> python -m venv venv
PS> .\venv\Scripts\activate
In Linux/MacOS:
$ python -m venv venv
$ source venv/bin/activate
Recommended by LinkedIn
python -m pip install openai
Step 3: Setting up the environment with an API key.
Now that you have the OpenAI Python library installed, you need to set up your environment with the API key. To do this, you can use an environment variable to store your API key. This will make it available to your Python scripts.
In Windows −
(venv) PS> $ENV:OPENAI_API_KEY = "<your-key-value-here>"
In Linux/MacOS −
(venv) $ export OPENAI_API_KEY="<your-key-value-here>"
Replace “<your-key-value-here>” with your API secret key that you had stored earlier.
Our Python environment is setup now, and our API key is also ready. It’s time to generate the image in Python using DALL-E.
Using DALL·E for Image Generation in Python
Before getting into the actual code of generating images in Python using DALL-E, let’s understand the pricing model of DALL-E for image generation.
DALL-E pricing per image
OpenAI prices each image generation based on the resolution of the image generated. The below table summarizes the per-image pricing for each resolution.
ResolutionPrice per image256×256$0.016512×512$0.0181024×1024$0.020
But let these prices not freak you out. Why? Because if you recently signed up on the OpenAI platform, then it offers a free trial of 18 credits for the first 3 months. Thought, this might limit the resolution of the images generated.
Generate Images with OpenAI in Python
In the following code, we will be importing the OpenAI library and storing the API key in an environment variable. We will be using the Image.create() function of the OpenAI library to store the response by accepting the human-generated prompt that will be called by the user.
File name: image-creator.py
import openai
openai.api_key = "YOUR_API_KEY"
PROMPT = "Clean image of Harley Quinn imagined as a duck"
response = openai.Image.create(
prompt=PROMPT,
n=1,
size="256x256",
)
print(response["data"][0]["url"])
In this code, you need to replace 'YOUR_API_KEY' with the secret API key that you stored before. You can also change the resolution of your image in the “size” variable. You can adjust the “n” parameter to change the number of images to be generated for your prompt. You can also add your own PROMPT to the variable.
To see the output, run the Python file in your terminal.
python image-creator.py
Output
Our output for the prompt that we gave the model −
Congratulations! You have successfully learned how to generate images using OpenAI's DALL·E 2 through the OpenAI API in Python. With this knowledge, you can go through many creative possibilities and use AI-generated images in your applications for a huge range of use cases. Have fun experimenting with DALL·E and its amazing image-generation capabilities!