stable-diffusion-webui

Форк
0
/
postprocessing.py 
161 строка · 6.3 Кб
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 = img
21
                    fn = ''
22
                else:
23
                    image = Image.open(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:
55
            break
56

57
        if isinstance(image_placeholder, str):
58
            try:
59
                image_data = Image.open(image_placeholder)
60
            except Exception:
61
                continue
62
        else:
63
            image_data = image_placeholder
64

65
        parameters, existing_pnginfo = images.read_info_from_image(image_data)
66
        if parameters:
67
            existing_pnginfo["parameters"] = parameters
68

69
        initial_pp = scripts_postprocessing.PostprocessedImage(image_data.convert("RGB"))
70

71
        scripts.scripts_postproc.run(initial_pp, args)
72

73
        if shared.state.skipped:
74
            continue
75

76
        used_suffixes = {}
77
        for pp in [initial_pp, *initial_pp.extra_images]:
78
            suffix = pp.get_suffix(used_suffixes)
79

80
            if opts.use_original_name_batch and name is not None:
81
                basename = os.path.splitext(os.path.basename(name))[0]
82
                forced_filename = basename + suffix
83
            else:
84
                basename = ''
85
                forced_filename = None
86

87
            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])
88

89
            if opts.enable_pnginfo:
90
                pp.image.info = existing_pnginfo
91
                pp.image.info["postprocessing"] = infotext
92

93
            shared.state.assign_current_image(pp.image)
94

95
            if save_output:
96
                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)
97

98
                if pp.caption:
99
                    caption_filename = os.path.splitext(fullfn)[0] + ".txt"
100
                    existing_caption = ""
101
                    try:
102
                        with open(caption_filename, encoding="utf8") as file:
103
                            existing_caption = file.read().strip()
104
                    except FileNotFoundError:
105
                        pass
106

107
                    action = shared.opts.postprocessing_existing_caption_action
108
                    if action == 'Prepend' and existing_caption:
109
                        caption = f"{existing_caption} {pp.caption}"
110
                    elif action == 'Append' and existing_caption:
111
                        caption = f"{pp.caption} {existing_caption}"
112
                    elif action == 'Keep' and existing_caption:
113
                        caption = existing_caption
114
                    else:
115
                        caption = pp.caption
116

117
                    caption = caption.strip()
118
                    if caption:
119
                        with open(caption_filename, "w", encoding="utf8") as file:
120
                            file.write(caption)
121

122
            if extras_mode != 2 or show_extras_results:
123
                outputs.append(pp.image)
124

125
        image_data.close()
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):
137
    """old handler for API"""
138

139
    args = scripts.scripts_postproc.create_args_for_run({
140
        "Upscale": {
141
            "upscale_mode": resize_mode,
142
            "upscale_by": upscaling_resize,
143
            "upscale_to_width": upscaling_resize_w,
144
            "upscale_to_height": upscaling_resize_h,
145
            "upscale_crop": upscaling_crop,
146
            "upscaler_1_name": extras_upscaler_1,
147
            "upscaler_2_name": extras_upscaler_2,
148
            "upscaler_2_visibility": extras_upscaler_2_visibility,
149
        },
150
        "GFPGAN": {
151
            "enable": True,
152
            "gfpgan_visibility": gfpgan_visibility,
153
        },
154
        "CodeFormer": {
155
            "enable": True,
156
            "codeformer_visibility": codeformer_visibility,
157
            "codeformer_weight": codeformer_weight,
158
        },
159
    })
160

161
    return run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output=save_output)
162

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.