-
Notifications
You must be signed in to change notification settings - Fork 35
/
image_generator.py
37 lines (30 loc) · 977 Bytes
/
image_generator.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
import openai
import os
import requests
from datetime import datetime
import time
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_images(image_prompts):
images = []
for prompt in image_prompts:
response = openai.Image.create(
prompt=prompt,
n=1,
size="1024x1024",
)
if response.data:
image_url = response.data[0].url
images.append(image_url)
else:
print(f"Error generating image for prompt '{prompt}'")
return []
time.sleep(12)
return images
def save_images(images, timestamp):
for idx, image_url in enumerate(images):
download_image(image_url, f"image_{timestamp}_{idx}.png")
def download_image(url, filename):
response = requests.get(url)
with open(filename, "wb") as f:
f.write(response.content)