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.
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 osimport openaiopenai.api_key = os.environ["SECRET_KEY"]
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.
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"]
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 requestsdata = requests.get(url).contentf = open('output/img.png','wb')f.write(data)f.close()
This script will save the downloaded image as img.png
in the output
directory.
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 osimport openaiimport requestsopenai.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).contentf = 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.
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.