Repository navigation
Expand file tree
/
Copy pathimg2img.py
More file actions
161 lines (124 loc) · 4.83 KB
/
Copy pathimg2img.py
File metadata and controls
161 lines (124 loc) · 4.83 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'''
usage: python3 img2img.py <directory> <glob pattern>
i.e.: python3 img2img.py images **/*.png
puts out <original_filename>_img2img.png next to the original file
add more servers to SERVERS to process stuff in parallel
add to or change PAYLOAD to change image generation parameters
'''
import asyncio
import base64
import logging
import sys
from io import BytesIO
from pathlib import Path
from aiofiles import open, ospath
from aiohttp import ClientSession, ClientTimeout
from PIL import Image
from sdparsers import ParserManager
SERVERS = ["http://127.0.0.1:7860"]
PAYLOAD = {
"steps": 5,
"denoising_strength": 0.2
}
session_timeout = ClientTimeout(total=None, sock_connect=10, sock_read=600)
queue = asyncio.Queue()
parser = ParserManager()
parse_images = True
'''set to `False` to prevent the script from automatically populating the payload'''
async def worker(server_address):
'''one of these guys is run for each SERVERS entry'''
async with ClientSession(server_address, timeout=session_timeout) as session:
while True:
# get a filename from the queue
filename = await queue.get()
try:
# determine the output filename
# attention: the API does return the file type set in the backend options
# see txt2img.py for one approach of handling this situation
output_filename = filename.with_stem(filename.stem + "_img2img")
if await ospath.exists(output_filename):
raise ValueError("file already exists", output_filename)
# prepare the img2img payload
payload = await get_payload(filename)
# call the img2img API
images = await img2img(payload, session)
# save the output to disk
image_bytes = base64.b64decode(images[0])
async with open(output_filename, 'wb') as fp:
await fp.write(image_bytes)
except RuntimeError:
logging.exception("error interrogating file: %s", filename)
except Exception:
logging.exception("unexpected error")
queue.task_done()
async def img2img(payload: dict, session: ClientSession):
async with session.post('/sdapi/v1/img2img', json=payload) as response:
if not response.ok:
raise RuntimeError("error querying server", response.status, await response.text())
result = await response.json()
return result['images']
async def get_payload(image_filename: Path, custom_payload=PAYLOAD):
'''build a payload from a given image and a custom payload'''
# read image
async with open(image_filename, mode='rb') as fp:
image_bytes = await fp.read()
# get image parameters
with BytesIO(image_bytes) as buffered, Image.open(buffered) as image:
mime_type = image.get_format_mimetype()
image_parameters = get_image_params(image) or {}
image_parameters.update({
'height': image.height,
'width': image.width
})
# convert image to something we can POST to A1111
base64_image = base64.b64encode(image_bytes).decode('utf-8')
return {
**image_parameters,
**custom_payload,
# The A1111 does not need a mime type for now. As we have it though, let's use it!
"init_images": [f"data:{mime_type};base64,{base64_image}"]
}
def get_image_params(image):
'''parse image generation parameters from the given image'''
if not parse_images:
return
image_parameters = parser.parse(image)
if not image_parameters:
return
params = {}
try:
prompt, negative_prompt = image_parameters.prompts[0]
if prompt:
params['prompt'] = prompt.value
if negative_prompt:
params['negative_prompt'] = negative_prompt.value
except KeyError:
logging.warning("no prompt found")
try:
sampler = image_parameters.samplers[0]
params.update(sampler.parameters, sampler_index=sampler.name)
except KeyError:
logging.warning("no sampler found")
return params
async def run():
# create worker tasks
tasks = [asyncio.create_task(worker(server_address))
for server_address in SERVERS]
# wait for all files to be processed
await queue.join()
# shut down workers
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
async def main(directory, glob_pattern):
dir_path = Path(directory)
if not dir_path.exists():
raise ValueError("directory does not exist")
for filename in dir_path.glob(glob_pattern):
queue.put_nowait(filename)
await run()
if __name__ == "__main__":
try:
asyncio.run(main(*sys.argv[1:3]))
except TypeError as error:
print(str(error))