图生图 API
更新时间
7 天前
接口描述
提交图生图任务。
请求
TIP
{api_url}
为你实际使用的 API 节点,请根据实际情况填写。例如:
www.dmxapi.cn
www.dmxapi.com
ssvip.dmxapi.com
请求方式: POST
请求地址:
/mj/submit/imagine
请求参数
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
mode | String | 否 | 生成模式,可选值:"Turbo"、"Fast"(默认)、"Relax" |
botType | String | 否 | 模型类型,可选值 "MID_JOURNEY"(默认) 或者 "NIJI_JOURNEY" |
prompt | String | 是 | 提示词,描述希望生成的图片内容 |
base64Array | Array | 否 | 包含图片 base64 数据的数组,格式为 "data:image/png;base64,<base64字符串>" |
notifyHook | String | 否 | 回调通知的 URL,处理完成后会向该地址发送回调 |
代码示例
深色背景为可以修改的参数,非必选参数已经注释,可以按照自己的需求启用。
py
import http.client
import json
import requests
import base64
# 配置全局变量
API_URL = "www.dmxapi.cn" # API 节点
DMX_API_TOKEN = "sk-XXXXXXXXXXXXX" # API 密钥
# 获取图片的base64编码
def get_image_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# 从URL获取图片并转换为base64格式
def url_image_to_base64(image_url):
"""
从URL获取图片并转换为base64格式
参数:
image_url (str): 图片的URL地址
返回:
str: base64编码的图片字符串
"""
try:
# 发送GET请求获取图片
response = requests.get(image_url)
# 将图片内容编码为base64
image_binary = response.content
base64_encoded = base64.b64encode(image_binary).decode('utf-8')
return base64_encoded
except Exception as e:
print(f"获取图片失败: {str(e)}")
return None
# 提交图生图任务
def midjourney_generate_image(image_path):
# 获取图片的base64编码
if image_path.startswith('https://') or image_path.startswith('http://'):
base64_string = url_image_to_base64(image_path)
else:
base64_string = get_image_base64(image_path)
conn = http.client.HTTPSConnection(API_URL)
payload = json.dumps({
"mode": "RELAX", # 模式 可选值:"Turbo"、"Fast"(默认)、"Relax"
"botType": "NIJI_JOURNEY", # 模型类型,可选值 "MID_JOURNEY"(默认) 或者 "NIJI_JOURNEY"
"prompt": "这是一个视频截图,请生成对应的吉卜力风格的图片", # 提示词,描述希望生成的图片内容
"base64Array": [
"data:image/png;base64," + base64_string # 包含图片 base64 数据的数组,格式为 "data:image/png;base64,<base64字符串>"
],
"notifyHook": "string" # 回调通知的 URL,处理完成后会向该地址发送回调
})
# 设置请求头,包含认证信息和内容类型
headers = {
'Authorization': f'Bearer {DMX_API_TOKEN}',
'Content-Type': 'application/json'
}
# 发送POST请求到/mj/submit/imagine接口
conn.request("POST", "/mj/submit/imagine", payload, headers)
# 获取响应
res = conn.getresponse()
data_json = json.loads(res.read().decode("utf-8"))
print(data_json)
print(data_json["description"])
task_id = data_json["result"]
return task_id
if __name__ == "__main__":
# image_path = "/Users/dmxapi/Desktop/dxmapi.jpg" # 本地图片方式生成
image_url = "https://cdn.jsdelivr.net/gh/timerring/scratchpad2023/2024/2025-05-14-22-17-12.png" # url 图片方式生成
print(midjourney_generate_image(image_url))
响应参数示例
成功提交任务
{
"code": 1,
"description": "提交成功",
"result": "1747141695143320",
"properties": {
"discordInstanceId": "1371290282703978598",
"discordChannelId": "1371290282703978598"
}
}
已提交到队列 等待启动
{
"code": 1,
"description": "In queue, there are 67 tasks ahead",
"properties": None,
"result": "1747141540401979"
}