Skip to content

DALL-E 3 API TUTORIAL BY DATACAMP

Relevant libraries

!pip install --upgrade openai
import os
from openai import OpenAI
from IPython.display import Image
OPENAI_API_KEY="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"

os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
client = OpenAI()
# Helper function to generate the images 

def get_image_from_DALL_E_3_API(user_prompt, 
                                image_dimension="1024x1024", 
                                image_quality="hd", 
                                model="dall-e-3", 
                                nb_final_image=1):
    
    response = client.images.generate(
      model = model,
      prompt = user_prompt,
      size = image_dimension,
      quality = image_quality,
      n=nb_final_image,
    )

    image_url = response.data[0].url
    
    display(Image(url=image_url))

Simple Prompt

puppy_prompt = "Create an image of a cute brown puppy sitting in a green meadow under a clear blue sky."

get_image_from_DALL_E_3_API(puppy_prompt)

Advanced Prompts

education_prompt = "Generate an illustration of the solar system with planets orbiting the sun, labeled in English, for a grade school science textbook"

get_image_from_DALL_E_3_API(education_prompt)
advertising_prompt = "Create an image of a family enjoying a picnic in a futuristic city park, with skyscrapers in the background and a clear blue sky, to be used in a campaign promoting eco-friendly urban living."

get_image_from_DALL_E_3_API(advertising_prompt)