Images
Image Variations
POST
/
images
/
variations
Image Variations
curl --request POST \
--url https://api.example.com/images/variationsimport requests
url = "https://api.example.com/images/variations"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://api.example.com/images/variations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/images/variations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/images/variations"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/images/variations")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/images/variations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body{
"created": 123,
"data": [
{
"url": "<string>",
"b64_json": "<string>"
}
]
}Creates a variation of a given image.
This endpoint only supports
dall-e-2.
Method Signature
client.images.create_variation(
image=open("image.png", "rb"),
n=2,
size="1024x1024"
)
Parameters
file
required
The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
string
The model to use for image generation. Only
dall-e-2 is supported at this time.integer
The number of images to generate. Must be between 1 and 10.
string
The format in which the generated images are returned. Must be one of
url or b64_json. URLs are only valid for 60 minutes after the image has been generated.string
The size of the generated images. Must be one of:
256x256512x5121024x1024
string
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
Returns
Returns anImagesResponse object containing the image variations.
integer
The Unix timestamp (in seconds) of when the image was created.
array
Examples
Create variations with URL response
from openai import OpenAI
client = OpenAI()
response = client.images.create_variation(
image=open("image.png", "rb"),
n=3,
size="1024x1024"
)
for i, image in enumerate(response.data):
print(f"Variation {i + 1}: {image.url}")
Create variations with base64 response
import base64
from openai import OpenAI
client = OpenAI()
response = client.images.create_variation(
image=open("original.png", "rb"),
n=2,
size="512x512",
response_format="b64_json"
)
for i, image in enumerate(response.data):
image_data = base64.b64decode(image.b64_json)
with open(f"variation_{i}.png", "wb") as f:
f.write(image_data)
print(f"Saved variation_{i}.png")
Multiple smaller variations
from openai import OpenAI
client = OpenAI()
# Generate 10 small variations for quick preview
response = client.images.create_variation(
image=open("source.png", "rb"),
n=10,
size="256x256"
)
print(f"Generated {len(response.data)} variations")
for i, image in enumerate(response.data):
print(f"Variation {i + 1}: {image.url}")
Image Variations
curl --request POST \
--url https://api.example.com/images/variationsimport requests
url = "https://api.example.com/images/variations"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://api.example.com/images/variations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/images/variations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/images/variations"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/images/variations")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/images/variations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body{
"created": 123,
"data": [
{
"url": "<string>",
"b64_json": "<string>"
}
]
}