SD Web UI搭建
一、介绍
1、该服务是基于SD Web UI搭建而成,采用sd-v1-5-inpainting模型,提供给AI Art Generator、AI Avatar项目使用,支持文字转图片、文字+图片转图片功能;
2、UI通过Web UI界面,调整参数,生成效果图;后台通过调用API接口传递UI调整的参数,生成所需要的图片,提供给前端使用;
二、搭建步骤
2、启动服务时(上述代码的最后一句),添加–api参数;
python launch.py --xformers --enable-insecure-extension-access --theme dark --api # 本地服务
python launch.py --listen --xformers --enable-insecure-extension-access --theme dark --api # 外部服务
三、API调用
1、文字转图片
import requests
import io
import base64
from PIL import Image
def txt2img():
url = "http://xx.com"
payload = {
"prompt": "cat",
"negative_prompt": "xx",
"steps": 20,
}
response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
images = response.json()['images']
if len(images) > 0:
image_data = images[0]
image = Image.open(io.BytesIO(base64.b64decode(image_data.split(",", 1)[0])))
image.save('output1.png')
txt2img()
2、文字+图片转图片
import requests
import io
import base64
from PIL import Image
def img2img():
url = "http://xx.com"
payload = {
"prompt": "cat",
"negative_prompt": "xx",
"steps": 20,
"init_images": ["data:image/png;base64," + base64.b64encode(open('input.png', 'rb').read()).decode('utf-8')]
}
response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
images = response.json()['images']
if len(images) > 0:
image_data = images[0]
image = Image.open(io.BytesIO(base64.b64decode(image_data.split(",", 1)[0])))
image.save('output1.png')
img2img()
3、更详细的文字+图片转图片
import requests
import io
import base64
from PIL import Image
def img2img_more():
url = "http://xx.com"
prompt = "((watercolor)), colorful, close-up, front, extreme detail, detailed, 8k, nice face, Portrait, flowing, fresh"
negative_prompt = "ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, bad anatomy, watermark, signature, cut off, low contrast, underexposed, overexposed, bad art, beginner, amateur, distorted face, blurry, draft, grainy"
payload = {
"prompt": prompt,
"negative_prompt": negative_prompt,
"steps": 30,
"cfg_scale": 15.0,
"denoising_strength": 0.65,
"sampler_name": "Euler a",
"inpainting_mask_weight": 0.35,
"extra_generation_params": {"Mask blur": 4},
"seed_resize_from_w": -1,
"seed_resize_from_h": -1,
"init_images": ["data:image/png;base64," + base64.b64encode(open('xx.png', 'rb').read()).decode('utf-8')]
}
response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
images = response.json()['images']
if len(images) > 0:
image_data = images[0]
image = Image.open(io.BytesIO(base64.b64decode(image_data.split(",", 1)[0])))
image.save('output1.png')
img2img_more()
参考:https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API