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 image

Also, 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.

Python3




# 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.

Generate Images With OpenAI in Python

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 Python

Similar Reads

Create 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....

How to Generate Variations of an Image?

...

How to Edit Images using a Mask Image with DALL E API?

...

Frequently Asked Questions on Image Generate with AI

...

Conclusion

...

Contact Us