ad
ad

AI Image Generator with Python, OpenAI, and DALL-E 2 [tutorial for beginners]

Science & Technology


Introduction

Welcome back to the channel! In today's tutorial, we'll explore how to harness the power of Python and the OpenAI API to generate images using artificial intelligence. Specifically, we'll focus on DALL-E 2, where you can provide a text prompt, and the AI will create a corresponding image. For instance, if you input "a dog wearing a superhero cape," the AI will generate a unique image of that scenario.

Getting Started with OpenAI API

First, head over to platform.openai.com to create your OpenAI account. Once your account is set up, navigate to platform.openai.com/account/api-keys to generate your API key. If you've previously created an API key, you can reuse it or generate a new one.

Storing the API Key

For security reasons, it's essential to store your API key as an environment variable rather than hardcoding it in your code. Here’s how to do this on Windows:

  1. Open System Properties.
  2. Go to the Advanced tab.
  3. Click on Environment Variables.
  4. Create a new environment variable named OPENAI_API_KEY.

By following these steps, your API key will be securely stored.

Writing the Python Program

Now that we have our API key set up, let's proceed with writing the Python code. You can use any code editor of your choice; in this tutorial, we will utilize Visual Studio Code. Create a new file called main.py, and we will import the necessary libraries:

import os
import openai

To install the OpenAI library, run the following command in your terminal:

pip install openai

Once the library is installed, we will retrieve the API key from the environment variable:

openai.api_key = os.getenv("OPENAI_API_KEY")

Getting User Input

Next, we ask the user for a prompt to guide the image generation:

user_prompt = input("Write your prompt for DALL-E 2: ")

Sending the Request to OpenAI

With the user's prompt in hand, it's time to send a request to the OpenAI API:

response = openai.Image.create(
    prompt=user_prompt,
    n=1,
    size="1024x1024"
)

Retrieving the Image

To access the generated image, we extract its URL from the response:

image_url = response['data'][0]['url']
print(f"Image URL: (image_url)")

When you execute main.py, it will prompt you to input an image description. Upon entering a description such as "a brown husky wearing a red cap," the program will generate a corresponding image, and you'll receive a URL that links to this image.

Downloading the Image

If you want to download the generated image locally, you can do so with a few additional lines of code. First, import the necessary libraries:

import urllib.request
from datetime import datetime

Creating a unique filename based on the current timestamp will ensure that images don't overwrite each other:

file_name = f"image_(datetime.now().strftime('%Y%m%d_%H%M%S')).png"
urllib.request.urlretrieve(image_url, file_name)

When you rerun the program with a new prompt, the image will be downloaded to your local machine with a unique name.

Final Thoughts

Through this tutorial, we've successfully generated images using OpenAI's DALL-E 2 with Python and learned how to download them locally. I encourage you to experiment with different prompts and see what unique images you can create!

Thank you for joining me, and please share your experiences with the OpenAI API in the comments below. I look forward to seeing you in the next video!


Keywords

OpenAI, DALL-E 2, Python, image generation, API key, environment variable, URL, download image, prompt, unique filename.


FAQ

Q1: What is DALL-E 2?
A1: DALL-E 2 is an AI model developed by OpenAI that can generate images based on textual descriptions.

Q2: How do I create an API key for OpenAI?
A2: Create an OpenAI account and navigate to the API keys section in your account settings to generate a new API key.

Q3: Why should I store my API key as an environment variable?
A3: Storing your API key as an environment variable enhances security by preventing it from being hardcoded in your source code.

Q4: Can I generate multiple images at once?
A4: Yes, you can adjust the parameter n in the openai.Image.create call to generate multiple images.

Q5: How can I download a generated image?
A5: Use the urllib.request library in Python to retrieve the image from the URL returned by the OpenAI API and save it locally.