Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/modules/postprocessing.py
3055 views
1
import os
2
3
from PIL import Image
4
5
from modules import shared, images, devices, scripts, scripts_postprocessing, ui_common, infotext_utils
6
from modules.shared import opts
7
8
9
def run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output: bool = True):
10
devices.torch_gc()
11
12
shared.state.begin(job="extras")
13
14
outputs = []
15
16
def get_images(extras_mode, image, image_folder, input_dir):
17
if extras_mode == 1:
18
for img in image_folder:
19
if isinstance(img, Image.Image):
20
image = images.fix_image(img)
21
fn = ''
22
else:
23
image = images.read(os.path.abspath(img.name))
24
fn = os.path.splitext(img.orig_name)[0]
25
yield image, fn
26
elif extras_mode == 2:
27
assert not shared.cmd_opts.hide_ui_dir_config, '--hide-ui-dir-config option must be disabled'
28
assert input_dir, 'input directory not selected'
29
30
image_list = shared.listfiles(input_dir)
31
for filename in image_list:
32
yield filename, filename
33
else:
34
assert image, 'image not selected'
35
yield image, None
36
37
if extras_mode == 2 and output_dir != '':
38
outpath = output_dir
39
else:
40
outpath = opts.outdir_samples or opts.outdir_extras_samples
41
42
infotext = ''
43
44
data_to_process = list(get_images(extras_mode, image, image_folder, input_dir))
45
shared.state.job_count = len(data_to_process)
46
47
for image_placeholder, name in data_to_process:
48
image_data: Image.Image
49
50
shared.state.nextjob()
51
shared.state.textinfo = name
52
shared.state.skipped = False
53
54
if shared.state.interrupted or shared.state.stopping_generation:
55
break
56
57
if isinstance(image_placeholder, str):
58
try:
59
image_data = images.read(image_placeholder)
60
except Exception:
61
continue
62
else:
63
image_data = image_placeholder
64
65
image_data = image_data if image_data.mode in ("RGBA", "RGB") else image_data.convert("RGB")
66
67
parameters, existing_pnginfo = images.read_info_from_image(image_data)
68
if parameters:
69
existing_pnginfo["parameters"] = parameters
70
71
initial_pp = scripts_postprocessing.PostprocessedImage(image_data)
72
73
scripts.scripts_postproc.run(initial_pp, args)
74
75
if shared.state.skipped:
76
continue
77
78
used_suffixes = {}
79
for pp in [initial_pp, *initial_pp.extra_images]:
80
suffix = pp.get_suffix(used_suffixes)
81
82
if opts.use_original_name_batch and name is not None:
83
basename = os.path.splitext(os.path.basename(name))[0]
84
forced_filename = basename + suffix
85
else:
86
basename = ''
87
forced_filename = None
88
89
infotext = ", ".join([k if k == v else f'{k}: {infotext_utils.quote(v)}' for k, v in pp.info.items() if v is not None])
90
91
if opts.enable_pnginfo:
92
pp.image.info = existing_pnginfo
93
pp.image.info["postprocessing"] = infotext
94
95
shared.state.assign_current_image(pp.image)
96
97
if save_output:
98
fullfn, _ = images.save_image(pp.image, path=outpath, basename=basename, extension=opts.samples_format, info=infotext, short_filename=True, no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=existing_pnginfo, forced_filename=forced_filename, suffix=suffix)
99
100
if pp.caption:
101
caption_filename = os.path.splitext(fullfn)[0] + ".txt"
102
existing_caption = ""
103
try:
104
with open(caption_filename, encoding="utf8") as file:
105
existing_caption = file.read().strip()
106
except FileNotFoundError:
107
pass
108
109
action = shared.opts.postprocessing_existing_caption_action
110
if action == 'Prepend' and existing_caption:
111
caption = f"{existing_caption} {pp.caption}"
112
elif action == 'Append' and existing_caption:
113
caption = f"{pp.caption} {existing_caption}"
114
elif action == 'Keep' and existing_caption:
115
caption = existing_caption
116
else:
117
caption = pp.caption
118
119
caption = caption.strip()
120
if caption:
121
with open(caption_filename, "w", encoding="utf8") as file:
122
file.write(caption)
123
124
if extras_mode != 2 or show_extras_results:
125
outputs.append(pp.image)
126
127
devices.torch_gc()
128
shared.state.end()
129
return outputs, ui_common.plaintext_to_html(infotext), ''
130
131
132
def run_postprocessing_webui(id_task, *args, **kwargs):
133
return run_postprocessing(*args, **kwargs)
134
135
136
def run_extras(extras_mode, resize_mode, image, image_folder, input_dir, output_dir, show_extras_results, gfpgan_visibility, codeformer_visibility, codeformer_weight, upscaling_resize, upscaling_resize_w, upscaling_resize_h, upscaling_crop, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility, upscale_first: bool, save_output: bool = True, max_side_length: int = 0):
137
"""old handler for API"""
138
139
args = scripts.scripts_postproc.create_args_for_run({
140
"Upscale": {
141
"upscale_enabled": True,
142
"upscale_mode": resize_mode,
143
"upscale_by": upscaling_resize,
144
"max_side_length": max_side_length,
145
"upscale_to_width": upscaling_resize_w,
146
"upscale_to_height": upscaling_resize_h,
147
"upscale_crop": upscaling_crop,
148
"upscaler_1_name": extras_upscaler_1,
149
"upscaler_2_name": extras_upscaler_2,
150
"upscaler_2_visibility": extras_upscaler_2_visibility,
151
},
152
"GFPGAN": {
153
"enable": True,
154
"gfpgan_visibility": gfpgan_visibility,
155
},
156
"CodeFormer": {
157
"enable": True,
158
"codeformer_visibility": codeformer_visibility,
159
"codeformer_weight": codeformer_weight,
160
},
161
})
162
163
return run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output=save_output)
164
165