OpenAI
How to create an image with OpenAI
Create imageBeta
POST
Creates an image given a prompt.
Request body
prompt
string
Required
A text description of the desired image(s). The maximum length is 1000 characters.
n
integer
Optional
Defaults to 1
The number of images to generate. Must be between 1 and 10.
size
string
Optional
Defaults to 1024×1024
The size of the generated images. Must be one of 256×256, 512×512, or 1024×1024.
response_format
string
Optional
Defaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
user
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
import os
import openai
openai.api_key = os.getenv(“OPENAI_API_KEY”)
openai.Image.create(
prompt=”A cute baby sea otter”,
n=2,
size=”1024×1024″
)
{
“prompt”: “A cute baby sea otter”,
“n”: 2,
“size”: “1024×1024”
}
{
“created”: 1589478378,
“data”: [
{
“url”: “https://…”
},
{
“url”: “https://…”
}
]
}
Create image editBeta
POST
Creates an edited or extended image given an original image and a prompt.
Request body
image
string
Required
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.
mask
string
Optional
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.
prompt
string
Required
A text description of the desired image(s). The maximum length is 1000 characters.
n
integer
Optional
Defaults to 1
The number of images to generate. Must be between 1 and 10.
size
string
Optional
Defaults to 1024×1024
The size of the generated images. Must be one of 256×256, 512×512, or 1024×1024.
response_format
string
Optional
Defaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
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=”1024×1024″
)
{
“created”: 1589478378,
“data”: [
{
“url”: “https://…”
},
{
“url”: “https://…”
}
]
}
Create image variationBeta
POST
Creates a variation of a given image.
Request body
image
string
Required
The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
n
integer
Optional
Defaults to 1
The number of images to generate. Must be between 1 and 10.
size
string
Optional
Defaults to 1024×1024
The size of the generated images. Must be one of 256×256, 512×512, or 1024×1024.
response_format
string
Optional
Defaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
import os
import openai
openai.api_key = os.getenv(“OPENAI_API_KEY”)
openai.Image.create_variation(
image=open(“otter.png”, “rb”),
n=2,
size=”1024×1024″
)
{
“created”: 1589478378,
“data”: [
{
“url”: “https://…”
},
{
“url”: “https://…”
}
]
}