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