0% found this document useful (0 votes)
2 views

Practical no3

The lab manual provides a comprehensive guide on generating AI images using the DALL·E 2 API with Python. It covers prerequisites, required tools, and a step-by-step procedure for setting up the environment, writing scripts, and running the code to generate images based on textual prompts. Additionally, it includes functionalities for image editing, downloading images, and creating a simple GUI for user interaction.

Uploaded by

shrikant gagare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical no3

The lab manual provides a comprehensive guide on generating AI images using the DALL·E 2 API with Python. It covers prerequisites, required tools, and a step-by-step procedure for setting up the environment, writing scripts, and running the code to generate images based on textual prompts. Additionally, it includes functionalities for image editing, downloading images, and creating a simple GUI for user interaction.

Uploaded by

shrikant gagare
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab Manual: Generating AI Images Using DALL·E 2

API with Python

Objective
The objective of this lab is to explore how to generate AI-driven images using the DALL·E 2
API through Python programming. By the end of this lab, students will gain hands-on
experience in interacting with OpenAI's API, submitting prompts to generate images, and
managing the API responses.

Prerequisites
 Basic knowledge of Python programming.
 A valid OpenAI API key for access to the DALL·E 2 model.
 Familiarity with the concept of image generation from textual descriptions.

Required Tools and Libraries


 Python 3.x: The programming language used to interface with the API.
 OpenAI Python Client Library: This is the library used to interact with OpenAI's
API, which can be installed via pip.
 API Key: A valid OpenAI API key is required to authenticate requests.
 Internet Connection: Required for sending API requests and fetching generated
images.

Understanding DALL·E 2 API


DALL·E 2 is a deep learning model developed by OpenAI capable of generating high-quality
images from text descriptions. This API allows users to input a prompt, and the model
generates an image based on that description. It can handle complex and creative prompts,
producing images that range from photorealistic to artistic renderings.

Step-by-Step Procedure
Setting Up the Environment

To begin, install the required libraries and obtain the necessary API key:

1. Install Python OpenAI Client: Ensure the OpenAI client is installed by running the
following in the terminal or command prompt:

bash
CopyEdit
pip install openai
2. Obtain OpenAI API Key: If you don't have an API key, sign up for an OpenAI
account and retrieve the key from OpenAI’s API dashboard.

Writing the Python Script

The following Python script demonstrates how to generate images using the DALL·E 2 API:

python
CopyEdit
import openai

# Set your OpenAI API key


openai.api_key = 'your-api-key-here'

# Function to generate an image with DALL·E 2 API


def generate_image(prompt):
response = openai.Image.create(
prompt=prompt,
n=1, # Number of images to generate
size="1024x1024", # Image size (options: 256x256, 512x512,
1024x1024)
)

# Get the image URL from the response


image_url = response['data'][0]['url']
return image_url

# Example usage
prompt = "A futuristic city skyline at sunset, with flying cars and glowing
neon lights."
image_url = generate_image(prompt)

print(f"Image URL: {image_url}")

Explanation of the Code:

 openai.api_key: The API key is used to authenticate and allow access to OpenAI’s
API.
 openai.Image.create: This method is called to generate the image. It requires a
textual prompt and can specify the size of the image (e.g., 1024x1024).
 prompt: The descriptive text input that informs the model what kind of image to
generate. You can modify the prompt for different results.
 Image URL: The URL where the generated image can be viewed or downloaded.

Running the Code

1. Execute the Script: After saving the Python script, run the following command in the
terminal to execute it:

bash
CopyEdit
python generate_image.py

2. Image URL: After execution, the script will print the URL of the generated image.
This URL points to the location where the image is hosted by OpenAI.
Accessing the Generated Image

Once the image has been generated, a URL to the image will be displayed in the terminal,
such as:

mathematica
CopyEdit
Image URL: https://... [image_url]

To view the image, simply paste the URL into a browser. You can then save or share the
image as required.

