Using Character Reference (--cref) with Midjourney API!
Posted on 2024/3/13 by GoAPI
Supporting Character Reference (--cref) in Midjourney API!
So for developers like you, this feature is going to be a game changer for your users who are looking to have continuity in their creative effort (ex. their storytelling images), or if your users are just doing some simple face-swap shenanigans for fun!
Experimenting with Character Reference
First, we will send a simple imagine API call with "a boy" as the prompt. Below is the cURL code snippet taken from Postman:
curl --location 'https://api.midjourneyapi.xyz/mj/v2/imagine'
--header 'X-API-Key: YOUR API KEY'
--header 'Content-Type: application/json'
--data '{
"prompt": "a boy",
"skip_prompt_check": false,
"process_mode": "fast",
"aspect_ratio": "",
"webhook_endpoint": "",
"webhook_secret": ""
}'
And below is the result that I got from Midjourney.
And let's say I want to continue working with the boy's look in the fourth image, so I will do an upscale API call on it (below is the Postman cURL code snippet)
curl --location 'https://api.midjourneyapi.xyz/mj/v2/upscale'
--header 'X-API-Key: YOUR API KEY'
--header 'Content-Type: application/json'
--data '{
"origin_task_id": "enter the taskID of the previous boy imagine task",
"index": "4",
"webhook_endpoint": "",
"webhook_secret": ""
}'
Now, let's say we want to create an image of the boy playing in a school yard, we will the use the '--cref' parameter for Character Reference in our new imagine prompt and append the previously obtained image url after the parameter as shown below. I will also set the --cw parameter to 100 as I want to maintain strong reference to the face, clothing, and hair.
curl --location 'https://api.midjourneyapi.xyz/mj/v2/imagine'
--header 'X-API-Key: YOUR API KEY'
--header 'Content-Type: application/json'
--data '{
"prompt": "boy playing in schoolyard --cref https://img.midjourneyapi.xyz/mj/taskid.png --cw 100",
"skip_prompt_check": false,
"process_mode": "fast",
"aspect_ratio": "",
"webhook_endpoint": "",
"webhook_secret": ""
}'
Experimenting with two URLs with --Cref
Now we can try an advanced feature of blending the character styles from two different links in the same prompt using --cref. Let's say I want to create an image where the previously generated image of the boy wearing an iron man suit and is fighting evil.
First, I found an iron man suit image from the internet.
And now for my imagine API call, I will use a simple prompt 'a boy wearing an iron man suit fighting evil' with the boy's image and the image of the iron man suit as character reference, and setting the --cw parameter to 0 as I want to only focus on the boy's facial structure.
curl --location 'https://api.midjourneyapi.xyz/mj/v2/imagine' --header 'X-API-Key: YOUR API KEY' --header 'Content-Type: application/json' --data '{
"prompt": "a boy wearing an iron man suit fighting evil --cref link1 link2 --cw 0",
"skip_prompt_check": false,
"process_mode": "fast",
"aspect_ratio": "",
"webhook_endpoint": "",
"webhook_secret": ""
}'
And voila! In the output we can see the boy's blonde hair was somewhat kept, his roundish facial structure was also somewhat kept, and the iron man suit design was very well kept. Despite the fact that the likeliness of the boy in the generated image is not extremely high, but we know that prompting with generative AI models is not perfect nor an exact science; with more trials and errors I am sure we will all get a better hang of it!
And that is it for our tutorial on how to use the Character Reference (--cref) parameter to achieve consistent characters in your generated images with Midjourney API!
Happy tinkering guys!