Skip to main content

Dall-E Image Generator

OpenAI Dall-E are text-to-image models developed by OpenAI using deep learning methodologies to generate digital images from natural language descriptions, called "prompts".

This notebook shows how you can generate images from a prompt synthesized using an OpenAI LLM. The images are generated using Dall-E, which uses the same OpenAI API key as the LLM.

# Needed if you would like to display images in the notebook
%pip install --upgrade --quiet opencv-python scikit-image
import os

from langchain_openai import OpenAI

os.environ["OPENAI_API_KEY"] = "<your-key-here>"

API Reference:

Run as a chain

from langchain.chains import LLMChain
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
template="Generate a detailed prompt to generate an image based on the following description: {image_desc}",
chain = LLMChain(llm=llm, prompt=prompt)
image_url = DallEAPIWrapper().run("halloween night at a haunted museum"))
# You can click on the link above to display the image
# Or you can try the options below to display the image inline in this notebook

import google.colab

except ImportError:
IN_COLAB = False

from google.colab.patches import cv2_imshow # for image display
from skimage import io

image = io.imread(image_url)
import cv2
from skimage import io

image = io.imread(image_url)
cv2.imshow("image", image)
cv2.waitKey(0) # wait for a keyboard input

Run as a tool with an agent

from langchain.agents import initialize_agent, load_tools

tools = load_tools(["dalle-image-generator"])
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
output ="Create an image of a halloween night at a haunted museum")

> Entering new AgentExecutor chain...
 What is the best way to turn this description into an image?
Action: Dall-E Image Generator
Action Input: A spooky Halloween night at a haunted museum

Thought: With the image generated, I can now make my final answer.
Final Answer: An image of a Halloween night at a haunted museum can be seen here:

> Finished chain.

Help us out by providing feedback on this documentation page: