To generate an image, you need to create an account and API key if you haven’t already. You can learn more about this in my tutorial here: 👇
✅ Recommended: OpenAI API Getting Started
Do you have an API key? Great!
To generate an image with DALL·E copy&paste the following code and replace your API key and the string description of the image to be generated (
import os import openai # Your API Key Here: 👇 openai.api_key = 'sk-XXXXXXXXXXXXXXXXXXXXXXXXXXX' # Your Image Prompt Here: 👇 prompt = "An oil painting of a dancing robot in the style of Monet" response = openai.Image.create( prompt=prompt, n=1, size="256x256", ) print(response["data"]["url"])
The output is a URL from where you can download the image:
You can change the size, e.g., to 1000×1000 pixels — but it’ll cost you more and takes longer:
# ... response = openai.Image.create( prompt=prompt, n=1, size="1024x1024", ) # ...
Here’s the pricing table by the way from the official API page:
|1024×1024||$0.020 / image|
|512×512||$0.018 / image|
|256×256||$0.016 / image|
Yeah, this article just cost me $0.054. 😱
🚀 Recommended: Best 35 Helpful ChatGPT Prompts for Coders (2023)
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.