How to Generate Variations of an Image?

Here we are going to use the same image generated above by DALL E and generate its variations.

Since DALL E only accepts square PNG images with sizes less than 4 MB and in RGBA format, we save our image with extension png and in RGBA format using the following code.

Python3




response = requests.get(url1)
# saving the image in PNG format
with open("img.png", "wb") as f:
  f.write(response.content)
# opening the saved image and converting it into "RGBA" format
# converted image is saved in result
result = Image.open('img.png').convert('RGBA')
# saving the new image in PNG format
result.save('img_rgba.png','PNG')


To generate variations of an existing Image we use the “create_edit” endpoint of the DALL-E API.

Python3




# editing image using create_edit endpoint of DALL-E API
response = openai.Image.create_edit(
  # opening original image in read mode
  image=open("/content/img_rgba.png", "rb"),
  # opening mask image in read mode
  mask=open("/content/mask.png", "rb"),
  # propmt describing the desired image
  prompt="gotham city skyline behind batman",
  # number of images to be generated
  n=3,
  # size of each generated image
  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_variant_{i}.png", "wb") as f:
    f.write(response.content)


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