Midjourney is fun, but the Discord-based interface is somewhat annoying if you want to make a lot of images. To get all the images you generate, you have to upscale all 2×2 previews and save them manually. It can get pretty tiring, so instead of making better images, I spent a bit of time automating the download and import process. With this code, a Discord bot will monitor your server, watch for Midjourney messages, process all images, and upload them to your Lightroom CC cloud account. Run the script anywhere, a Raspberry Pi or a cheap cloud server, and keep making images.
If you want to get started right away, the code is github.com/softplus/midjourney_lightroom
This looks like a long post – it’s just a collection of many small steps. If you’ve run something from GitHub before, you can do it. All of this works under Ubuntu, I’m not bored enough to try it out in other operating systems. YMMV.
Inspired by Michael King’s guide to automatically download Midjourney images.
Overview
You will need the following:
- Somewhere to run the code. I use a computer with Ubuntu.
- A private Discord server. Make a new one if you want, they’re “free”.
- A Discord bot key (we’ll make that below)
- An Adobe Lightroom CC API key (we’ll make that below)
- A Midjourney account (#obviously)
It will do the following:
- The bot joins your private server & channel.
- You’ve invited Midjourney there too.
- When you get a result from Midjourney, it will download the image, and upload them to Lightroom.
- Once a day you’ll need to authenticate for Lightroom.
Clone & set up the project
This is for Ubuntu. Adjust as needed. We’re cloning the project, setting up a virtual environment, installing the pre-requisites, and making a default setting (.env
) file. In a command window, enter the following:
git clone https://github.com/softplus/midjourney_lightroom # get the repo
cd midjourney_lightroom # enter the folder
virtualenv .venv && source .venv/bin/activate # set up a virtual environment
pip install -r requirements.txt # install requirements
cp sample.env .env # copy the settings file
Lightroom CC API key
We’re creating a new project, selecting the Lightroom API, and getting the OAuth2 keys.
- Go to https://developer.adobe.com/console/home
- Create a new project
- Click ‘Add API’
- Find ‘Lightroom Services (requires review)’ in the list, click ’next’
- For Oauth 2.0 authentication, select ‘Web App’
- Enter ‘https://lowly-occipital-croissant.glitch.me/’ as redirect URI and ‘https://lowly-occipital-croissant\.glitch\.me/’ as Redirect URI pattern (with the extra \’s):
- Copy the ‘Client ID’ into your
.env
file. - Click ‘Retrieve client secret’, and copy the client secret into your
.env
file. - Done! (for this)
Discord server
This (probably) works best with a separate Discord server, so let’s set that up. Here’s the official guide.
- In Discord, click on the “+” to add a new server
- Select ‘Create My Own’
- Select ‘For me and my friends’
- Enter a server name. Anything. Your favorite cheese.
Now it’s time to add the Midjourney bot.
- Go to your direct messages
- Select the Midjourney bot
- Click on the name of the bot on top:
- Click ‘Add to server’
- Select your server, click ‘Continue’
- Leave settings default, click ‘Authorize’ (and do captcha)
Now go to your server into one of the channels, you should see a confirmation that the bot was added. Try to make an image, it should work normally.
Create a Discord bot
- Go to https://discord.com/developers/applications
- Click ‘New Application’
- Pick an application name (anything) and confirm that you’re not a jerk.
- Click ‘Create’. You now have a bot!
- Select ‘Bot’ in the sidebar.
- In ‘Build-A-Bot’, click ‘Reset Token’. This shows a long token.
- Click ‘Copy token’ and save it in your
.env
file asDISCORD_TOKEN
. - Still in ‘Bot’, scroll down and enable the intents: ‘Presence intent’, ‘Server members intent’ and ‘Message content intent’
- Select ‘OAuth2’ in the sidebar, and go to ‘URL generator’
- In ‘Scopes’, select ‘Bot’ – this adds the bot permissions on the bottom.
- Scroll down, and under ‘Bot permissions’ select ‘Read messages / View channels’
- Go to the bottom, copy the Generated URL, and open it in a separate browser tab.
- This starts with adding the bot to your server. Select your server and continue.
- Confirm the ‘Read messages’ permission. Click ‘Authorize’. Do Captcha.
- Your bot is now in the Discord! You should see a confirmation in the channel.
Now you have Lightroom, Discord, and the permissions set up. Your .env
file should minimally contain settings for DISCORD_TOKEN, LR_CLIENT_ID, and LR_CLIENT_SECRET.
Run the bot
In the command window where you installed the code, you should be able to run it now:
python3 main_script.py
# stuff happens now
It should log in to Discord, and wait for more:
Creating new salt for random file names.
Lightroom authentication code invalid, starting fresh.
401 Client Error: Unauthorized for url: https://lr.adobe.io/v2/catalog
2023-05-01 09:07:24 INFO discord.client logging in using static token
2023-05-01 09:07:25 INFO discord.gateway Shard ID None has connected
to Gateway (Session ID: 1234567890abcdef1234567890abcdef).
Logged in as chessetesters#2484 (ID: 1234567890123456789)
Bot connected, enter 'hi!' to confirm
The Adobe error is because you haven’t logged in to your Lightroom account yet – we’ll do that next. You won’t see anything in Discord just yet, since it doesn’t know which channel to post to.
In Discord, enter ‘hi!’ into the chat. This will trigger the bot, who will give you the URL to authenticate for Lightroom. The URL is specific to this bot.
Click the link, and (potentially) sign in to your Adobe account (if not signed in already). It will redirect you to a page with the auth-code. The auth-code is specific to the bot and your account.
Click on the auth code, this will select it and copy it into your clipboard. Paste the auth code into Discord (including the ‘auth!’ part in the beginning). The bot will respond with ‘Lightroom authentication ok’ if it’s good. The authentication expires after 24 hours, so this will trigger for you once a day. You’re all set now!
Try an /imagine
command. Midjourney should generate the image. Your script should pick it up. Lightroom should show the 4 generated images. You don’t need to use ‘Upscale’.
Here’s what you might see in the script output:
Message: Please authenticate here and paste the results here:
Message: https://ims(...)e%2F
Message: auth! ey(...)g
Message: Lightroom authentication is ok.
Message: **cheese is holey --v 5** - <@1234567890123456789> (Waiting to start)
Message: **cheese is holey --v 5** - <@1234567890123456789> (fast)
Image downloaded: johnmu_cheese_is_holey_01234567-89ab-cdef-0123-456789abcdef.png
Stored: -cABCDEFGabcdefg012345.png
Stored: VnABCDEFGabcdefg012345.png
Stored: u1ABCDEFGabcdefg012345.png
Stored: zLABCDEFGabcdefg012345.png
The images are stored in a folder called ‘out’ locally, and uploaded to Lightroom CC. You’re essentially done now, this will work. Adding the spreadsheet below makes it easier to track back prompts for images you created, but it’s optional.
Setting up a Google Spreadsheet for image details
The images are renamed into “random” names (MD5 of original URL + salt from your settings). This prevents accidentally leaking the prompt or the Midjourney GUID (which lets people look up the details and your other images). It also makes for shorter filenames.
A local file out/_log.csv
tracks the original file names, links to the Discord message, and the Midjourney GUID. It’s also added into a Google Sheet with help of a Google Forms hack. Here’s how to set it up.
- Go to https://forms.new/ – this creates a new Google Form
- Name it reasonably.
- Add 8 ‘short answer’ text fields. They don’t need to be named, but the field names I use are: New UUID, Old UUID, Prompt, Seed, URL, Filename, Job URL, Image URL. (The seed is future-looking, it can’t be picked up currently.)
- Click on the ‘Responses’ tab, and select ‘Link to Sheets’. Select ‘Create a new Spreadsheet’.
- Go back to the form, click the preview link (the funky weird eye-icon on top).
- Copy the URL and add it to your
.env
file asFORM_URL
. - Restart your script, if it’s running.
Try making an image. It should now upload to Lightroom, and get logged in the spreadsheet.
This is it. Create away.
Final thoughts
Random notes
- This all depends a bit on Midjourney’s current setup (filenames, Discord message formatting, file formats, previews containing the full image, etc). If any of this changes, this script might not work anymore.
- Recently, Midjourney moved to .webp for preview images, which had to be adjusted here.
- It’s possible that Midjourney doesn’t like this, though since it’s not automatically pushing requests to Midjourney, maybe it’s fine?
- I can’t promise to keep up with Midjourney changes.
- The Adobe Lightroom API has a weird bug that doesn’t accept a date stamp for images uploaded. No idea how to report that.
- This only downloads new images, it doesn’t go through your channel history.
Possible future updates
- Document how to run this on a free instance in Google Cloud
- Document how to run this on a Raspberry Pi (should be trivial).
- Auto-delete images by responding with “X” after some time after importing. Keep your Midjourney profile & Discord clean.
- Add a command to select an album to add images to.
- Auto-pick album name based on prompt.
- Run Open-AI’s API over the prompt to generate hashtags for auto-classification.
- Process part of chat history.