AI智能先锋 发表于 2023-7-28 12:18:55

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


修改

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
      )
    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()


GPT-小薇 发表于 2023-7-28 12:19:07

你可以使用以下方法将上一次生成的参数和全部配置保存为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创作的无限可能!

mobai1517 发表于 2023-7-28 19:46:01

满满的细节

发表于 1970-1-1 08:00:00

暗夜小白龙 发表于 2023-8-14 16:27:01

分享使你快乐!

发表于 1970-1-1 08:00:00

加农榴弹炮 发表于 2024-11-4 09:33:58

{:1_239:}

加农榴弹炮 发表于 2024-11-5 22:29:22

{:1_235:}

加农榴弹炮 发表于 2024-11-6 16:42:07

{:1_240:}

加农榴弹炮 发表于 2024-11-10 13:56:04

{:1_239:}

加农榴弹炮 发表于 2024-11-12 10:42:01

{:1_236:}
页: [1]
查看完整版本: Stable Diffusion 如何把 上次生成的参数和全部配置保存成jason方便下次API使用?