[2024] How to generate an image with DALLE-3 API
1 min readJan 25, 2024
This article gives you the simplest and most useful code for using DALLE-3 API for the first time.
▼Install modules
pip install openai
pip install requests
▼Code
- dalle.py
from openai import OpenAI
import requests
import os
prompt_str = "cute dog"
client = OpenAI(api_key="YOUR-OPENAI-API-KEY")
response = client.images.generate(
model="dall-e-3",
prompt=prompt_str,
size="1024x1024",
quality="standard", # /"hd"
n=1,
)
image_url = response.data[0].url
print(image_url)
url_parts = image_url.split('?')
file_name = url_parts[0].split('/')[-1]
print('file_name : ', file_name)
### Download the image
response = requests.get(image_url)
if response.status_code == 200:
with open(file_name, "wb") as f:
f.write(response.content)
print("The image was downloaded and saved.")
else:
print("Image download failed.")
▼Run
python dalle.py
You can get the image link.
▼Reference and more details:
- Official Document
https://platform.openai.com/docs/guides/images/usage?context=node
Congratulations!
I hope this article can be even just a little helpful to you.!
Happy Developing!
KASATA | Engineer and Entrepreneur