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
The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
The model to use for image generation. Only dall-e-2 is supported at this time.
The number of images to generate. Must be between 1 and 10.
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.
The size of the generated images. Must be one of:
256x256
512x512
1024x1024
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more .
Returns
Returns an ImagesResponse object containing the image variations.
The Unix timestamp (in seconds) of when the image was created.
The list of generated images. The URL of the generated image (when response_format=url). URLs are only valid for 60 minutes.
The base64-encoded JSON of the generated image (when response_format=b64_json).
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 } " )