You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
def encode_image(image_path):
|
|
|
|
with open(image_path, "rb") as i:
|
|
|
|
b64 = base64.b64encode(i.read())
|
|
|
|
return b64.decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
# 读取JSON文件
|
|
|
|
json_file = r'../JSON/28_1.json'
|
|
|
|
with open(json_file, 'r', encoding='utf-8') as file:
|
|
|
|
payload = json.load(file)
|
|
|
|
|
|
|
|
# 此处为读取一张图片作为输入图像
|
|
|
|
refer_img = encode_image('../Image/28/1.jpg')
|
|
|
|
source_img = encode_image('../Image/1ae93e2587822c291362abf9504cb49ec.jpeg')
|
|
|
|
|
|
|
|
payload["alwayson_scripts"]["ControlNet"]["args"][0]["input_image"] = source_img # 按用户输入的图片进行面部参考
|
|
|
|
payload["init_images"] = [refer_img] # 图生图,风格参考图是输入
|
|
|
|
payload["alwayson_scripts"]["ControlNet"]["args"][1]["input_image"] = refer_img
|
|
|
|
print(json.dumps(payload, sort_keys=False, ensure_ascii=False, indent=4)) # 生成可以正确显示中文的json格式输出
|
|
|
|
|
|
|
|
# API地址
|
|
|
|
url = "http://192.168.1.21:7860"
|
|
|
|
# response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
|
|
|
|
|
|
|
|
# if response.status_code != 200:
|
|
|
|
# print(response.text)
|
|
|
|
# exit(0)
|
|
|
|
# r = response.json()
|
|
|
|
|
|
|
|
# image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
|
|
|
|
|
|
|
|
|
|
|
|
# print(payload)
|
|
|
|
# image.show()
|
|
|
|
# image.save('output.png')
|