Conclusion
This lab successfully demonstrated how to generate images using OpenAI’s DALL·E 2 API.
Students learned how to send a prompt to the model, retrieve the image URL, and view the
generated image. This powerful tool has vast applications in various fields such as graphic
design, entertainment, marketing, and more, enabling the creation of highly customized
visuals.

References
1. OpenAI API Documentation: https://beta.openai.com/docs/
2. DALL·E 2 Introduction by OpenAI: https://openai.com/dall-e-2
3. OpenAI Python Library GitHub: https://github.com/openai/openai-python

Write answer of the following in lab manual

1 What is the main function of DALL·E 2?

2 Explain the role of API keys in cloud services.

3 How does image editing with masks work in DALL·E 2

4 What are the advantages of using tkinter for building GUI

5 How can we handle API errors effectively in Python?

Generate an AI- Image using DALL·E 2 API using Python .


import openai
import requests

from PIL import Image, ImageDraw

import tkinter as tk

from tkinter import filedialog, simpledialog, messagebox

# Set your OpenAI API key

openai.api_key = "YOUR_API_KEY"

# Generate image from prompt

def generate_image(prompt, size="512x512", n=1):

response = openai.Image.create(

prompt=prompt,

n=n,

size=size

return [item['url'] for item in response['data']]

# Download image from URL

def download_image(url, save_path):

img_data = requests.get(url).content

with open(save_path, 'wb') as handler:

handler.write(img_data)

print(f"Downloaded: {save_path}")

# Generate variations of an image

def generate_variation(image_path, size="512x512", n=1):

with open(image_path, "rb") as image_file:


response = openai.Image.create_variation(

image=image_file,

n=n,

size=size

return [item['url'] for item in response['data']]

# Create automatic mask (circle mask as example)

def create_mask(image_path, mask_path):

image = Image.open(image_path).convert("RGBA")

mask = Image.new("RGBA", image.size, (0, 0, 0, 0))

draw = ImageDraw.Draw(mask)

# Draw a white circle (editable)

width, height = image.size

draw.ellipse([(width//4, height//4), (3*width//4, 3*height//4)], fill=(255, 255, 255, 255))

mask.save(mask_path)

print(f"Mask saved at: {mask_path}")

# Edit image with mask

def edit_image(image_path, mask_path, prompt, size="512x512"):

with open(image_path, "rb") as image_file, open(mask_path, "rb") as mask_file:

response = openai.Image.create_edit(

image=image_file,

mask=mask_file,

prompt=prompt,

n=1,
size=size

return response['data'][0]['url']

# Batch generation

def batch_generate(prompt, count=3, save_prefix="batch_image"):

urls = generate_image(prompt, n=count)

for idx, url in enumerate(urls):

filename = f"{save_prefix}_{idx + 1}.png"

download_image(url, filename)

# Simple GUI

def launch_gui():

def generate_and_download():

prompt = prompt_entry.get()

count = int(count_entry.get())

urls = generate_image(prompt, n=count)

for idx, url in enumerate(urls):

filename = f"gui_image_{idx + 1}.png"

download_image(url, filename)

messagebox.showinfo("Done", "Images generated and downloaded!")

root = tk.Tk()

root.title("AI Image Generator")

tk.Label(root, text="Prompt:").grid(row=0, column=0)

prompt_entry = tk.Entry(root, width=50)


prompt_entry.grid(row=0, column=1)

tk.Label(root, text="Number of Images:").grid(row=1, column=0)

count_entry = tk.Entry(root, width=5)

count_entry.insert(0, "1")

count_entry.grid(row=1, column=1)

generate_button = tk.Button(root, text="Generate Images", command=generate_and_download)

generate_button.grid(row=2, column=0, columnspan=2, pady=10)

root.mainloop()

# === Example run ===

# 1. Batch generate images

# batch_generate("A magical forest with glowing mushrooms", count=3)

# 2. Create a mask automatically

# create_mask("generated_image.png", "auto_mask.png")

# 3. Edit image with mask

# edited_url = edit_image("generated_image.png", "auto_mask.png", "Add a unicorn in the center")

# download_image(edited_url, "edited_image.png")

# 4. Launch GUI

launch_gui()

You might also like