How to Randomly Set a Part of Prompt from the List of Predefined Words in Fooocus: Wildcard Feature

Imagine you want to generate an image with a pet for the ad of your business (e.g. a pet food store), but you are not really sure what animals look the best for the particular image you have in mind. You decided that you want to pick from:

  • Dog
  • Cat
  • Hamster
  • Ferret

You want to use a prompt:

Photo of a <type of pet> on the grass on a sunny spring day

So for a dog, it would be:

Photo of a dog on the grass on a sunny spring day

The problem is that you don’t want to sit in front of your computer and manually change the prompt each time you want to change the pet type. Rather, you want Fooocus to automatically pick one pet type from the list of these 4 pet types for the prompt and generate some images for each. This way, if you specify 20 images for the number of images to generate, you can walk way from your computer and come back some time later to see the result. Is there a way to do this? Yes, Fooocus supports a feature called wildcard that does this. Using this feature is very easy. Let’s dive in.

Overview of steps

Using the wildcard feature requires three steps:

  • Create a file that contains a list of words to be used in a prompt
  • Specify the placeholder in your prompt. The placeholder is replaced by the word(s) randomly selected from the word list file
  • Set the number of images to generate

Step 1: Create a file that contains a list of words to be used in a prompt

We need a file that contains the list of pet types in this step. The name of the this file is the same as the placeholder that we will be using in our prompt. In our case, let’s use “pet_type” as the placeholder and the name of the file will be pet_type.txt. This file needs to be placed under the Fooocus/wildcards directory as shown below. If you open up this folder using your file viewer application (e.g. Windows Explorer, Mac Finder), you should see that Fooocus already has some pre-defined placeholders:

Let’s create pet_type.txt and save in this folder:

dog
cat
hamster
ferret

Step 2: Specify the placeholder in your prompt

Now let’s move on to how to specify this word list in your prompt. You will put two underscore characters before and after the placeholder when you refer it in your prompt. Since we are using pet_type, we need to put __pet_type__.

Here is the complete prompt:

Photo of a __pet_type__ on the grass on a sunny spring day

Step 3: Set the number of images to generate

Check the Advanced checkbox at the bottom of the screen to display the Setting tab. Then select the total number of images to generate. In this tutorial, 20 is used.

Now you are all set. If you press generate, and go to the console, you should see messages that indicate that wildcard feature is working:

[Fooocus] Processing prompts ...
[Wildcards] processing: Photo of a __pet_type__ on the grass on a sunny spring day
[Wildcards] Photo of a dog on the grass on a sunny spring day

Here is an example of 20 images generated using this placeholder:

Advanced use cases

In this section, we will be covering slightly more advanced use cases. They are:

  • Multiple wildcards
  • Nested wildcards
Multiple wildcards

If you want, you can use multiple placeholders in a single prompt. For example, you can define a set of colors to apply in a word list file, pet_color.txt that contains:

black
white

and use it with another word list file cat_or_dog.txt

cat
dog

Here is the complete prompt:

Photo of a __pet_color__ __cat_or_dog__ on the grass on a sunny spring day

If you press generate, you should see in console that each placeholder is getting replaced:

[Fooocus] Processing prompts ...
[Wildcards] processing: Photo of a __pet_color__ __cat_or_dog__ on the grass on a sunny spring day
[Wildcards] Photo of a black __cat_or_dog__ on the grass on a sunny spring day
[Wildcards] Photo of a black dog on the grass on a sunny spring day
Nested wildcards

In the word list file, in addition to actual words that replace the placeholder, you can also use a placeholder as a word. This may sound confusing, so let’s go over an example:

Let’s recap the two word list files that we had in the previous section:

pet_color.txt

black
white

cat_or_dog.txt

cat
dog

Now instead of having two files, we will have just one file:

cat_or_dog_with_color.txt

__pet_color__ cat
__pet_color__ dog

Now you use in a prompt

Photo of a __cat_or_dog_with_color__ on the grass on a sunny spring day

If you hit generate, you can see that this is processed correctly:

[Fooocus] Processing prompts ...
[Wildcards] processing: Photo of a __cat_or_dog_with_color__ on the grass on a sunny spring day
[Wildcards] Photo of a __pet_color__ dog on the grass on a sunny spring day
[Wildcards] processing: Photo of a __pet_color__ dog on the grass on a sunny spring day
[Wildcards] Photo of a white dog on the grass on a sunny spring day

One advantage of using nested wildcards over multiple wildcards is that nested wildcards allow you to define a valid combination of words. For example, if you add an elephant to the list of pets as a hypothetical example, for the multiple wildcards option, in a file dog_cat_or_elephant.txt, you will have:

cat
dog
elephant

Now you take a look at pet_color.txt below:

black
white

Then you will realize that this would result in the following possible combinations:

  • black cat
  • white cab
  • black dog
  • white dog
  • black elephant
  • white elephant

Unfortunately black elephant and white elephant do not make sense.

Instead, if you use the second approach:

cat_dog_or_elephant.txt

__pet_color__ cat
__pet_color__ dog
elephant

If you use this approach and hit generate, you can see that expansion is working as expected in console output:

[Wildcards] processing: Photo of a __cat_dog_or_elephant__ on the grass on a sunny spring day
[Wildcards] Photo of a __pet_color__ cat on the grass on a sunny spring day
[Wildcards] processing: Photo of a __pet_color__ cat on the grass on a sunny spring day
[Wildcards] Photo of a black cat on the grass on a sunny spring day
[Wildcards] processing: Photo of a __cat_dog_or_elephant__ on the grass on a sunny spring day
[Wildcards] Photo of a elephant on the grass on a sunny spring day

Troubleshooting

If this feature is not working, here are some tips to troubleshoot:

  • In the prompt (or in the word list file if you use nested wildcards), make sure that you have two underscore characters before and after the placeholder word.
  • Check the console output and verify that placeholder substitution is working.
  • Most likely you are not using an old version of Fooocus, but according to the update log, nested wildcard feature is only available after Fooocus version 2.1.716, so make sure that you update to the newer version of Fooocus.

References

[1] Fooocus developers. sdxl_styles.py. Retrieved from https://github.com/lllyasviel/Fooocus/blob/main/modules/sdxl_styles.py

Leave a Comment

Your email address will not be published. Required fields are marked *