Wednesday, December 25, 2024
Google search engine
HomeLanguagesGenerate Images With OpenAI in Python

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.

Generate IMage With OpenAI Python

Create AI Image Using Python

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.

Step 1: Log in to your OpenAI account after creating one.

Step 2: As shown in the figure below, after logging in, select Personal from the top-right menu, and then select “View API keys”.

 

Step 3: After completing step 2, a page containing API keys is displayed, and the button “Create new secret key” is visible. A secret key is generated when you click on that, copy it and save it somewhere else because it will be needed in further steps.

 

Step 4: Now launch any text editor or online notebook such as Google Colab or Jupyter Notebook. Here, we’re using a Google Colab notebook to install the Open AI library in Python with the command listed below.

pip install -q openai

Step 5: Import the openai library, and then do as follows. Store the created key in the below-mentioned variable.

python3




# importing openai module
import openai
# assigning API KEY to the variable
  
openai.api_key = 'API_KEY'


Step 6: Import the requests library and Image module from PIL library.

Python3




# importing other libraries
import requests
from PIL import Image


Step 7: Now we define a function to generate an Image using the “create” endpoint of DALL E API.

Python3




# function for text-to-image generation 
# using create endpoint of DALL-E API
# function takes in a string argument
def generate(text):
  res = openai.Image.create(
    # text describing the generated image
    prompt=text,
    # number of images to generate 
    n=1,
    # size of each generated image
    size="256x256",
  )
  # returning the URL of one image as 
  # we are generating only one image
  return res["data"][0]["url"]


The above function takes a string as an argument and passes it to the API endpoint. The other are parameters used are n = “number of images generated using that prompt” and size = “size of the image generated”. The API can give generate the image in either Base64 format or URL. We return the URL of the generated image as the output.

Note: The size of the generated images must be one of 256×256, 512×512, or 1024×1024.

Step 8: Now we generate an Image using the Text Prompt.

Python3




# prompt describing the desired image
text = "batman art in red and blue color"
# calling the custom function "generate"
# saving the output in "url1"
url1 = generate(text)
# using requests library to get the image in bytes
response = requests.get(url1)
# using the Image module from PIL library to view the image
Image.open(response.raw)


Output:

image generated using DALL E Python API

batman art in red and blue color

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:

 

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.

Image generated using DALL E Python API

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.

Frequently Asked Questions on Image Generate with AI

Q1: How to use the OpenAI library in Python?

First, we need to install the OpenAI package using pip install openai in the Python terminal. After this, we need to provide the secret key which can be found on the website itself OpenAI but for that as well you first need to create an account on their website.

Q 2: How can I generate AI images?

In this progression, OpenAI’s Dall-E project is a revolution that can create state of art images on the basis of text prompts provided by the users. Using this tool you can bring your imagination to pictures with just one click. Although this tool is not completely free but to explore it and analyze its workings new users are provided some free image generation count.

Q 3: Can ChatGPT generate images?

No, ChatGPT was not designed to generate images instead it was designed as a ChatBot. It can give efficient answers and suggestions to problems but it can not create any visualization or images as per the requirements. ChatGPT is a transformer-based model which is well-suited for NLP-related tasks.

Q 4: How do I access OpenAI API in Python?

If you would like to access the OpenAI API then you need to first create your account on the OpenAI website. After this, you can get your API key unique for your account which you can use. After that, you can follow this article to create awesome images using Python scripts. But the OpenAI API is not free of cost for the commercial purpose but you can use it for some trial or educational purposes.

Q 5: How to use OpenAI image Generator?

After completing the above steps mentioned to use the OpenAI API in Python we just need to use the create function with some prompt in it to create the desired number of images for that prompt. Also, we can create variations of an existing image using the create_variations() function provided by the library.

Conclusion

We covered several ways in the whole article for generating new images and editing existing images using DALL – E API using Python which would help you in achieving your desired outputs. There are numerous use cases of DALL – E like social media content generation, logo creation, or generating stock photos, etc.

To learn more about Chat GPT, you can refer to:

RELATED ARTICLES

Most Popular

Recent Comments