Skip to content

Commit eb5a448

Browse files
committed
feat: mj image to image
1 parent 1b20b98 commit eb5a448

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 图生图 API
3+
gitChangelog: false
4+
updatedAt: 2025-05-08
5+
---
6+
7+
8+
# 图生图 API
9+
10+
## 接口描述
11+
12+
提交图生图任务。
13+
14+
## 请求
15+
16+
> [!TIP]
17+
> `{api_url}` 为你实际使用的 API 节点,请根据实际情况填写。例如:
18+
> - `www.dmxapi.cn`
19+
> - `www.dmxapi.com`
20+
> - `ssvip.dmxapi.com`
21+
22+
23+
- 请求方式: POST
24+
25+
- 请求地址: `/mj/submit/imagine`
26+
27+
## 请求参数
28+
29+
| 参数名 | 类型 | 必填 | 说明 |
30+
| --- | --- | --- | --- |
31+
| mode | String || 生成模式,可选值:"Turbo"、"Fast"(默认)、"Relax" |
32+
| botType | String || 模型类型,可选值 "MID_JOURNEY"(默认) 或者 "NIJI_JOURNEY" |
33+
| prompt | String || 提示词,描述希望生成的图片内容 |
34+
| base64Array | Array || 包含图片 base64 数据的数组,格式为 "data:image/png;base64,<base64字符串>" |
35+
| notifyHook | String || 回调通知的 URL,处理完成后会向该地址发送回调 |
36+
37+
## 代码示例
38+
39+
> 深色背景为可以修改的参数,非必选参数已经注释,可以按照自己的需求启用。
40+
41+
42+
<<< @/zh/snippets/midjourney/api/image-to-image-api.py{7-8,51-57,76-77}
43+
44+
## 响应参数示例
45+
46+
### 成功提交任务
47+
48+
```
49+
{
50+
"code": 1,
51+
"description": "提交成功",
52+
"result": "1747141695143320",
53+
"properties": {
54+
"discordInstanceId": "1371290282703978598",
55+
"discordChannelId": "1371290282703978598"
56+
}
57+
}
58+
```
59+
60+
### 已提交到队列 等待启动
61+
62+
```
63+
{
64+
"code": 1,
65+
"description": "In queue, there are 67 tasks ahead",
66+
"properties": None,
67+
"result": "1747141540401979"
68+
}
69+
```

docs/zh/models/midjourney/api/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ updatedAt: 2025-05-08
1515

1616
## 提交任务接口
1717

18+
- [图生图 API](image-to-image-api.md)
1819

1920

docs/zh/models/midjourney/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ updatedAt: 2025-05-08
2121

2222
### 提交任务接口
2323

24+
- [图生图 API](./api/image-to-image-api.md)
2425

2526
## 模型介绍
2627

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import http.client
2+
import json
3+
import requests
4+
import base64
5+
6+
# 配置全局变量
7+
API_URL = "www.dmxapi.cn" # API 节点
8+
DMX_API_TOKEN = "sk-XXXXXXXXXXXXX" # API 密钥
9+
10+
# 获取图片的base64编码
11+
def get_image_base64(image_path):
12+
with open(image_path, "rb") as image_file:
13+
return base64.b64encode(image_file.read()).decode("utf-8")
14+
15+
# 从URL获取图片并转换为base64格式
16+
def url_image_to_base64(image_url):
17+
"""
18+
从URL获取图片并转换为base64格式
19+
20+
参数:
21+
image_url (str): 图片的URL地址
22+
23+
返回:
24+
str: base64编码的图片字符串
25+
"""
26+
try:
27+
# 发送GET请求获取图片
28+
response = requests.get(image_url)
29+
30+
# 将图片内容编码为base64
31+
image_binary = response.content
32+
base64_encoded = base64.b64encode(image_binary).decode('utf-8')
33+
34+
return base64_encoded
35+
36+
except Exception as e:
37+
print(f"获取图片失败: {str(e)}")
38+
return None
39+
40+
# 提交图生图任务
41+
def midjourney_generate_image(image_path):
42+
43+
# 获取图片的base64编码
44+
if image_path.startswith('https://') or image_path.startswith('http://'):
45+
base64_string = url_image_to_base64(image_path)
46+
else:
47+
base64_string = get_image_base64(image_path)
48+
49+
conn = http.client.HTTPSConnection(API_URL)
50+
payload = json.dumps({
51+
"mode": "RELAX", # 模式 可选值:"Turbo"、"Fast"(默认)、"Relax"
52+
"botType": "NIJI_JOURNEY", # 模型类型,可选值 "MID_JOURNEY"(默认) 或者 "NIJI_JOURNEY"
53+
"prompt": "这是一个视频截图,请生成对应的吉卜力风格的图片", # 提示词,描述希望生成的图片内容
54+
"base64Array": [
55+
"data:image/png;base64," + base64_string # 包含图片 base64 数据的数组,格式为 "data:image/png;base64,<base64字符串>"
56+
],
57+
"notifyHook": "string" # 回调通知的 URL,处理完成后会向该地址发送回调
58+
})
59+
60+
# 设置请求头,包含认证信息和内容类型
61+
headers = {
62+
'Authorization': f'Bearer {DMX_API_TOKEN}',
63+
'Content-Type': 'application/json'
64+
}
65+
# 发送POST请求到/mj/submit/imagine接口
66+
conn.request("POST", "/mj/submit/imagine", payload, headers)
67+
# 获取响应
68+
res = conn.getresponse()
69+
data_json = json.loads(res.read().decode("utf-8"))
70+
# print(data_json)
71+
print(data_json["description"])
72+
task_id = data_json["result"]
73+
return task_id
74+
75+
if __name__ == "__main__":
76+
# image_path = "/Users/dmxapi/Desktop/dxmapi.jpg" # 本地图片方式生成
77+
image_url = "https://cdn.klingai.com/bs2/upload-kling-api/8089468206/image/Cl6kH2gHPegAAAAABUwweg-0_raw_image_0.png" # url 图片方式生成
78+
print(midjourney_generate_image(image_url))

0 commit comments

Comments
 (0)