Images
Given a prompt and/or an input image, the model will generate a new image.
Related guide: Image generation
Create image Beta
POST https://api.openai.com/v1/images/generations
Creates an image given a prompt.
Request body
promptstringRequired
A text description of the desired image(s). The maximum length is 1000 characters.
nintegerOptionalDefaults to 1
The number of images to generate. Must be between 1 and 10.
sizestringOptionalDefaults to 1024x1024
The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
response_formatstringOptionalDefaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
userstringOptional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
Example request:
curl:
curl https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "A cute baby sea otter",
"n": 2,
"size": "1024x1024"
}'
python:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create(
prompt="A cute baby sea otter",
n=2,
size="1024x1024"
)
node.js:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImage({
prompt: "A cute baby sea otter",
n: 2,
size: "1024x1024",
});
Parameters:
{
"prompt": "A cute baby sea otter",
"n": 2,
"size": "1024x1024"
}
Response:
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}
Create image edit Beta
POST https://api.openai.com/v1/images/edits
Creates an edited or extended image given an original image and a prompt.
Request body
imagestringRequired
The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
maskstringOptional
An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
promptstringRequired
A text description of the desired image(s). The maximum length is 1000 characters.
nintegerOptionalDefaults to 1
The number of images to generate. Must be between 1 and 10.
sizestringOptionalDefaults to 1024x1024
The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
response_formatstringOptionalDefaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
userstringOptional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
Example request:
curl:
curl https://api.openai.com/v1/images/edits \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F image="@otter.png" \
-F mask="@mask.png" \
-F prompt="A cute baby sea otter wearing a beret" \
-F n=2 \
-F size="1024x1024"
python:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create_edit(
image=open("otter.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A cute baby sea otter wearing a beret",
n=2,
size="1024x1024"
)
node.js:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImageEdit(
fs.createReadStream("otter.png"),
fs.createReadStream("mask.png"),
"A cute baby sea otter wearing a beret",
2,
"1024x1024"
);
Response:
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}
Create image variation Beta
POST https://api.openai.com/v1/images/variations
Creates a variation of a given image.
Request body
imagestringRequired
The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
nintegerOptionalDefaults to 1
The number of images to generate. Must be between 1 and 10.
sizestringOptionalDefaults to 1024x1024
The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
response_formatstringOptionalDefaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
userstringOptional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
Example request:
curl:
curl https://api.openai.com/v1/images/variations \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F image="@otter.png" \
-F n=2 \
-F size="1024x1024"
python:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create_variation(
image=open("otter.png", "rb"),
n=2,
size="1024x1024"
)
node.js:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImageVariation(
fs.createReadStream("otter.png"),
2,
"1024x1024"
);
Response:
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}