|
import base64, io, requests, json |
|
from PIL import Image, PngImagePlugin |
|
from datetime import datetime, date |
|
|
|
address = 'http://127.0.0.1:7860' |
|
input_file = "extensions\sd-webui-reactor\example\IamSFW.jpg" |
|
time = datetime.now() |
|
today = date.today() |
|
current_date = today.strftime('%Y-%m-%d') |
|
current_time = time.strftime('%H-%M-%S') |
|
output = 'outputs/api/output_'+current_date+'_'+current_time |
|
try: |
|
im = Image.open(input_file) |
|
except Exception as e: |
|
print(e) |
|
finally: |
|
print(im) |
|
|
|
img_bytes = io.BytesIO() |
|
im.save(img_bytes, format='PNG') |
|
img_base64 = base64.b64encode(img_bytes.getvalue()).decode('utf-8') |
|
|
|
|
|
args=[ |
|
img_base64, |
|
True, |
|
'0', |
|
'0', |
|
'C:\stable-diffusion-webui\models\insightface\inswapper_128.onnx', |
|
'CodeFormer', |
|
1, |
|
True, |
|
'4x_NMKD-Superscale-SP_178000_G', |
|
2, |
|
1, |
|
False, |
|
True, |
|
1, |
|
0, |
|
0, |
|
False, |
|
0.8, |
|
] |
|
|
|
|
|
|
|
|
|
prompt = "(8k, best quality, masterpiece, highly detailed:1.1),realistic photo of fantastic happy woman,hairstyle of blonde and red short bob hair,modern clothing,cinematic lightning,film grain,dynamic pose,bokeh,dof" |
|
|
|
neg = "ng_deepnegative_v1_75t,(badhandv4:1.2),(worst quality:2),(low quality:2),(normal quality:2),lowres,(bad anatomy),(bad hands),((monochrome)),((grayscale)),(verybadimagenegative_v1.3:0.8),negative_hand-neg,badhandv4,nude,naked,(strabismus),cross-eye,heterochromia,((blurred))" |
|
|
|
payload = { |
|
"prompt": prompt, |
|
"negative_prompt": neg, |
|
"seed": -1, |
|
"sampler_name": "DPM++ 2M Karras", |
|
"steps": 15, |
|
"cfg_scale": 7, |
|
"width": 512, |
|
"height": 768, |
|
"restore_faces": False, |
|
"alwayson_scripts": {"reactor":{"args":args}} |
|
} |
|
|
|
try: |
|
print('Working... Please wait...') |
|
result = requests.post(url=f'{address}/sdapi/v1/txt2img', json=payload) |
|
except Exception as e: |
|
print(e) |
|
finally: |
|
print('Done! Saving file...') |
|
|
|
if result is not None: |
|
r = result.json() |
|
n = 0 |
|
|
|
for i in r['images']: |
|
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) |
|
|
|
png_payload = { |
|
"image": "data:image/png;base64," + i |
|
} |
|
response2 = requests.post(url=f'{address}/sdapi/v1/png-info', json=png_payload) |
|
|
|
pnginfo = PngImagePlugin.PngInfo() |
|
pnginfo.add_text("parameters", response2.json().get("info")) |
|
output_file = output+'_'+str(n)+'_.png' |
|
try: |
|
image.save(output_file, pnginfo=pnginfo) |
|
except Exception as e: |
|
print(e) |
|
finally: |
|
print(f'{output_file} is saved\nAll is done!') |
|
n += 1 |
|
else: |
|
print('Something went wrong...') |
|
|