1
+ %% Using DALL·E to Edit Images
2
+ % This example shows how to generate and edit images using the |openAIImages|
3
+ % object.
4
+ %
5
+ % To run this example, you need a valid OpenAI API key. Creating images using
6
+ % DALL-E may incur a fee.
7
+
8
+ loadenv(" .env" )
9
+ addpath(' ..' )
10
+ %% Generate image variations
11
+ % Use the image variation feature in DALL·E 2.
12
+
13
+ mdl = openAIImages(ModelName = " dall-e-2" );
14
+ %%
15
+ % Show the image to get variations for.
16
+
17
+ imagePath = fullfile(' examples' ,' images' ,' bear.png' );
18
+ figure
19
+ imshow(imagePath )
20
+ %%
21
+ % Benerate variations for that image.
22
+
23
+ [images ,resp ] = createVariation(mdl , imagePath , NumImages= 4 );
24
+ if ~isempty(images )
25
+ tiledlayout(' flow' )
26
+ for ii = 1 : numel(images )
27
+ nexttile
28
+ imshow(images{ii })
29
+ end
30
+ else
31
+ disp(resp .Body .Data .error )
32
+ end
33
+ %% Edit an Image with DALL·E
34
+ % Use an image containing a mask to apply modifications to the masked area.
35
+
36
+ imagePath = fullfile(' examples' ,' images' ,' mask_bear.png' );
37
+ figure
38
+ imshow(imagePath )
39
+ %%
40
+ % Add a swan to the masked area using the function |edit|.
41
+
42
+ [images ,resp ] = edit(mdl , imagePath , " Swan" , MaskImagePath= maskImagePath );
43
+ if isfield(resp .Body .Data ,' data' )
44
+ figure
45
+ imshow(images{1 });
46
+ else
47
+ disp(resp .Body .Data .error )
48
+ end
49
+ %%
50
+ % _Copyright 2024 The MathWorks, Inc._
0 commit comments