Python 使用 Stable Diffusion API 生成图片示例

Python 使用 Stable Diffusion API 生成图片示例

    正在检查是否收录...



代码:

import base64 import datetime import json import os import requests def submit_post(url: str, data: dict): """ Submit a POST request to the given URL with the given data. :param url: url :param data: data :return: response """ return requests.post(url, data=json.dumps(data)) def save_encoded_image(b64_image: str, output_path: str): """ Save the given image to the given output path. :param b64_image: base64 encoded image :param output_path: output path :return: None """ # 判断当前目录下是否存在 output 文件夹,如果不存在则创建 if not os.path.exists("output"): os.mkdir("output") timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S") output_path = f"{output_path}_{timestamp}" + ".png" # 将文件放入当前目录下的 output 文件夹中 output_path = f"output/{output_path}" with open(output_path, "wb") as f: f.write(base64.b64decode(b64_image)) def save_json_file(data: dict, output_path: str): """ Save the given data to the given output path. :param data: data :param output_path: output path :return: None """ # 忽略 data 中的 images 字段 data.pop('images') # 将 data 中的 info 字段转为 json 字符串,info 当前数据需要转义 data['info'] = json.loads(data['info']) # 输出 data.info.infotexts info_texts = data['info']['infotexts'] for info_text in info_texts: print(info_text) # 判断当前目录下是否存在 output 文件夹,如果不存在则创建 if not os.path.exists("output"): os.mkdir("output") timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S") output_path = f"{output_path}_{timestamp}" + ".json" # 将文件放入当前目录下的 output 文件夹中 output_path = f"output/{output_path}" with open(output_path, "w") as f: json.dump(data, f, indent=4, ensure_ascii=False) if __name__ == '__main__': """ Example usage: python3 txt2img.py """ txt2img_url = "http://192.168.2.82:7860/sdapi/v1/txt2img" # 服务器地址 prompt = input("请输入提示词:") negative_prompt = input("请输入反面提示词:") data = {'prompt': prompt, 'negative_prompt': negative_prompt} # 将 data.prompt 中的文本,删除文件名非法字符,已下划线分隔,作为文件名 output_path = data['prompt'].replace(" ", "_").replace("/", "_").replace("\\", "_").replace(":", "_").replace("\"", "_").replace( "<", "_").replace(">", "_").replace("|", "_") response = submit_post(txt2img_url, data) save_encoded_image(response.json()['images'][0], output_path) save_json_file(response.json(), output_path) 

输出:

>> python txt2img.py 请输入提示词:masterpiece, best quality, 1boy, orc, male focus, mecha, robot, science fiction, solo, black background <lora:nijiMecha:1> <lora:wzh-000022:1> 请输入反面提示词:(worst quality, low quality:1.4), greyscale, monochrome, watermark, signature masterpiece, best quality, 1boy, orc, male focus, mecha, robot, science fiction, solo, black background <lora:nijiMecha:1> <lora:wzh-000022:1> Negative prompt: (worst quality, low quality:1.4), greyscale, monochrome, watermark, signature Steps: 50, Sampler: Euler, CFG scale: 7.0, Seed: 274772779, Size: 512x512, Model hash: ce67b54cf2, Model: oukaNiji5_v10, Seed resize from: -1x-1, Denoising strength: 0, Clip skip: 2 

说明:

运行后,图片以及 JSON 将会输出到当前目录下 output 中;

TIP:

当然前提是你已经部署好 Stable Diffusion API 服务; 并且安装好跟我一样的模型以及一些相关的 LoRA
print('HELLO WORLD') # http://www.skyner.cn 

tpucodejsonprompturl提示词文件夹apierpbotchromepython服务器diffusionclipamlapi 服务clistable diffusionpng
  • 本文作者:李琛
  • 本文链接: https://wapzz.net/post-1554.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.3W+
5
0
1
WAP站长官方

CPU硬解Stable-Diffusion

上一篇

LLMs:Chinese-LLaMA-Alpaca-2(基于deepspeed框架)的简介、安装、案例实战应用之详细攻略

下一篇
  • 复制图片
按住ctrl可打开默认菜单