## Dall-E Image Generator

> [OpenAI Dall-E](https://openai.com/dall-e-3) 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 osfrom langchain_openai import OpenAIos.environ["OPENAI_API_KEY"] = "<your-key-here>"
```

## Run as a chain[​](#run-as-a-chain "Direct link to Run as a chain")

```
from langchain.chains import LLMChainfrom langchain_community.utilities.dalle_image_generator import DallEAPIWrapperfrom langchain_core.prompts import PromptTemplatefrom langchain_openai import OpenAIllm = OpenAI(temperature=0.9)prompt = PromptTemplate(    input_variables=["image_desc"],    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(chain.run("halloween night at a haunted museum"))
```

```
'https://oaidalleapiprodscus.blob.core.windows.net/private/org-i0zjYONU3PemzJ222esBaAzZ/user-f6uEIOFxoiUZivy567cDSWni/img-i7Z2ZxvJ4IbbdAiO6OXJgS3v.png?st=2023-08-11T14%3A03%3A14Z&se=2023-08-11T16%3A03%3A14Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-08-10T20%3A58%3A32Z&ske=2023-08-11T20%3A58%3A32Z&sks=b&skv=2021-08-06&sig=/sECe7C0EAq37ssgBm7g7JkVIM/Q1W3xOstd0Go6slA%3D'
```

```
# 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 notebooktry:    import google.colab    IN_COLAB = Trueexcept ImportError:    IN_COLAB = Falseif IN_COLAB:    from google.colab.patches import cv2_imshow  # for image display    from skimage import io    image = io.imread(image_url)    cv2_imshow(image)else:    import cv2    from skimage import io    image = io.imread(image_url)    cv2.imshow("image", image)    cv2.waitKey(0)  # wait for a keyboard input    cv2.destroyAllWindows()
```

```
from langchain.agents import initialize_agent, load_toolstools = load_tools(["dalle-image-generator"])agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)output = agent.run("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 GeneratorAction Input: A spooky Halloween night at a haunted museumhttps://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3DObservation: https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3DThought: 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: https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22> Finished chain.
```

* * *

#### Help us out by providing feedback on this documentation page: