Image
We can perform Image generation and Image editing using DALL-E model of OpenAI. Before we begin, let’s import some image-processing libraries.
Python3
# importing other libraries import requests from PIL import Image from io import BytesIO |
Now we construct a function to produce an image using the DALL E API’s “create” endpoint.
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" ] |
An argument string is passed to the API endpoint by the aforementioned function. Other parameters are n (the “number of images generated using that prompt”) and size (the “size of the image generated”). Either Base64 or a URL can be used by the API to generate an image. As output, API provides the created image’s URL.
Note: The size of the generated images must be one of 256×256, 512×512, or 1024×1024.
Examples for Image Generation
Now Let’s see some examples to gain a better understanding of Image Generation using DALL-E
Prompt 1:
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, stream = True ) # using the Image module from PIL library to view the image Image. open (response.raw) |
Output:
batman art in red and blue color
Prompt 2:
Python3
# text prompt describing my desired image text = "a scenic view of moon shining light on a yacht" # generate function uses DALL-E API to generate image # it returns a temporary URL of the image url1 = generate(text) # We use the requests library to fetch the image from URL response = requests.get(url1, stream = True ) # We use the Image Class from PIL library to open the image Image. open (response.raw) |
Output:
A scenic view of moon shining light on a yacht
Examples for Image Editing
For editing the image we use the create edit endpoint of the 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. For this we use the below 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' ) |
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.
Let’s see the example to gain a better understanding of Image Editing using DALL-E.
Image 1:
We will be using the below image as input:
Input Image for Create Edit endpoint
Below is the code for editing the image and generating 3 images as output.
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:
OpenAI Python API – Complete Guide
OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industry by storm. Everyone has incorporated ChatGPT to do their work more efficiently and those who failed to do so have lost their jobs. The age of AI has started and people not adapting to AI could introduce some difficulties for them.
In this article, we will be discussing how you can leverage the power of AI and make your day-to-day tasks a lot easier by using the OpenAI APIs (Application Programming Interface) that allow developers to easily access their AI models and Integrate them into their own applications using Python.
Table of Content
- What is OpenAI?
- What is OpenAI API?
- Generate OpenAI API key
- Installation of OpenAI package
- Prompt Engineering
- Text
- Chat
- Image
- Audio
- Embeddings
- Fine-Tuning
- API Error Codes
- Conclusion
- FAQs on OpenAI Python API
Contact Us