[AI绘画] Stable Diffusion 如何把 上次生成的参数和全部配置保存成jason方便下次API使用?...

查看:35749 |回复:10 | 2023-7-28 12:18:55

您需要 登录 才可以下载或查看,没有账号?注册

x
image.png
修改

samples_log_stdout
为 true  ,注意必须小写
image.png




重启 启动器跑图



把控制台的这段复制下.就是全部数据了
这样就能把一次  出图 ,转换为一个API格式 的json了 !
这个可以发给API 重新生成 , 或者seed -1 变成随机.
  1. import json
  2. import base64

  3. #  '"init_images": [""]  是图生图才有的


  4. with open("img/A.jpg", "rb") as image_file:
  5.     encoded_string = base64.b64encode(image_file.read()).decode()
  6. b64 = encoded_string

  7. #print(b64)

  8. def format_json(json_str):
  9.     try:
  10.         # 解析JSON字符串为Python数据结构
  11.         data = json.loads(json_str)
  12.         # 转换为格式化的JSON字符串
  13.         formatted_json = json.dumps(data, indent=4, ensure_ascii=False)
  14.         return formatted_json
  15.     except json.JSONDecodeError as e:
  16.         print("JSON解析错误:", e)
  17.         return None

  18. def main():
  19.     # 读取txt文件
  20.     input_file_path = "txt/图生图API.txt"
  21.     with open(input_file_path, "r", encoding="utf-8") as f:
  22.         text_content = f.read()

  23.     # 在text_content的第一个 "{" 后面插入 "init_images": [""],然后拼接在一起
  24.     first_brace_index = text_content.find("{")
  25.     if first_brace_index >= 0:
  26.         modified_text_content = (
  27.             text_content[:first_brace_index+1] +
  28.             f'"init_images": ["{b64}"] , ' +   # 特别注意 逗号','
  29.             text_content[first_brace_index+1:]
  30.         )
  31.     else:
  32.         modified_text_content = text_content

  33.     # 使用format_json函数进行格式化
  34.     #print(modified_text_content)
  35.     formatted_json_str = format_json(modified_text_content)

  36.     if formatted_json_str:
  37.         # 保存为json文件
  38.         output_file_path = "json/img2img.json"
  39.         with open(output_file_path, "w", encoding="utf-8") as f:
  40.             f.write(formatted_json_str)
  41.             print("成功保存为:", output_file_path)

  42. if __name__ == "__main__":
  43.         main()
点击此处复制文本



评分

参与人数 6元素币 +35 活跃度 +16 展开 理由
雪无梦白 + 6 + 2 好贴子不应默默无闻,我先点了!
小毒爷 + 2 + 5 打开格局
b888888 + 8 + 1 这么6
玖阑枢 + 4 + 2 楼主靠谱
simon2014 + 8 + 1 成大触就需要这
yunliangfen + 7 + 5 每天一早上元素,挖矿撩妹两不误。

查看全部评分

2023-7-28 12:18:55  
 赞 赞 0

使用道具 登录

10个回答,把该问题分享到群,邀请大神一起回答。
2#
你可以使用以下方法将上一次生成的参数和全部配置保存为json方便下次使用:

1. 将控制台的输出复制下来,这将包含所有的参数和配置信息。

2. 新建一个文本文件,将复制的内容粘贴到文本文件中。

3. 将文本文件保存为json文件,并为其命名。

下次使用时,你可以直接读取这个json文件,以便获取之前保存的参数和配置信息。

English Translation:

You can save the parameters and configurations generated last time as a json file for easy reuse by following these steps:

1. Copy the console output, which contains all the parameters and configuration information.

2. Create a new text file and paste the copied content into it.

3. Save the text file as a json file with a name of your choice.

Next time, you can read this json file to retrieve the saved parameters and configurations.
   ——微元素 × GPT,助力游戏开发,CG创作的无限可能!  
回复 收起回复
2023-7-28 12:19:07   回复
 赞 赞 0

使用道具 登录

3#
满满的细节
回复 收起回复
2023-7-28 19:46:01   回复
 赞 赞 0

使用道具 登录

5#
分享使你快乐!
回复 收起回复
2023-8-14 16:27:01   回复
 赞 赞 0

使用道具 登录

7#
回复 收起回复
2024-11-4 09:33:58   回复
 赞 赞 0

使用道具 登录

8#
回复 收起回复
2024-11-5 22:29:22   回复
 赞 赞 0

使用道具 登录

9#
回复 收起回复
2024-11-6 16:42:07   回复
 赞 赞 0

使用道具 登录

10#
回复 收起回复
2024-11-10 13:56:04   回复
 赞 赞 0

使用道具 登录

11#
回复 收起回复
2024-11-12 10:42:01   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表