2022-09-20 06:13:12 +08:00
|
|
|
import os
|
|
|
|
|
2022-09-11 16:31:16 +08:00
|
|
|
from PIL import Image
|
|
|
|
|
2024-01-01 22:25:30 +08:00
|
|
|
from modules import shared, images, devices, scripts, scripts_postprocessing, ui_common, infotext_utils
|
2022-09-11 16:31:16 +08:00
|
|
|
from modules.shared import opts
|
|
|
|
|
2022-10-29 05:36:25 +08:00
|
|
|
|
2023-12-04 18:11:00 +08:00
|
|
|
def run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output: bool = True):
|
2022-09-12 04:24:24 +08:00
|
|
|
devices.torch_gc()
|
2022-09-11 16:31:16 +08:00
|
|
|
|
2023-06-30 18:11:31 +08:00
|
|
|
shared.state.begin(job="extras")
|
2023-01-03 23:34:51 +08:00
|
|
|
|
2022-10-16 12:50:55 +08:00
|
|
|
outputs = []
|
2022-12-04 14:13:36 +08:00
|
|
|
|
2023-08-11 23:32:12 +08:00
|
|
|
def get_images(extras_mode, image, image_folder, input_dir):
|
|
|
|
if extras_mode == 1:
|
|
|
|
for img in image_folder:
|
|
|
|
if isinstance(img, Image.Image):
|
2024-03-05 00:14:53 +08:00
|
|
|
image = images.fix_image(img)
|
2023-08-11 23:32:12 +08:00
|
|
|
fn = ''
|
|
|
|
else:
|
2024-03-05 00:14:53 +08:00
|
|
|
image = images.read(os.path.abspath(img.name))
|
2023-08-11 23:32:12 +08:00
|
|
|
fn = os.path.splitext(img.orig_name)[0]
|
|
|
|
yield image, fn
|
|
|
|
elif extras_mode == 2:
|
|
|
|
assert not shared.cmd_opts.hide_ui_dir_config, '--hide-ui-dir-config option must be disabled'
|
|
|
|
assert input_dir, 'input directory not selected'
|
|
|
|
|
|
|
|
image_list = shared.listfiles(input_dir)
|
|
|
|
for filename in image_list:
|
2023-12-02 23:01:11 +08:00
|
|
|
yield filename, filename
|
2023-08-11 23:32:12 +08:00
|
|
|
else:
|
|
|
|
assert image, 'image not selected'
|
|
|
|
yield image, None
|
2022-09-11 16:31:16 +08:00
|
|
|
|
2022-10-16 12:50:55 +08:00
|
|
|
if extras_mode == 2 and output_dir != '':
|
|
|
|
outpath = output_dir
|
|
|
|
else:
|
|
|
|
outpath = opts.outdir_samples or opts.outdir_extras_samples
|
2022-09-11 16:31:16 +08:00
|
|
|
|
2023-01-23 14:24:43 +08:00
|
|
|
infotext = ''
|
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
data_to_process = list(get_images(extras_mode, image, image_folder, input_dir))
|
|
|
|
shared.state.job_count = len(data_to_process)
|
|
|
|
|
|
|
|
for image_placeholder, name in data_to_process:
|
2023-08-11 23:32:12 +08:00
|
|
|
image_data: Image.Image
|
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
shared.state.nextjob()
|
2023-01-23 14:24:43 +08:00
|
|
|
shared.state.textinfo = name
|
2023-12-02 23:01:11 +08:00
|
|
|
shared.state.skipped = False
|
|
|
|
|
|
|
|
if shared.state.interrupted:
|
|
|
|
break
|
|
|
|
|
|
|
|
if isinstance(image_placeholder, str):
|
|
|
|
try:
|
2024-03-05 00:14:53 +08:00
|
|
|
image_data = images.read(image_placeholder)
|
2023-12-02 23:01:11 +08:00
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
image_data = image_placeholder
|
|
|
|
|
2024-04-30 18:17:53 +08:00
|
|
|
image_data = image_data if image_data.mode in ("RGBA", "RGB") else image_data.convert("RGB")
|
|
|
|
|
2023-08-11 23:32:12 +08:00
|
|
|
parameters, existing_pnginfo = images.read_info_from_image(image_data)
|
2023-07-03 18:08:28 +08:00
|
|
|
if parameters:
|
|
|
|
existing_pnginfo["parameters"] = parameters
|
2022-10-10 09:26:52 +08:00
|
|
|
|
2024-04-30 18:17:53 +08:00
|
|
|
initial_pp = scripts_postprocessing.PostprocessedImage(image_data)
|
2022-09-11 16:31:16 +08:00
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
scripts.scripts_postproc.run(initial_pp, args)
|
2023-01-23 14:24:43 +08:00
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
if shared.state.skipped:
|
|
|
|
continue
|
|
|
|
|
|
|
|
used_suffixes = {}
|
|
|
|
for pp in [initial_pp, *initial_pp.extra_images]:
|
|
|
|
suffix = pp.get_suffix(used_suffixes)
|
|
|
|
|
|
|
|
if opts.use_original_name_batch and name is not None:
|
|
|
|
basename = os.path.splitext(os.path.basename(name))[0]
|
|
|
|
forced_filename = basename + suffix
|
|
|
|
else:
|
|
|
|
basename = ''
|
|
|
|
forced_filename = None
|
|
|
|
|
2024-01-01 22:25:30 +08:00
|
|
|
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])
|
2023-12-02 23:01:11 +08:00
|
|
|
|
|
|
|
if opts.enable_pnginfo:
|
|
|
|
pp.image.info = existing_pnginfo
|
|
|
|
pp.image.info["postprocessing"] = infotext
|
|
|
|
|
2024-01-20 04:40:03 +08:00
|
|
|
shared.state.assign_current_image(pp.image)
|
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
if save_output:
|
|
|
|
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)
|
2022-10-20 18:10:32 +08:00
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
if pp.caption:
|
|
|
|
caption_filename = os.path.splitext(fullfn)[0] + ".txt"
|
2024-01-04 06:16:58 +08:00
|
|
|
existing_caption = ""
|
|
|
|
try:
|
2023-12-02 23:01:11 +08:00
|
|
|
with open(caption_filename, encoding="utf8") as file:
|
|
|
|
existing_caption = file.read().strip()
|
2024-01-04 06:16:58 +08:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2023-01-03 23:34:51 +08:00
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
action = shared.opts.postprocessing_existing_caption_action
|
|
|
|
if action == 'Prepend' and existing_caption:
|
|
|
|
caption = f"{existing_caption} {pp.caption}"
|
|
|
|
elif action == 'Append' and existing_caption:
|
|
|
|
caption = f"{pp.caption} {existing_caption}"
|
|
|
|
elif action == 'Keep' and existing_caption:
|
|
|
|
caption = existing_caption
|
|
|
|
else:
|
|
|
|
caption = pp.caption
|
2022-12-17 20:31:03 +08:00
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
caption = caption.strip()
|
|
|
|
if caption:
|
|
|
|
with open(caption_filename, "w", encoding="utf8") as file:
|
|
|
|
file.write(caption)
|
2022-09-11 16:31:16 +08:00
|
|
|
|
2023-12-02 23:01:11 +08:00
|
|
|
if extras_mode != 2 or show_extras_results:
|
|
|
|
outputs.append(pp.image)
|
2022-09-16 17:43:24 +08:00
|
|
|
|
2022-09-29 09:14:13 +08:00
|
|
|
devices.torch_gc()
|
2023-11-15 13:26:37 +08:00
|
|
|
shared.state.end()
|
2023-01-23 14:24:43 +08:00
|
|
|
return outputs, ui_common.plaintext_to_html(infotext), ''
|
|
|
|
|
|
|
|
|
2023-12-04 18:11:00 +08:00
|
|
|
def run_postprocessing_webui(id_task, *args, **kwargs):
|
|
|
|
return run_postprocessing(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2024-04-02 04:33:45 +08:00
|
|
|
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):
|
2023-01-23 14:24:43 +08:00
|
|
|
"""old handler for API"""
|
|
|
|
|
|
|
|
args = scripts.scripts_postproc.create_args_for_run({
|
|
|
|
"Upscale": {
|
2024-04-09 11:13:47 +08:00
|
|
|
"upscale_enabled": True,
|
2023-01-23 14:24:43 +08:00
|
|
|
"upscale_mode": resize_mode,
|
|
|
|
"upscale_by": upscaling_resize,
|
2024-04-02 04:33:45 +08:00
|
|
|
"max_side_length": max_side_length,
|
2023-01-23 14:24:43 +08:00
|
|
|
"upscale_to_width": upscaling_resize_w,
|
|
|
|
"upscale_to_height": upscaling_resize_h,
|
|
|
|
"upscale_crop": upscaling_crop,
|
|
|
|
"upscaler_1_name": extras_upscaler_1,
|
|
|
|
"upscaler_2_name": extras_upscaler_2,
|
|
|
|
"upscaler_2_visibility": extras_upscaler_2_visibility,
|
|
|
|
},
|
|
|
|
"GFPGAN": {
|
2023-12-02 23:01:11 +08:00
|
|
|
"enable": True,
|
2023-01-23 14:24:43 +08:00
|
|
|
"gfpgan_visibility": gfpgan_visibility,
|
|
|
|
},
|
|
|
|
"CodeFormer": {
|
2023-12-02 23:01:11 +08:00
|
|
|
"enable": True,
|
2023-01-23 14:24:43 +08:00
|
|
|
"codeformer_visibility": codeformer_visibility,
|
|
|
|
"codeformer_weight": codeformer_weight,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-12-04 18:11:00 +08:00
|
|
|
return run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output=save_output)
|