File size: 1,193 Bytes
70e3e61 1c681f7 70e3e61 1c681f7 70e3e61 1c681f7 |
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 |
"""Generate an image using the DALL-E API
Keyword arguments:
prompt -- The prompt to generate the image from
Return: An image saved in a .png file
"""
import io
from openai import OpenAI
from PIL import Image
import requests
from config import OPENAI_API_KEY
def generate_img_with_dalle(prompt="", api_key=OPENAI_API_KEY):
"""Generate an image using the DALL-E API"""
client = OpenAI(api_key=api_key)
# DALL-E model parameters
size = '1024x1024' # Choose between '1024x1024', '512x512', '256x256'
quality = 'hd' # Choose between 'standard', 'hd'
# Generate image using DALL-E
print('Creating DALLE image...')
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size=size,
quality=quality,
n=1,
response_format="url")
image_url = response.data[0].url
# Download and save the image
print('Saving image...')
response = requests.get(image_url, timeout=30)
img = Image.open(io.BytesIO(response.content))
img.save('output_img/dalle_generated_img.png') # Save the image as a .png file
print('Image saved in output_img/dalle_generated_img.png')
return "output_img/dalle_generated_img.png"
|