Core API
Image Generation with DALL-E
Generate and edit images programmatically using the DALL-E 3 API.
DALL-E 3
DALL-E 3 is OpenAI's latest image generation model. It produces high-quality images from text descriptions and is much better at following complex prompts than previous versions.
Key Features
- Prompt understanding: Follows detailed, nuanced prompts
- Sizes: 1024x1024, 1024x1792, 1792x1024
- Quality: standard or hd
- Style: natural or vivid
- Revised prompts: DALL-E automatically rewrites prompts for better results (disable with
dall-e-3version)
Image Editing (DALL-E 2)
DALL-E 2 (not 3) supports:
- Variations: Create variations of an existing image
- Inpainting: Edit specific areas of an image using a mask
Example
javascript
import OpenAI from 'openai';
import fs from 'fs';
import https from 'https';
const openai = new OpenAI();
// Generate an image
async function generateImage(prompt) {
const response = await openai.images.generate({
model: "dall-e-3",
prompt: prompt,
n: 1,
size: "1024x1024",
quality: "standard",
style: "vivid", // or "natural"
});
const imageUrl = response.data[0].url;
const revisedPrompt = response.data[0].revised_prompt;
console.log("Image URL:", imageUrl);
console.log("Revised prompt:", revisedPrompt);
return imageUrl;
}
// Download image to file
function downloadImage(url, filename) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filename);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => { file.close(); resolve(); });
}).on('error', reject);
});
}
// Generate and save
async function createAndSave(prompt, filename) {
const url = await generateImage(prompt);
await downloadImage(url, filename);
console.log(`Saved to ${filename}`);
}
// Batch generation
async function generateBatch(prompts) {
const results = await Promise.all(
prompts.map((prompt, i) =>
generateImage(prompt)
.then(url => ({ prompt, url, index: i }))
.catch(err => ({ prompt, error: err.message, index: i }))
)
);
return results;
}
// Example usage
await createAndSave(
"A serene Japanese zen garden at dawn, soft morning light, cherry blossoms, photorealistic",
"zen_garden.png"
);Try it yourself — JAVASCRIPT