Skip to content

Latest commit

 

History

History
82 lines (58 loc) · 1.83 KB

UsingDALLEToEditImages.md

File metadata and controls

82 lines (58 loc) · 1.83 KB

Using DALL·E™ to Edit Images

To run the code shown on this page, open the MLX file in MATLAB®: mlx-scripts/UsingDALLEToEditImages.mlx

This example shows how to generate and edit images using the openAIImages object.

To run this example, you need a valid OpenAI™ API key. Creating images using DALL•E may incur a fee.

loadenv(".env")
addpath("../..")

We want to load images files relative to the project directory below:

projectDir = fileparts(which("openAIImages"));

Generate image variations

Use the image variation feature in DALL•E 2.

mdl = openAIImages(ModelName="dall-e-2");

Show the image to get variations for.

imagePath = fullfile(projectDir,"examples","images","bear.png");
figure
imshow(imagePath)

figure_0.png

Generate variations for that image.

[images,resp] = createVariation(mdl, imagePath, NumImages=4);
if ~isempty(images)
    tiledlayout('flow')
    for ii = 1:numel(images)
        nexttile
        imshow(images{ii})
    end
else
    disp(resp.Body.Data.error)
end

figure_1.png

Edit an Image with DALL·E

Use an image containing a mask to apply modifications to the masked area.

maskImagePath = fullfile(projectDir,"examples","images","mask_bear.png");
figure
imshow(maskImagePath)

figure_2.png

Add a swan to the masked area using the function edit.

[images,resp] = edit(mdl, imagePath, "Swan", MaskImagePath=maskImagePath);
if isfield(resp.Body.Data,'data')
    figure
    imshow(images{1});
else
    disp(resp.Body.Data.error)
end

figure_3.png

Copyright 2024 The MathWorks, Inc.