您需要 登录 才可以下载或查看,没有账号?注册
x
修改
samples_log_stdout
为 true ,注意必须小写
重启 启动器跑图
把控制台的这段复制下.就是全部数据了
这样就能把一次 出图 ,转换为一个API格式 的json了 !
这个可以发给API 重新生成 , 或者seed -1 变成随机.
- import json
- import base64
- # '"init_images": [""] 是图生图才有的
- with open("img/A.jpg", "rb") as image_file:
- encoded_string = base64.b64encode(image_file.read()).decode()
- b64 = encoded_string
- #print(b64)
- def format_json(json_str):
- try:
- # 解析JSON字符串为Python数据结构
- data = json.loads(json_str)
- # 转换为格式化的JSON字符串
- formatted_json = json.dumps(data, indent=4, ensure_ascii=False)
- return formatted_json
- except json.JSONDecodeError as e:
- print("JSON解析错误:", e)
- return None
- def main():
- # 读取txt文件
- input_file_path = "txt/图生图API.txt"
- with open(input_file_path, "r", encoding="utf-8") as f:
- text_content = f.read()
- # 在text_content的第一个 "{" 后面插入 "init_images": [""],然后拼接在一起
- first_brace_index = text_content.find("{")
- if first_brace_index >= 0:
- modified_text_content = (
- text_content[:first_brace_index+1] +
- f'"init_images": ["{b64}"] , ' + # 特别注意 逗号','
- text_content[first_brace_index+1:]
- )
- else:
- modified_text_content = text_content
- # 使用format_json函数进行格式化
- #print(modified_text_content)
- formatted_json_str = format_json(modified_text_content)
- if formatted_json_str:
- # 保存为json文件
- output_file_path = "json/img2img.json"
- with open(output_file_path, "w", encoding="utf-8") as f:
- f.write(formatted_json_str)
- print("成功保存为:", output_file_path)
- if __name__ == "__main__":
- main()
点击此处复制文本
|