openai-image-generator/main.py

27 lines
854 B
Python

import openai
import datetime
import requests
import os
openai.api_key = "API_TOKEN"
promtInput = input("Your prompt: ")
response = openai.Image.create(
prompt = promtInput,
n = 4,
size = "1024x1024"
)
# Create image directory
os.mkdir("images")
# Current time
current_time = datetime.datetime.now()
# Format current time to a filename and save it to disk
for i in range(4):
image_url = response['data'][i]['url']
formatDateTime = str(current_time.year) + "-" + str(current_time.month) + "-" + str(current_time.day) + "_" + str(current_time.hour) + "-" + str(current_time.minute) + "-" + str(current_time.second) + "_Image-" + str(i+1) + ".png"
img_data = requests.get(image_url).content
with open("images/" + formatDateTime, 'wb') as handler:
handler.write(img_data)
print("Image " + str(i+1) + " saved.")