Function Resource

Function Repository Resource:

GenerateImageFromText

Source Notebook

Generate an image from a text prompt using artificial intelligence

Contributed by: Arnoud Buzing

ResourceFunction["GenerateImageFromText"][text]

generates an image from a text prompt using artificial intelligence.

Details and Options

The text prompt has to be a string in the English language.
The GenerateImageFromText function requires a user access token from Hugging Face. Visit their security tokens web page for information on how to obtain this token. Use SystemCredential["HuggingFace"]="your_token_here" to make this token permanently available to your Wolfram Language sessions.

Examples

Basic Examples (2) 

Set your user access token using SystemCredential to make it permanently available to your Wolfram Language sessions. Check the "Details and Options" section for more details:

In[1]:=
SystemCredential[
  "HuggingFace"] = "replace_with_your_user_access_token"

Generate an image based on a text prompt:

In[2]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A dog in a grassy field"]
Out[2]=

Scope (5) 

Use a more detailed prompt to get more specific content:

In[3]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A snow capped mountain with a blooming cherry tree in the foreground"]
Out[3]=

Add a styling prompt to change the result:

In[4]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A snow capped mountain with a blooming cherry tree in the foreground, using water color"]
Out[4]=

Use styling based on a specific well-known painter:

In[5]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A snow capped mountain with a blooming cherry tree in the foreground, in the style of Van Gogh"]
Out[5]=

Adding more prompts can generate more complex images:

In[6]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A snow capped mountain with a blooming cherry tree in the foreground, towering clouds, dramatic view point, in the style of Rembrandt"]
Out[6]=

Adding color and material prompts also change appearance:

In[7]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A snow capped mountain with a blooming cherry tree in the foreground, towering clouds, dramatic view point, in the style of Rembrandt, muted colors, charcoal"]
Out[7]=

Possible Issues (2) 

When the service is not available, or not yet available, a message is returned:

In[8]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A dog in a grassy field"]
Out[8]=

When an invalid token is provided, a message is returned:

In[9]:=
ResourceFunction[
CloudObject[
  "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]]["A dog in a grassy field"]
Out[9]=

Neat Examples (1) 

Generate variations by appending an otherwise inconsequential integer to the string:

In[10]:=
Grid[
 Table[
  ResourceFunction[
CloudObject[
    "https://www.wolframcloud.com/obj/arnoudbuzing/DeployedResources/Function/GenerateImageFromText"]][
   "A snow capped mountain with a blooming cherry tree in the foreground, in the style of Van Gogh " <> ToString[3 i + j]], {i, 3}, {j, 3}]
 ]
Out[10]=