便携AI聚合API支持Midjourney,包括绘图、变换、重绘、混合、缩放等,可以通过聊天工具直接使用,也可以通过代码调用,本文分享下Midjourney API如何通过代码调用。
前言
文中使用的API Key均为便携AI聚合API后台生成的令牌,以sk-开头的一串随机字符,获取方法:《便携AI聚合API新建令牌(API key)教程》。
便携AI聚合API支持所有Midjourney指令(包括Midjourney Proxy和Midjourney Proxy Plus),如:
- mj_imagine (绘图)
- mj_variation (变换)
- mj_reroll (重绘)
- mj_blend (混合)
- mj_upscale (放大)
- mj_describe (图生文)
- mj_zoom (比例变焦)
- mj_shorten (提示词缩短)
- mj_modal (窗口提交)
- mj_inpaint (局部重绘提交)
- mj_custom_zoom (自定义比例变焦)
- mj_high_variation (强变换)
- mj_low_variation (弱变换)
- mj_pan (平移)
- swap_face (换脸)
绘图文生图
请求地址:https://api.bianxie.ai/mj/submit/imagine
功能:根据你的prompt和垫图来画图。
请求方式:POST
参数示例:
{ "botType": "MID_JOURNEY", # 枚举,MID_JOURNEY或者NIJI_JOURNEY "prompt": "Cat", # 提示词 "base64Array": [], # 垫图的base64数组 "notifyHook": "", # 回调地址, 为空时使用全局notifyHook,不需要提供 "state": "" # 自定义参数 }
代码示例:
url = "https://api.bianxie.ai/mj/submit/imagine" payload = json.dumps({ "botType": "MID_JOURNEY", "prompt": "Cat", }) headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)
返回示例(其中result就是任务ID,这个很重要):
{ "code": 1, "description": "Submit success", "result": "1730451923591908", "properties": { "discordChannelId": "1300379142822559754", "discordInstanceId": "1573315683610218496" } }
这个就说明你的绘图任务已经成功提交了,在便携AI聚合API的midjourney后台也能看到这个任务,地址:https://api.bianxie.ai/midjourney,如下图:
生成的图片:
你也可以通过代码通过任务ID来查看这个任务的状态,以及对这个任务进行其他变种操作(如放大缩小、重绘、局部重绘等),具体的方法看第二部分。
根据任务ID查询任务
请求地址:https://api.bianxie.ai/mj/task/{id}/fetch
功能:根据id来查询具体的操作,以及获取customId。
请求方式:GET
参数:不需要参数,任务ID就在路径里。
代码示例(将1730451923591908换成你的任务ID):
url = "https://api.bianxie.ai/mj/task/1730451923591908/fetch payload = '' headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("GET", url, headers=headers, data=payload) print(response.text)
返回示例,这里包含了U1、U2、U3、U4、V1、V2、V3、V4,以及重绘(reroll)的customId,根据这个customId我们可以提交Action以进行之后的操作:
{ "id": "1730451923591908", "action": "IMAGINE", "customId": "", "botType": "", "prompt": "Cat", "promptEn": "Cat", "description": "Submit success", "state": "", "submitTime": 1730451923591, "startTime": 1730451926508, "finishTime": 1730451959441, "imageUrl": "https://api.bianxie.ai/mj/image/1730451923591908", "status": "SUCCESS", "progress": "100%", "failReason": "", "buttons": [{ "customId": "MJ::JOB::upsample::1::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "U1", "type": 2, "style": 2 }, { "customId": "MJ::JOB::upsample::2::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "U2", "type": 2, "style": 2 }, { "customId": "MJ::JOB::upsample::3::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "U3", "type": 2, "style": 2 }, { "customId": "MJ::JOB::upsample::4::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "U4", "type": 2, "style": 2 }, { "customId": "MJ::JOB::reroll::0::3e8691c8-bc26-4445-b099-b1cd1b0d123c::SOLO", "emoji": "🔄", "label": "", "type": 2, "style": 2 }, { "customId": "MJ::JOB::variation::1::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "V1", "type": 2, "style": 2 }, { "customId": "MJ::JOB::variation::2::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "V2", "type": 2, "style": 2 }, { "customId": "MJ::JOB::variation::3::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "V3", "type": 2, "style": 2 }, { "customId": "MJ::JOB::variation::4::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "emoji": "", "label": "V4", "type": 2, "style": 2 }], "maskBase64": "", "properties": { "finalPrompt": "Cat", "finalZhPrompt": "" } }
提交Action命令
请求地址:https://api.bianxie.ai/mj/submit/action
功能:根据customId来对已绘的图片进行操作,包括UNSAMPLE(放大)、VARIATION(变化)、ZOOM(图片变焦)、PAN(焦点移动)、重绘(REROLL)等。
请求方式:POST
参数:
{ "customId": "MJ::JOB::upsample::1::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "taskId": "1730451923591908", "notifyHook": "", "state": "" }
其中customId和taskId是必须的(customId可以通过上一部分根据任务ID查询任务获取)。
代码示例:
url = "https://api.bianxie.ai/mj/submit/action" payload = json.dumps({ "customId": "MJ::JOB::upsample::1::3e8691c8-bc26-4445-b099-b1cd1b0d123c", "taskId": "1730451923591908", }) headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)
返回示例(跟绘图一样,会有一个新的任务ID):
{ "code": 1, "description": "Submit success", "result": "1730453101043963", "properties": { "discordChannelId": "1300379142822559754", "discordInstanceId": "1573315683610218496" } }
同样可以通过网站后台或者fetch命令来查看这个任务的状态。
unsample的结果:
注意,如果是局部重绘(inpaint)和自定义变焦(custom_zoom)的操作,则必须要同时提交窗口(Modal)。例如这里对放大的结果(1730453101043963)再来一个局部重绘(inpaint),返回结果:
{ "code": 21, "description": "Waiting for window confirm", "result": "1730463251310715", "properties": { "finalPrompt": "Cat", "remix": true } }
在网站后台(https://api.bianxie.ai/midjourney)也会有一个新的任务,并且任务状态是窗口等待:
提交Modal(窗口)
请求地址:https://api.bianxie.ai/mj/submit/modal
功能:提交需要局部修改的部分,base64,底色纯黑,选中区域纯白。
请求方式:POST
参数:
{ "maskBase64": "", # 局部重绘的蒙版base64 "prompt": "", "taskId": "1730463251310715" }
如果是局部重绘,maskBase64和taskId都是必须的,taskId就是正在等待窗口的ID(例如这里的1730463251310715),maskBase64是一个图片的base64编码,底色纯黑,需要修改的地方就是纯白,例如我需要修改左上角,maskBase64应该就是这样的:
图片可以通过这个网站转成base64编码:在线图片转Base64编码工具。
代码示例:
url = "https://api.bianxie.ai/mj/submit/modal" payload = json.dumps({ "maskBase64": "data:image/jpeg;base64,xxx", "prompt": "make it more cartoon", "taskId": "1730463251310715" }) headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)
返回示例:
{ "code": 1, "description": "Submit success", "result": "1730463251310715", "properties": { "discordChannelId": "1300379142822559754", "discordInstanceId": "1573315683610218496" } }
局部重绘的结果:
Describe图生文
请求地址:https://api.bianxie.ai/mj/submit/describe
功能:描述提供的图片。
请求方式:POST
参数:
{ "botType": "MID_JOURNEY", "base64": "data:image/png;base64,xxx", "notifyHook": "", "state": "" }
base64参数就是你需要描述的图片的base64编码,图片可以通过这个网站转成base64编码:在线图片转Base64编码工具。
示例代码(上传的图片是刚才生成的白猫):
url = "https://api.bianxie.ai/mj/submit/describe" payload = json.dumps({ "botType": "MID_JOURNEY", "base64": "data:image/png;base64,xxx" }) headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)
之后可以通过fetch或者在网站后台(https://api.bianxie.ai/midjourney)看到这个图片的prompt:
{ "id": "1730464720359934", "action": "DESCRIBE", "customId": "", "botType": "", "prompt": "", "promptEn": "1️⃣ White, cute cat with blue eyes, on a white background, suitable for mobile wallpaper, vector illustration, high resolution, highly detailed, hyper-realistic, high definition, HDR. --ar 128:127\n\n2️⃣ A white cat with blue eyes, on a white background, suitable for a mobile wallpaper, in high definition, in a cute style, 3D rendering. --ar 128:127\n\n3️⃣ White, cute cat with blue eyes, white background, mobile wallpaper, 3D rendering, high resolution, high quality, high detail, professional photography, sharp focus, studio lighting, soft light, volumetric lighting, cinematic effect, hyperrealistic, hyperdetailed, hyper-realistic, super-realistic, hyper-realistic --ar 128:127\n\n4️⃣ A white, cute cat with blue eyes on a white background, suitable for a mobile wallpaper. The image has high resolution, high detail, high quality, high sharpness, and high definition. --ar 128:127", "description": "Submit success", "state": "", "submitTime": 1730464720359, "startTime": 1730464723831, "finishTime": 1730464732715, "imageUrl": "https://api.bianxie.ai/mj/image/1730464720359934", "status": "SUCCESS", "progress": "100%", "failReason": "", "buttons": [{ "customId": "MJ::Job::PicReader::1", "emoji": "1️⃣", "label": "", "type": 2, "style": 2 }, { "customId": "MJ::Job::PicReader::2", "emoji": "2️⃣", "label": "", "type": 2, "style": 2 }, { "customId": "MJ::Job::PicReader::3", "emoji": "3️⃣", "label": "", "type": 2, "style": 2 }, { "customId": "MJ::Job::PicReader::4", "emoji": "4️⃣", "label": "", "type": 2, "style": 2 }, { "customId": "MJ::Picread::Retry", "emoji": "🔄", "label": "", "type": 2, "style": 2 }, { "customId": "MJ::Job::PicReader::all", "emoji": "🎉", "label": "Imagine all", "type": 2, "style": 2 }], "maskBase64": "", "properties": { "finalPrompt": "1️⃣ White, cute cat with blue eyes, on a white background, suitable for mobile wallpaper, vector illustration, high resolution, highly detailed, hyper-realistic, high definition, HDR. --ar 128:127\n\n2️⃣ A white cat with blue eyes, on a white background, suitable for a mobile wallpaper, in high definition, in a cute style, 3D rendering. --ar 128:127\n\n3️⃣ White, cute cat with blue eyes, white background, mobile wallpaper, 3D rendering, high resolution, high quality, high detail, professional photography, sharp focus, studio lighting, soft light, volumetric lighting, cinematic effect, hyperrealistic, hyperdetailed, hyper-realistic, super-realistic, hyper-realistic --ar 128:127\n\n4️⃣ A white, cute cat with blue eyes on a white background, suitable for a mobile wallpaper. The image has high resolution, high detail, high quality, high sharpness, and high definition. --ar 128:127", "finalZhPrompt": "" } }
Blend图片混合
请求地址:https://api.bianxie.ai/mj/submit/blend
功能:图片混合。
请求方式:POST
参数:
{ "botType": "MID_JOURNEY", "base64Array": [ # 进行混合的图片的base64数组 "data:image/png;base64,xxx1", "data:image/png;base64,xxx2" ], "dimensions": "SQUARE", # 生成的图片的比例,PORTRAIT(2:3); SQUARE(1:1); LANDSCAPE(3:2) "notifyHook": "", "state": "" }
代码示例:
url = "https://api.bianxie.ai/mj/submit/blend payload = json.dumps({ "botType": "MID_JOURNEY", "base64Array": [ "data:image/png;base64,xxx1", "data:image/png;base64,xxx2" ], "dimensions": "LANDSCAPE" }) headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)
混合结果可以通过fetch或者在网站后台(https://api.bianxie.ai/midjourney)查看,结果如下(这里将第一部分左上角的白猫和右下角的黑猫混合了):
SWAP_FACE人脸替换
请求地址:https://api.bianxie.ai/mj/insight-face/swap
功能:人脸替换。
请求方式:POST
参数:
{ "sourceBase64": "data:image/png;base64,xxx1", # 人脸源图片base64 "targetBase64": "data:image/png;base64,xxx2", # 目标图片base64 "notifyHook": "", "state": "" }
代码示例:
url = "https://api.bianxie.ai/mj/insight-face/swap" payload = json.dumps({ "sourceBase64": "data:image/png;base64,xxx1", "targetBase64": "data:image/png;base64,xxx2" }) headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) print(response.text)