Skip to content

Commit 0de8366

Browse files
authored
Master OpenAI's DALL.E API in 20 Minutes
1 parent 7c8a804 commit 0de8366

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

dalle_api.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# OpenAI Image Generation API: https://platform.openai.com/docs/guides/images/usage
2+
# A good reading: https://www.makeuseof.com/generate-images-using-openai-api-dalle-python/
3+
# A dall-e prompt book: https://dallery.gallery/the-dalle-2-prompt-book/
4+
5+
# Import modules
6+
import os
7+
from dotenv import load_dotenv
8+
from openai import OpenAI
9+
import requests
10+
from PIL import Image
11+
12+
13+
# Load environment variables
14+
load_dotenv()
15+
16+
17+
# Create an OpenAI client
18+
client = OpenAI()
19+
20+
21+
# Define a class for image generation
22+
class ImageGenerator:
23+
# Constructor
24+
def __init__(self):
25+
self.openai_api_key = os.environ["OPENAI_API_KEY"]
26+
27+
# Generate images
28+
def generate_images(self, img_prompt, img_size="1024x1024", img_count=1, img_model='dall-e-2'):
29+
if self.openai_api_key:
30+
# Generate images
31+
response = client.images.generate(
32+
model=img_model,
33+
prompt = img_prompt,
34+
size = img_size,
35+
n = img_count
36+
)
37+
self.images = response.data
38+
39+
# Get the URLs of the generated images
40+
self.image_urls = [image.url for image in self.images]
41+
print(self.image_urls)
42+
return self.image_urls
43+
else:
44+
print("OpenAI API key not found")
45+
46+
# Download the images
47+
def download_images(self, folder, img_names):
48+
for img_url, img_name in zip(self.image_urls, img_names):
49+
img = requests.get(img_url)
50+
with open("{}/{}.png".format(folder, img_name), "wb") as f:
51+
f.write(img.content)
52+
53+
# Edit the image
54+
# Note: Use the following tool to prepare the input image and the mask: https://labs.openai.com/editor
55+
# Use Upload button to upload the edited image, use Erase button to create a mask, and use Download button to download the edited image
56+
def edit_images(self, folder, img_name, mask_name, img_prompt, img_count=1, img_size="512x512", img_model='dall-e-2'):
57+
if self.openai_api_key:
58+
response = client.images.edit(
59+
image = open("{}/{}.png".format(folder, img_name), "rb"),
60+
mask = open("{}/{}.png".format(folder, mask_name), "rb"),
61+
prompt = img_prompt,
62+
n = img_count,
63+
size = img_size,
64+
)
65+
self.images = response.data
66+
self.image_urls = [image.url for image in self.images]
67+
print(self.image_urls)
68+
return self.image_urls
69+
else:
70+
print("OpenAI API key not found")
71+
72+
# Generate a variation of an existing image
73+
def vary_images(self, folder, img_name, img_count=1, img_size="1024x1024", img_model='dall-e-2'):
74+
if self.openai_api_key:
75+
response = client.images.create_variation(
76+
image = open("{}/{}.png".format(folder, img_name), "rb"),
77+
n = img_count,
78+
size = img_size,
79+
model = img_model
80+
)
81+
self.images = response.data
82+
self.image_urls = [image.url for image in self.images]
83+
print(self.image_urls)
84+
return self.image_urls
85+
else:
86+
print("OpenAI API key not found")
87+
88+
89+
# Instantiate the class
90+
imageGen = ImageGenerator()
91+
92+
93+
# Generate images
94+
# imageGen.generate_images(
95+
# img_prompt = "Film still, extreme wide shot of an elephant alone on the savannah, extreme long shot",
96+
# img_count = 2,
97+
# img_size = '1024x1024'
98+
# )
99+
100+
101+
# Download the images
102+
# imageGen.download_images(
103+
# folder="images",
104+
# img_names=["elephant1", "elephant2"])
105+
106+
107+
# Inpainting
108+
# Note: Use the following tool to prepare the input image and the mask: https://labs.openai.com/editor
109+
# imageGen.edit_images(
110+
# folder = "images_edits",
111+
# img_name = "tower",
112+
# mask_name = "tower_masked",
113+
# img_prompt = "A university tower with a blue sky and a hot air balloon",
114+
# img_count = 2,
115+
# img_size = '1024x1024'
116+
# )
117+
118+
119+
# # Create a variation of an existing image
120+
imageGen.vary_images(
121+
folder = "images",
122+
img_name = "elephant1",
123+
img_count = 2,
124+
img_size = '1024x1024'
125+
)

0 commit comments

Comments
 (0)