How to edit an image using DALL.E API

DALL.E, developed by OpenAI, is an AI model capable of crafting images from textual inputs. Notably, it also has the capacity to modify existing images. In this Answer, we'll explore the process of editing images using Python and the DALL·E API. This includes environment setup, preparing the necessary images, executing the necessary code, and storing the final product.

Environment setup

Before delving into the process, ensure Python is installed on your computer. Additionally, you'll need to have the openai and requests libraries installed, which can be done using pip:

pip install openai requests

Following this, you'll need to input your OpenAI API key, which is required to authorize your requests to the OpenAI API.

import os
import openai
openai.api_key = os.environ["SECRET_KEY"]

Preparing the input images

Editing an image using DALL·E requires two primary input images: the initial image you want to alter and a secondary mask image that specifies the areas of the primary image you want to adjust.

Both the original and mask images should be square images in PNG format. In the mask image, the areas highlighted in white represent the parts to be edited, while the colored areas denote the parts to be kept intact.

Editing the image

With our images ready, we can use the Image.create_edit method to commence the image modification. This function requires the following parameters:

  • image: The original image to edit as a file object in binary mode.

  • mask: The mask image is also a file object in binary mode.

  • prompt: Textual instruction that outlines how to modify the image.

  • n: The number of images to be generated.

  • size: The dimensions of the resulting image.

Below is an example of using the Image.create_edit method:

response = openai.Image.create_edit(
image=open("/dalle/image.png", "rb"),
mask=open("/dalle/mask.png", "rb"),
prompt="An indoor lounge area with a brown dog swimming in the pool",
n=1,
size="256x256"
)

This function returns a response, including a URL to the edited image. This URL can be extracted from the response as follows:

url = response["data"][0]["url"]

Storing the resultant image

Once the URL of the modified image is obtained, it can be downloaded and stored using the requests library to download the image, which is then written into a file.

import requests
data = requests.get(url).content
f = open('output/img.png','wb')
f.write(data)
f.close()

This script will save the downloaded image as img.png in the output directory.

Complete executable code

Now that we've covered all segments of the process, we will compile it all. Here is the complete Python script for editing an image using DALL·E:

import os
import openai
import requests
openai.api_key = os.environ["SECRET_KEY"]
response = openai.Image.create_edit(
image=open("/dalle/image.png", "rb"),
mask=open("/dalle/mask.png", "rb"),
prompt="An indoor lounge area with a brown dog swimming in the pool",
n=1,
size="256x256"
)
url = response["data"][0]["url"]
data = requests.get(url).content
f = open('output/img.png','wb')
f.write(data)
f.close()

Note: This code will only be executable when you enter your API key. To learn how to obtain OpenAI's API key, click here.

Conclusion

You have now successfully learned how to modify images using Python and the DALL·E API. Feel free to experiment with various prompts and mask images to see the diverse range of alterations DALL·E can execute. Always remember AI is a highly potent tool, and it's crucial to wield it responsibly.

Copyright ©2024 Educative, Inc. All rights reserved