Setting Up an E-commerce Website for AI-generated Art with Midjourney: Part 2-Automated Art generation.

Kevin Sallee
10 min readFeb 18, 2023

--

This article is part of a series about my e-commerce website selling art generated with Midjourney, Maquinarte. You can read the first part here.

If you’re a developer who’s interested in making a profit from art, you might be familiar with text to image generation tools like DALL-E, Stable Diffusion, and Midjourney. I’m an expert Python programmer who’s been using Midjourney for the past six months to create art for my website Maquinarte, which sells framed posters and canvases generated by the platform.

Midjourney is a text-to-image generation service that works by executing prompts on a Discord channel. The prompts are commands that specify what kind of art you want to create, like “/imagine prompt: A duck in the style of Paul Gauguin.” You can find full documentation on how to prompt on the Midjourney website, and prompt engineering is becoming a popular field, with even jobs popping up for it.

If you’re looking to start generating art using Midjourney, getting the results you want can be challenging, and it’s important to experiment with different prompts to find the ones that work for you. However, with a bit of practice and the right approach, you can create beautiful and unique pieces of art that are sure to catch the attention of potential buyers.

In this article, I’ll share my approach to using Midjourney for profit, including tips on where to start, how to generate prompts, how to add randomness to your art, and some of the caveats and limitations you should be aware of. Whether you’re a seasoned programmer or just getting started with text-to-image generation, you’re sure to find some valuable insights here. So, let’s get started!

Where to start

When it comes to generating art using Midjourney, getting the results you want is not always easy. In fact, it can be quite challenging. However, the effort you put into it can be highly rewarding, both artistically and financially. In this section, I’ll go over some of the best ways to get started with Midjourney and provide some tips on how to create prompts that will generate the kind of art that you’re looking for.

