Startec

Startec

Generate Images With OpenAI in Python - GeeksforGeeks

Mai 18, às 11:50

·

7 min de leitura

·

0 leituras

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Generate Images With OpenAI in Python - GeeksforGeeks

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

import openai

openai.api_key = 'API_KEY'

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

Python3

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

def generate(text):

  res = openai.Image.create(

    prompt=text,

    n=1,

    size="256x256",

  )

  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

text = "batman art in red and blue color"

url1 = generate(text)

response = requests.get(url1)

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)

with open("img.png", "wb") as f:

  f.write(response.content)

result = Image.open('img.png').convert('RGBA')

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

response = openai.Image.create_edit(

  image=open("/content/img_rgba.png", "rb"),

  mask=open("/content/mask.png", "rb"),

  prompt="gotham city skyline behind batman",

  n=3,

  size="256x256"

)

res = response['data']

for i in range(len(res)):

  image_url = res[i]['url']

  response = requests.get(image_url, stream=True)

  k = Image.open(response.raw)

  k.show()

  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

response = openai.Image.create_edit(

  image=open("img_rgba.png", "rb"),

  mask=open("mask.png", "rb"),

  prompt="gotham city skyline behind batman",

  n=1,

  size="256x256"

)

res = response['data']

for i in range(len(res)):

  image_url = res[i]['url']

  response = requests.get(image_url, stream=True)

  k = Image.open(response.raw)

  k.show()

  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:


Last Updated : 18 May, 2023

Like Article

Save Article


Continue lendo

AI | Techcrunch

Disney is reportedly preparing a standalone ESPN streaming service
Disney is actively preparing to launch a standalone ESPN streaming service, according to a new report from the Wall Street Journal. The report indicates that ESPN is planning to sell its channel directly to...

Hoje, às 15:51

AI | Techcrunch

The billionaires are trying to live longer… again
Hello, and welcome back to Equity, a podcast about the business of startups, where we unpack the numbers and nuance behind the headlines. This week Mary Ann, Becca, and Alex gathered to chew through the biggest news of the week. Here’s what the gang got into today: Vice goes bankrupt: Now is not a great time […]

Hoje, às 15:17

AI | Techcrunch

NASA picks Blue Origin-led team to build second human landing system on the moon, joining SpaceX
NASA has chosen a Blue Origin-led team to develop a second lunar landing system for the Artemis program, as the agency looks to provide competition with SpaceX and support long-term exploration of the...

Hoje, às 14:41

AI | TechCrunch

Apple reportedly limits internal use of AI-powered tools like ChatGPT and GitHub Copilot
As big tech companies are in a fierce race with each other to build generative AI tools, they are being cautious about giving their secrets away. In a move to prevent any of its data from ending up with...

Hoje, às 13:55

AI | Techcrunch

Apple is on the hunt for generative AI talent
Apple, like a number of companies right now, may be grappling with what role the newest advances in AI are playing, and should play, in its business. But one thing Apple is confident about is the fact that it...

Hoje, às 13:16

Victoria Lo

Enhancing Public Speaking Skills: A Guide by an Introvert
Public speaking can be a daunting task for many people, especially for introverts who may feel uncomfortable in large groups or social situations. However, with a bit of preparation and practice, introverts...

Hoje, às 13:16

DEV

How React Preserve and Reset State
State is isolated between components. React keeps track of which state belongs to which component based on their place in the UI tree. You can control when to preserve state and when to reset it between...

Hoje, às 12:55

AI | Techcrunch

Restaurant365 gobbles up $135M to supersize its software for the food service industry
The price of food continues to go up and up, but surprisingly that hasn’t (yet?) played out as pressure on the wider restaurant industry. Now, a startup that’s building technology to serve that sector announced a supersized round of funding to nourish its growth. Restaurant365, which develops all-in-one restaurant management software, announced $135 million in […]

Hoje, às 11:57

AI | Techcrunch

To secure early-stage funding, entrepreneurs should build ESG into their business models
The fiduciary duty of investment managers would suggest a long-term imperative to ensure that the funds they manage are not placed into assets that will become stranded or obsolete.

Hoje, às 11:30

Hacker News

WSJ News Exclusive | Apple Restricts Employee Use of ChatGPT, Joining Other Companies Wary of Leaks
By Aaron Tilley and Miles KruppaUpdated May 18, 2023 7:35 pm ETSam Altman, CEO of ChatGPT creator OpenAI, touted the benefits of AI and acknowledged potential downsides of the technology during a Senate...

Hoje, às 10:55