Image-generation
Image generation
This Markdown file can be "evaluated" with the Raku package "Text::CodeProcessing", [AAp2]. Here is a shell command:
file-code-chunks-eval Image-generation.md Remark: For doing image variations using image files see the document Image-variation-and-edition.md (and its woven version, Image-variation-and-edition_woven.md.)
Here we load the Raku package "WWW::OpenAI", [AAp1]:
use WWW::OpenAI;Images can be generated with the function openai-create-image -- see the section
"Images" of [OAI2].
Here is an example that returns "whole images" in Base64 format:
my @imgResB64 = |openai-create-image(
"raccoon with a sliced onion in the style of Raphael",
model => 'dall-e-3',
n => 1,
size => 'square',
response-format => 'b64_json',
format => 'values',
method => 'tiny');
@imgResB64.map({ '' }).join("\n\n") Remark: The obtained images are put into the woven document via the output of the code cell,
which has the setting results=asis.
The first argument is a prompt used to generate the image(s).
Here are the descriptions of the named arguments (options):
modeltakesWhatever, or one of 'dall-e-2' or 'dall-e-3'ntakes a positive integer, for the number of images to be generatedsizetakes the values according to themodelvalue:'dall-e-2' : '1024x1024', '512x512', '256x256', 'large', 'medium', 'small'
'dall-e-3' : '1024x1024', '1792x1024', '1024x1792', 'square', 'portrait', 'landscape'
response-formattakes the values "url" and "b64_json"methodtakes the values "tiny" and "curl"
Remark: See the model-parameter breakdown in the section below.
Here we generate a few images, get their URLs, and place (embed) the image links using a table:
my @imgRes = |openai-create-image(
"painting of a racoon and onion in the style of Rene Magritte",
response-format => 'url',
n => 2,
size => 'small',
format => 'values',
method => 'tiny');
@imgRes.map({ '' }).join("\n\n") Parameters
As it was mentioned above, the argument "model" can be Whatever of one of "dall-e-2" or "dall-e-3".
Not all parameters that are valid for one of the models are valid or respected by the other -- see the subsection "Create image" of OpenAI's documentation.
Here is a table that shows a breakdown of the model-parameter relationships:
| Parameter | Type | Required/Optional | Default | dall-e-2 | dall-e-3 | Valid Values |
|---|---|---|---|---|---|---|
| prompt | string | Required | N/A | āļø | āļø | Maximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3 |
| model | string | Optional | dall-e-2 | āļø | āļø | N/A |
| n | integer or null | Optional | 1 | āļø | āļø (only n=1) | Must be between 1 and 10. For dall-e-3, only n=1 is supported |
| quality | string | Optional | standard | ā | āļø | N/A |
| response_format | string or null | Optional | url | āļø | āļø | Must be one of url or b64_json |
| size | string or null | Optional | 1024x1024 | āļø (256x256, 512x512, 1024x1024) | āļø (1024x1024, 1792x1024, 1024x1792) | Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models |
| style | string or null | Optional | vivid | ā | āļø | Must be one of vivid or natural |
References
Articles
[AA1] Anton Antonov, "Connecting Mathematica and Raku", (2021), RakuForPrediction at WordPress.
Packages
[AAp1] Anton Antonov, WWW::OpenAI Raku package, (2023), GitHub/antononcube.
[AAp2] Anton Antonov, Text::CodeProcessing, (2021), GitHub/antononcube.
[OAI1] OpenAI Platform, OpenAI platform.
[OAI2] OpenAI Platform, OpenAI documentation.
[OAIp1] OpenAI, OpenAI Python Library, (2020), GitHub/openai.