Create an Image variation with DALL·E 2
Post by Amina Delali, July 31th, 2023
How to Create an Image Variation with OpenAI and Dall.e 2?
Imagine having an image that you like a lot, or that corresponds exactly to what you wanted. But you can't use this image over and over. Or maybe, you need several similar images to it at the same time. With generative AI you can easily solve this issue, especially using DALL-E2 and the OpenAI API.
Considering that you have created an OpenAI account , and you have already credits for using the API, whether you paid for them, or had them for free. You can ask DALL.E2, to create a similar image to the one that you already have. This is done by making a request of an Image Variation using your provided key, and by specifying some parameters:
- The image from which you want to create a variation. For us, it will be "myImage2.png "
- The number of images to generate. We will generate only one image.
- The size of the generated images. For the sizes, I selected: 1024x1024 pixels image size.
Now, that we have made the request, we are going to recuperate the generated image, save it to a file, then show that image:
We can also create a Canvas widget in a Tkinter window, and display that image in the created Canvas object. But before doing so, we must create a PhotoImage object corresponding to a resized version of our created image:
You can find the entire code below, but you must change the statement: openai.api_key = "Your_OpenAI_Key" first, to make the code works for you. Just replace the string "Your_OpenAI_Key" by the string of your OpenAI key.
from tkinter import *
from PIL import ImageTk, Image
openai.api_key = "Your_OpenAI_Key"
file_name = "theGenImage.png"
imageFile = Image.open(file_name)
main_window.title("Python OpenAI Image Generation")
imageFile = imageFile.resize((580,530), Image.LANCZOS)
imageFile = ImageTk.PhotoImage(imageFile)
can = Canvas(main_window, width=580, height=530,bg=bgColor)
can.image = imageFile
can.create_image(0,0, anchor=NW, image=imageFile, tags="bg_img")
Something to say ?
If you want to add something, please feel free to do it by commenting below 🙂 .