实现效果
运行mj.py,如下所示。输入中文,自动生成提示词,自动开始下载。用户选择是否需要变换图片,选择需要对哪个图片变换,自动保存。
之前想做一个网页版,只实现了demo效果不好看,就不开放了。
实现步骤
- 提交任务
- 查询任务图片
提交任务demo:
import requests
import json
url = "xxxxx/mj/submit/imagine"
secret_key = ""
headers = {
"Authorization": secret_key,
"Content-Type": "application/json"
}
data = {
"base64Array": [],
"notifyHook": "",
"prompt": "a stunningly beautiful princess dressed in a light silk allur dress,flirty open eyes, photography, front-up composition ,unsplash",
"state": ""
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print(response.text)
else:
print(f"Error: {response.status_code} - {response.text}")
# 任务ID结果在result
# {"code":1,"description":"提交成功","result":"1699256705394703","properties":{"discordInstanceId":"1170599634478039134"}}
紧接着查询任务:
# coding=gbk
import requests
task_id = "1699325243934018" # 任务ID
url = f"xxxxx/mj/task/{task_id}/fetch"
secret_key = "xxx"
headers = {
"Authorization": secret_key
}
response = requests.get(url, headers=headers)
# 当progress参数为处理进度,如果能实时返回就好了
if response.status_code == 200:
task_details = response.json()
print(task_details)
else:
print(f"Error: {response.status_code} - {response.text}")
# 提示词在prompt中,图片在imageUrl参数中,progress为完成状态的进度
# {'id': '1699256705394703', 'action': 'IMAGINE', 'prompt': 'beautiful chinese girl', 'promptEn': 'beautiful chinese girl', 'description': '提交成功', 'state': '', 'submitTime': 1699256705394, 'startTime': 1699256707640, 'finishTime': 1699256743786, 'imageUrl': 'https://api.aigcbest.top/mj/image/1699256705394703', 'status': 'SUCCESS', 'progress': '100%', 'failReason': ''}
完整源码
开源地址:Midjourney