Practical no3
Practical no3
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.
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.
The following Python script demonstrates how to generate images using the DALL·E 2 API:
python
CopyEdit
import openai
# Example usage
prompt = "A futuristic city skyline at sunset, with flying cars and glowing
neon lights."
image_url = generate_image(prompt)
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.
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
import tkinter as tk
openai.api_key = "YOUR_API_KEY"
response = openai.Image.create(
prompt=prompt,
n=n,
size=size
img_data = requests.get(url).content
handler.write(img_data)
print(f"Downloaded: {save_path}")
image=image_file,
n=n,
size=size
image = Image.open(image_path).convert("RGBA")
draw = ImageDraw.Draw(mask)
mask.save(mask_path)
response = openai.Image.create_edit(
image=image_file,
mask=mask_file,
prompt=prompt,
n=1,
size=size
return response['data'][0]['url']
# Batch generation
download_image(url, filename)
# Simple GUI
def launch_gui():
def generate_and_download():
prompt = prompt_entry.get()
count = int(count_entry.get())
download_image(url, filename)
root = tk.Tk()
count_entry.insert(0, "1")
count_entry.grid(row=1, column=1)
root.mainloop()
# create_mask("generated_image.png", "auto_mask.png")
# download_image(edited_url, "edited_image.png")
# 4. Launch GUI
launch_gui()