How to Generate Images Using the OpenAI API and DALL·E 2 in Python
If you’ve ever used ChatGPT, then you’ve witnessed its intuition. With OpenAI’s APIs, code generation, text completion, text comparison, model training, and image generation are all in the bag for developers to explore and integrate into apps.
In this tutorial, you’ll learn how to use OpenAI’s API with Python to generate, edit, and vary images using natural language prompts.
Getting Started With OpenAI Using Python
You need to set up a few things before starting this tutorial. First, ensure you’ve installed Python’s latest version on your computer. If you use a Linux distribution like Ubuntu, you might also want to seehow to install Python on Ubuntu.
Create a new folder for your project and open your command line to your project directory.
Next is the OpenAI package installation. You need to installpillowto convert images to RGBA while using the image editing endpoint. You might also want to installpython-dotenv; you’ll use this to mask secret keys.
We recommend creating adedicated Python virtual environmentto isolate dependencies.
The code used in this project is available in aGitHub repositoryand is free for you to use under the MIT license.
To install thepython-dotenv,pillow, andopenaipackages, run the following command in the terminal:

Now head over toOpenAIand log in to your dashboard to grab your API key:
How to Generate and Edit Images With OpenAI API in Python
OpenAI API’s image generation features endpoints for generating new images, creating image variations, and editing them.
In the following sections, you’ll explore these endpoints to generate, edit, tweak, and download images using natural language prompts.

Generating Images Using the OpenAI API
The image generation endpoint accepts three keyword arguments. These includeprompt,n, andsize.
Thepromptkeyword is a string of text describing the image you wish to generate, whilenis the number of images to generate during an API call. Thesizeis the image size, and as of writing, it only accepts 256x256, 512x512, and 1024x1024 pixels, according to the officialOpenAIimage generation documentation.
The following code generates images using natural language prompts and outputs their URL (image addresses):
The above code contains anImageGeneratorclass with theimage_urlandAPIKeyattributes. ThegenerateImagemethod adapts the requirements of the image generation endpoint. It generatesnURLs using the natural language prompt, depending on theImageCountvalue.
However,self.image_urlextracts the generated image URLs from the JSON response into a list using the for loop in a list comprehension.
The output looks like this:
Finally, thedownloadImagemethod downloads each image by requesting the generated addresses. This method acceptsnnumber of names that you wish to give each image.
Editing Images Using the OpenAI API
The editing endpoint allows you to edit an existing image based on a mask template. The mask is an RGBA format with an annotated, transparent area and must be the same size as the image you want to edit.
When you provide a mask, the editing endpoint replaces its masked area with the new prompt in the existing image to produce a new one. Add the following methods to the above previous class:
TheconvertImagemethod converts the mask image to RGBA format. You can achieve this using theconvertmethod from Python’s pillow package (imported as PIL).
Once converted, it saves the new mask as an override of the existing one. Thus, image conversion is the first task the editing method (editImage) executes.
Theimagereads the target image from an existing file, while themaskreads the converted mask image, which must have a transparent area.
You can mark out a transparent area in the mask usingphoto editing softwarelike Gimp or Photoshop.
For example, the image we want to edit using the prompt in the code above is:
Now, assume you want to replace the antelope standing by the river bank with an eagle, as stated in the prompt.
Here’s what the mask looks like:
Since the keyword in the prompt from the above code says, “an eagle standing on the river bank,” the blank spot gets filled up by an eagle, replacing the antelope that was previously there.
Here’s the new image in this case:
You can try playing around with the image editing endpoint using other photos.
Generating Image Variations in Python Using the OpenAI API
The variation endpoint generates alternative images from an existing image. Expand the image generator class further by adding an image variation method as shown below:
The above code generates variations of an image.
Use OpenAI to Your Advantage
While some people fear AI might steal their jobs, it might be a haven, after all—if you learn to control and use it. This OpenAI image creation tutorial is just one of many use cases of AI in the real world. The OpenAI APIs provide handy pre-trained models you can easily integrate into your app. So you can take up the challenge and build something worthwhile from this tutorial.
Although the image generation API is still in beta as of writing, it already lets you craft imaginary artworks. Hopefully, it gets further updates to allow upscaling and accept varying image sizes.
These free AI apps can take a sentence you type and turn it into a realistic painting or image.
it’s possible to block out the constant surveillance and restore your privacy with a few quick changes.
Unlock a world of entertainment possibilities with this clever TV hack.
OneDrive is one of the best, but it has a catch.
These films will leave you questioning humanity, but also wanting more.
It’s not super flashy, but it can help to keep your computer up and running.