One of the first things you’ll want to do is familiarize yourself with prompt engineering. Essentially, this means learning how to write prompts that will produce the kind of art you want. Fortunately, there are plenty of resources available online that can help you with this. I recommend checking out this advanced guide to writing prompts for Midjourney on Medium (https://medium.com/mlearning-ai/an-advanced-guide-to-writing-prompts-for-midjourney-text-to-image-aa12a1e33b6), which provides a detailed look at the different elements of a prompt and how they can be used to control the output of the model.

Before I go any further, I should mention that Midjourney used to have a database of styles and artists it could recognize. However, the company has since removed this feature, and now relies on external sites like Journey into AI (https://journeyintoai.com/) to recognize and categorize styles and artists. This means that when you’re creating a prompt, you need to be very clear about the style or artist that you’re trying to emulate.

When it comes to actually creating prompts, there is no one-size-fits-all approach. It’s a very personal process, and you may find that you prefer very short prompts to very detailed ones, or vice versa. One approach that I find works well is to start with a topic that you’re interested in, and then test a few different iterations and slightly different prompts until you find something that is generic enough to generate a wide range of art.

For example, let’s say that you’re interested in creating art based on animals. You might start with a simple prompt like “/imagine prompt: An elephant in the style of Van Gogh”. This will generate an image of an elephant with the distinctive brushstrokes and color palette of Van Gogh. However, you may find that this prompt is too limiting, and that you’re not happy with the results you’re getting. So you might experiment with different artists, styles, or animals until you find something that works better for you.

In conclusion, generating the perfect prompts for your desired results is a process of trial and error. With the help of prompt engineering, resources such as Journey Into AI, and the willingness to experiment, anyone can generate stunning art using Midjourney. Remember to keep your prompts concise and to test out different iterations until you find one that works best for you. With a bit of practice, you’ll be generating incredible art in no time.

Once you’re happy with your prompts, it’s time to automate the prompt generation process.

Sending commands programatically

Generating prompts manually on Discord is not ideal if you want to generate a large number of art pieces. Not only does it take time, but there is also a queue limit, which means that you need to constantly monitor the channel to send new prompts. This is where automation comes in.

To send commands to Midjourney programmatically, I utilized the Discord API and its very interesting endpoint, https://discord.com/api/v9/interactions. This endpoint allows you to create interactions with bots in Discord servers. In my case, I used it to send prompts to Midjourney.

To send requests to the Discord API, I needed to properly authenticate and authorize my bot. However, I was too lazy to properly set up OAuth, so I resorted to a hacky solution. I created a prompt on Discord, inspected the request details in the network tab of Google Chrome, and copied the request as a cURL command. I then used a tool called curlconverter to convert the cURL command to a Python requests call. This gave me a method to send one prompt to Midjourney.

Next, I needed to generate multiple prompts in a randomized manner. I achieved this by writing a Python script that sends multiple prompts to Midjourney, with each prompt repeated a certain number of times. The script takes in a list of prompts and generates new prompts by shuffling the original list and randomly selecting a number of prompts from it. The code for the prompt generation function looks like this:

def send_command(command):
"""
This is a slightly modified version of what comes out of
curlconverter, but mostly as is.
"""
# Of course these values depend on what you have.
cookies = {
'__dcfduid': 'foo',
'__sdcfduid': 'bar',
'OptanonConsent': 'baz',
'locale': 'en-GB',
'__cfruid': 'foo',
}

headers = {
'authority': 'discord.com',
'accept': '*/*',
'accept-language': 'en-GB,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,es-MX;q=0.6,es;q=0.5,en-US;q=0.4,la;q=0.3',
'authorization': 'some auth',
'cache-control': 'no-cache',
'content-type': 'multipart/form-data; foo',
# Requests sorts cookies= alphabetically
# 'cookie': '__dcfduid=fc199d90df8e11ecba3e09503dcd183c; __sdcfduid=fc199d91df8e11ecba3e09503dcd183c6d2d526cbec2ad16331f55420857a938bc38f7cee3a6f16078936a77b8a9bacf; OptanonConsent=isIABGlobal=false&datestamp=Mon+Aug+29+2022+13%3A29%3A45+GMT-0500+(Central+Daylight+Time)&version=6.33.0&hosts=&landingPath=https%3A%2F%2Fdiscord.com%2F&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1; locale=en-GB; __cfruid=bca67bc011a779511d4e96927f570121c6262aa0-1665710238',
'dnt': '1',
'origin': 'https://discord.com',
'pragma': 'no-cache',
'referer': 'some discord channel',
'sec-ch-ua': '"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
'x-debug-options': 'bugReporterEnabled',
'x-discord-locale': 'en-GB',
'x-super-properties': 'some properties',
}
# This is the only place where you need to be smart. You need to find
# the prompt in the data and replace it with "%s"
data = 'Some big string %s' % (command)

response = requests.post('https://discord.com/api/v9/interactions', cookies=cookies, headers=headers, data=data)
response.raise_for_status()


def send_commands(command_generator, nb_commands=1, repeat=1, time_between_commands=5):
"""
The function to send X commands, and repeat each one Y times.
You can specify a time between commands as an int or as a tuple.
If it's an int, the time between two commands.
For a tuple, a random time in the range will be used.
"""
for i in range(nb_commands):
for _ in range(repeat):
command = command_generator()
print(i + 1, command)
send_command(command)
if isinstance(time_between_commands, tuple):
# get random integer between 1 and time_between_commands
time.sleep(random.randint(time_between_commands[0], time_between_commands[1]))
else:
time.sleep(time_between_commands)

send_command is a function that takes a single parameter, command. This function sends a request to the Discord API endpoint for interactions using the requests.post() method. It uses a cookies dictionary and a headers dictionary to provide the necessary authentication and authorization information for the request.

The send_commands function is the one that generates multiple prompts and sends them to Midjourney programmatically. It takes three parameters:

  • command_generator: a function that generates a single prompt string.
  • nb_commands: the number of prompts to generate and send.
  • repeat: the number of times to repeat each prompt.
  • time_between_commands: the time delay between sending each prompt.

The only piece missing now is the command generator. Let’s see how we can come up with random commands to create a collection of art pieces.

Generating randomized commands

In the previous section, we learned how to programmatically interact with Midjourney using the Discord API. We created a function called send_commands which sends requests to the Discord API endpoint for interactions using the requests.post() method. We also learned that the function takes in a single parameter, command, which is the prompt that we want to send to Midjourney. In this section, we will learn how to generate random prompts programmatically using Python.

When it comes to creating a collection of art pieces, one of the most challenging aspects is coming up with creative prompts. The prompts need to be unique and interesting to keep the viewers engaged. One way to create unique and interesting prompts is by adding randomness to them. By adding randomness, we can create prompts that are unexpected, surprising, and creative.

To add randomness to our prompts, we will use Python’s built-in random module. The random module provides several functions for generating random numbers, selecting random items from a list, and shuffling lists. We will be using these functions to generate random prompts.

Let’s start with an example. Suppose we want to generate a prompt in the style of the famous Spanish architect Santiago Calatrava. We can start by asking ChatGPT to generate a random list of 50 themes for that artist, in python format, and copy-paste them into our code. Here’s what the code would look like:

def get_salatrava_command():
SUBJECTS = [
"Sculptural Forms",
"Organic Shapes",
"Fluid Movement",
"Skeletal Structures",
"Dynamic Tension",
"Sustainability",
...
]
ASPECT_RATIOS = ["5:4", "4:3", "1:1", "3:4", "4:5", "3:2", "2:3"]
subject = random.choice(SUBJECTS)
aspect_ratio = random.choice(ASPECT_RATIOS)
return "%s, in the style of Santiago Calatrava --ar %s" % (
subject, aspect_ratio
)

Of course you can adapt this, you can for example add chaos, add actions, add multiple artists by using random.choices instead of random.choice , etc.

Using this method, we can generate an almost infinite number of different prompts to create art pieces inspired by Santiago Calatrava’s work. This is just one example of how we can add randomness to the prompts that we generate. We can apply this same principle to generate prompts for any artist or subject we want to create art pieces inspired by.

Here’s the example output you get when calling send_commands:

> d.send_commands(d.get_calatrava_command, nb_commands=10, repeat=1, time_between_commands=(10, 20))
0 Sculptural Forms, in the style of Santiago Calatrava --ar 5:4
1 Dynamic Tension, in the style of Santiago Calatrava --ar 1:1
2 Organic Shapes, in the style of Santiago Calatrava --ar 3:2
3 Skeletal Structures, in the style of Santiago Calatrava --ar 4:5
...

You can see examples of the results I obtained with this technique in the Calatrava collection at Maquinarte.

In conclusion, generating random prompts can be a powerful tool in creating unique and interesting art pieces. With Python’s random module, we can add randomness to prompts and generate an almost infinite number of different prompts for any artist or subject we want to create art pieces inspired by. However, there are caveats and limits to using this method, which we will detail in the next section.

Caveats and limits

While using this technique to generate art pieces can be fun and exciting, there are a few caveats and limits to keep in mind.

Firstly, it’s unclear if this technique is tolerated by Discord’s terms of service. It’s important to note that I have only used this technique for personal use, and more for testing than to actually use it, and have ensured that enough time is put between commands to prevent generating too much content. In practice, I just generate the random prompts and then copy and paste them in discord.

Secondly, the upscaling process of the generated art pieces is manual, as the user needs to select the art piece that they want to upscale in their Discord channel. This process can be tedious, but it’s necessary to ensure that what I choose to upscale is good art, since it will be visible on Maquinarte.

Additionally, the code to authenticate the Discord API is a bit hacky, and the user will need to change the cookies from time to time. Properly using the discord API should be the way to go. I’ll do it at some point to make the code more robust.

It’s also important to note that while this technique has worked flawlessly for me and I have generated some art pieces with it, I am not aware of anyone else using it.

Overall, it’s important to use this technique responsibly and to ensure that it’s not violating any terms of service agreements.

Conclusion

Automating the process of generating art with Midjourney using Python can save a lot of time and effort. This technique is still experimental and has some limitations, but it can be a useful tool for artists and designers who want to explore the possibilities of AI-generated art.

As with any new technology, there are some caveats and limits to be aware of with my approach, such as the need to upscale the images manually and the hacky authentication code. However, with some patience and creativity, this approach can lead to amazing results.

If you want to see how this technique is used in practice, I invite you to check out Maquinarte and its collections of Modern art, Santiago Calatrava, children animals, and much more. And if you’re interested in the technical details behind the backend, be sure to read the first article of these series, which provides a step-by-step guide on setting up an e-commerce website for AI-generated art with Midjourney.

I hope this article has been helpful in introducing you to the world of AI-generated art and showing you how to leverage Midjourney’s capabilities with Python. As AI technologies continue to evolve and improve, I’m excited to see what new possibilities they will bring to the world of art and design.

--

--

Kevin Sallee

15+ years of Software Engineering. I am a computer nerd and an art lover, curious by nature, information sponge.