-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautomatic1111.py
146 lines (116 loc) · 4.75 KB
/
automatic1111.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import base64
import requests
from PIL import Image
from io import BytesIO
import json
import argparse
from utils import to_image_url
MAX_DIMENSION = 2048
def create_img2img_payload(
input_image, positive_prompt, negative_prompt,
mask_image=None,
strength=0.8,
steps=20, cfg_scale=8.0):
"""
Create a payload for the img2img API.
Args:
input_image (PIL.Image.Image): The input image.
positive_prompt (str): The positive prompt for generating the output image.
negative_prompt (str): The negative prompt for generating the output image.
mask_image (PIL.Image.Image, optional): The mask for the input image. Defaults to None.
strength (float, optional): The denoising strength. Defaults to 0.8.
steps (int, optional): The number of steps for generating the output image. Defaults to 20.
cfg_scale (float, optional): The scale factor for the configuration. Defaults to 8.0.
Returns:
dict: The payload for the img2img API.
"""
width, height = input_image.size
if width > MAX_DIMENSION or height > MAX_DIMENSION:
# resize the image so the largest dimension is MAX_DIMENSION
if width > height:
new_width = MAX_DIMENSION
new_height = int(MAX_DIMENSION * height / width)
else:
new_height = MAX_DIMENSION
new_width = int(MAX_DIMENSION * width / height)
input_image = input_image.resize((new_width, new_height))
mask_image = mask_image.resize((new_width, new_height))
payload = {
"init_images": [
to_image_url(input_image)
],
"prompt": positive_prompt,
"negative_prompt": negative_prompt,
"denoising_strength": strength,
"width": width,
"height": height,
"steps": steps,
"cfg_scale": cfg_scale,
"mask_blur_x": 0, # if we provide a mask it's already blurred
"mask_blur_y": 0,
"mask_blur": 0,
}
if mask_image is not None:
payload["mask"] = to_image_url(mask_image)
return payload
def make_img2img_request(server_address, payload):
"""
Sends an img2img request to the automatic1111 API endpoint.
Args:
server_address (str): The address:port of the server to send the request to.
payload (dict): The payload generated by create_img2img_payload.
Returns:
list: A list of images returned in the response.
Raises:
requests.exceptions.Timeout: If the request times out.
"""
server_url = f"http://{server_address}/sdapi/v1/img2img"
payload = json.dumps(payload)
response = requests.post(url=server_url, data=payload, timeout=500).json()
return response.get("images")
def make_models_request(server_address):
"""
Sends a request to the automatic1111 API endpoint to get the available models.
Args:
server_address (str): The address:port of the server to send the request to.
Returns:
list: A list of available models.
Raises:
requests.exceptions.Timeout: If the request times out.
"""
server_url = f"http://{server_address}/sdapi/v1/sd-models"
response = requests.get(url=server_url, timeout=3).json()
if not isinstance(response, list):
return None
return [entry["model_name"] for entry in response]
def main():
# Parse arguments - input image, positive prompt, negative prompt, server address
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-i', '--input-image', type=str,
help='Path to the input image')
parser.add_argument('-m', '--mask-image', type=str,
default=None,
help='Path to the mask image')
parser.add_argument('-p', '--prompt', type=str, help='Positive prompt')
parser.add_argument('-n', '--negative-prompt', type=str,
default='', help='Negative prompt')
parser.add_argument('-e', '--steps', type=int,
default=30, help='Number of steps')
parser.add_argument('-s', '--server', type=str, help='Server address')
args = parser.parse_args()
image = Image.open(args.input_image)
mask_image = None
if args.mask_image is not None:
mask_image = Image.open(args.mask_image)
payload = create_img2img_payload(
image, args.prompt, args.negative_prompt, mask_image=mask_image, steps=args.steps)
images = make_img2img_request(args.server, payload)
if images is None:
print("No response from server")
else:
for image in images:
image = Image.open(BytesIO(base64.b64decode(image)))
image.show()
input("Press Enter to continue...")
if __name__ == '__main__':
main()