Image Generation

Learn how to generate or manipulate images with DALL·E.

Introduction

The Images API provides three methods for interacting with images:

  • Creating images from scratch based on a text prompt (DALL·E 3 and DALL·E 2)
  • Creating edited versions of images by having the model replace some areas of a pre-existing image, based on a new text prompt (DALL·E 2 only)
  • Creating variations of an existing image (DALL·E 2 only)

Usage

Generations

The image generations endpoint allows you to create an original image given a text prompt. When using DALL·E 3, images can have a size of 1024x1024, 1024x1792 or 1792x1024 pixels.

Generate an image

import OpenAI from "openai";
const openai = new OpenAI();

const response = await openai.images.generate({
  model: "dall-e-3",
  prompt: "a white siamese cat",
  n: 1,
  size: "1024x1024",
});

console.log(response.data[0].url);

Edits (DALL·E 2 only)

The image edits endpoint allows you to edit or extend an image by uploading an image and mask indicating which areas should be replaced. The transparent areas of the mask indicate where the image should be edited.

Edit an image

import OpenAI from "openai";
const openai = new OpenAI();

const response = await openai.images.edit({
  model: "dall-e-2",
  image: fs.createReadStream("sunlit_lounge.png"),
  mask: fs.createReadStream("mask.png"),
  prompt: "A sunlit indoor lounge area with a pool containing a flamingo",
  n: 1,
  size: "1024x1024"
});

console.log(response.data[0].url);

Variations (DALL·E 2 only)

The image variations endpoint allows you to generate a variation of a given image.

Generate an image variation

import OpenAI from "openai";
const openai = new OpenAI();

const response = await openai.images.createVariation({
  model: "dall-e-2",
  image: fs.createReadStream("corgi_and_cat_paw.png"),
  n: 1,
  size: "1024x1024"
});

console.log(response.data[0].url);

Content Moderation

Prompts and images are filtered based on our content policy, returning an error when a prompt or image is flagged.

Tips & Best Practices

Prompting

With DALL·E 3, the model automatically enhances prompts for better results. For literal prompts, add: "I NEED to test how the tool works with extremely simple prompts. DO NOT add any detail, just use it AS-IS:"

Image Requirements

For edits and variations, images must be square PNG files less than 4MB. The mask must have the same dimensions as the original image.