forked from guifaChild/text_to_vedio_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_to_image_midjourney.py
121 lines (101 loc) · 3.57 KB
/
data_to_image_midjourney.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding:utf-8 -*-
"""
作者:张贵发
日期:2023年07月08日
描述:
"""
import asyncio
import os.path
import pandas as pd
from midjourney.app .handler import prompt_handler
from midjourney.util._queue import taskqueue
from midjourney.lib.api import discord
import fileinput
import json
import requests
import json
# 构造请求的 URL 和 JSON 参数
def SaveImgFromUrl(response, save_path):
numOfOutput = len(response)
org_path = save_path
for i in range(numOfOutput):
save_path = org_path
img_content = requests.get(response[i]["url"]).content
if i >= 1:
save_path = save_path.split(".")[0] + "_" + str(i + 1) + "." + save_path.split(".")[1]
with open(save_path, "wb") as f:
f.write(img_content)
async def my_async_function(trigger_id,prompt):
loop = asyncio.get_running_loop()
# 在这里执行异步操作
taskqueue.put(trigger_id, discord.generate, prompt)
def CreateImage(description):
url = "http://127.0.0.1:8062/v1/api/trigger/imagine"
headers = {
"accept": "application/json",
"Content-Type": "application/json"
}
payload = {
"prompt": description
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
# 获取响应内容
response_data = response.json()
return "**<#"+response_data['trigger_id']+"#>"
def image_url(data_id_content,line):
if "fetch - Fetch: xxx, " in line and data_id_content in line and ".png" in line:
data_line = line.split("fetch - Fetch: xxx, ")[1]
result = data_line.split(", 'proxy_url': '")[1].split("', 'size':")[0]
print(result)
return result
else:
return ""
import requests
from PIL import Image
def save_image_from_url(url, save_path):
response = requests.get(url, stream=True)
response.raise_for_status()
with open(save_path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print("图像已保存:", save_path)
def load_image_data_midjourney(path):
print("最新的内容显示")
lists=[]
df = pd.read_csv(path)
newpath = path.split(".csv")[0].replace("data_prompt", "data_image")
if not os.path.exists(newpath):
os.makedirs(newpath)
for index, row in df.iterrows():
data_id_content = CreateImage(row["prompt"])
# 监听文件的内容变化
with open("midjourney/log/mj-api.log","r") as log_file :
label ="meiyou"
while True :
if label == "hanyou" :
break
lines = log_file.readlines()
for line in lines:
image_urls = image_url(data_id_content,line.strip())
if image_urls != "" and image_urls not in lists:
lists.append(image_urls)
label="hanyou"
break
for index, row in df.iterrows():
childpath = os.path.join(newpath,str(index)+".png")
save_image_from_url(lists[index],childpath)
return newpath
if __name__ == '__main__':
lists=[]
with open("midjourney/log/mj-api.log", "r") as log_file:
label = "meiyou"
while True:
if label == "hanyou":
break
lines = log_file.readlines()
for line in lines:
image_urls = image_url("<#6165796329#>", line.strip())
if image_urls != "" and image_urls not in lists:
lists.append(image_urls)
label = "hanyou"
break