Generate image variations using DALL·E 2
[imageVariation,httpResponse] = createVariation(model,image)
___ = createVariation(___,Name=Value)
[imageVariation,httpResponse] = createVariation(model,image)
generates variations from an image using the OpenAI® image generation model DALL·E 2.
___ = createVariation(___,Name=Value)
specifies additional options using one or more name-value arguments.
First, specify the OpenAI API key as an environment variable and save it to a file called ".env"
. Next, load the environment file using the loadenv
function.
loadenv(".env")
Connect to the OpenAI Images API. By default, the model is DALL·E 2.
model = openAIImages
model =
openAIImages with properties:
ModelName: "dall-e-2"
TimeOut: 10
Generate and display an image based on a natural language prompt.
frogImage = generate(model,"A small frog wearing spectacles.");
imshow(frogImage{1})
Save the image to a PNG (*.png) file.
imwrite(frogImage{1},"frog.png")
Create four different variations of the image.
[imageVariations,httpResponse] = createVariation(model,"frog.png",NumImages=4)
1 | |
---|---|
1 | 1024x1024x3 uint8 |
2 | 1024x1024x3 uint8 |
3 | 1024x1024x3 uint8 |
4 | 1024x1024x3 uint8 |
httpResponse =
ResponseMessage with properties:
StatusLine: 'HTTP/1.1 200 OK'
StatusCode: OK
Header: [1x18 matlab.net.http.HeaderField]
Body: [1x1 matlab.net.http.MessageBody]
Completed: 0
Show the image variations in a montage.
montage(imageVariations)
openAIImages
object
Image generation model, specified as an openAIImages
object. The model name model.ModelName
must be "dall-e-2"
.
string scalar | character vector
Input image, specified as a PNG (*.png) file. The size of the image must be less than 4MB and the image must be square.
1
(default) | positive integer less than or equal to 10
Specify the number of images to generate.
"1024x1024"
(default) | "256x256"
| "512x512"
Size of the generated image in pixels.
cell array of numerical matrices
Images that the model generates, returned as a cell array with NumImages
elements. Each element of the cell array contains a generated image specified as an RGB images of size Size
. For example, if you specify Size="1024x1024"
, then the generated images have size 1024x1024x3
.
matlab.net.http.ResponseMessage
object
Response message returned by the server, specified as a matlab.net.http.ResponseMessage
object.
openAIImages
| generate
| edit
Copyright 2024 The MathWorks, Inc.