stable-diffusion-webui

Форк
0
/
postprocessing_split_oversized.py 
70 строк · 2.4 Кб
1
import math
2

3
from modules import scripts_postprocessing, ui_components
4
import gradio as gr
5

6

7
def split_pic(image, inverse_xy, width, height, overlap_ratio):
8
    if inverse_xy:
9
        from_w, from_h = image.height, image.width
10
        to_w, to_h = height, width
11
    else:
12
        from_w, from_h = image.width, image.height
13
        to_w, to_h = width, height
14
    h = from_h * to_w // from_w
15
    if inverse_xy:
16
        image = image.resize((h, to_w))
17
    else:
18
        image = image.resize((to_w, h))
19

20
    split_count = math.ceil((h - to_h * overlap_ratio) / (to_h * (1.0 - overlap_ratio)))
21
    y_step = (h - to_h) / (split_count - 1)
22
    for i in range(split_count):
23
        y = int(y_step * i)
24
        if inverse_xy:
25
            splitted = image.crop((y, 0, y + to_h, to_w))
26
        else:
27
            splitted = image.crop((0, y, to_w, y + to_h))
28
        yield splitted
29

30

31
class ScriptPostprocessingSplitOversized(scripts_postprocessing.ScriptPostprocessing):
32
    name = "Split oversized images"
33
    order = 4000
34

35
    def ui(self):
36
        with ui_components.InputAccordion(False, label="Split oversized images") as enable:
37
            with gr.Row():
38
                split_threshold = gr.Slider(label='Threshold', value=0.5, minimum=0.0, maximum=1.0, step=0.05, elem_id="postprocess_split_threshold")
39
                overlap_ratio = gr.Slider(label='Overlap ratio', value=0.2, minimum=0.0, maximum=0.9, step=0.05, elem_id="postprocess_overlap_ratio")
40

41
        return {
42
            "enable": enable,
43
            "split_threshold": split_threshold,
44
            "overlap_ratio": overlap_ratio,
45
        }
46

47
    def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, split_threshold, overlap_ratio):
48
        if not enable:
49
            return
50

51
        width = pp.shared.target_width
52
        height = pp.shared.target_height
53

54
        if not width or not height:
55
            return
56

57
        if pp.image.height > pp.image.width:
58
            ratio = (pp.image.width * height) / (pp.image.height * width)
59
            inverse_xy = False
60
        else:
61
            ratio = (pp.image.height * width) / (pp.image.width * height)
62
            inverse_xy = True
63

64
        if ratio >= 1.0 or ratio > split_threshold:
65
            return
66

67
        result, *others = split_pic(pp.image, inverse_xy, width, height, overlap_ratio)
68

69
        pp.image = result
70
        pp.extra_images = [pp.create_copy(x) for x in others]
71

72

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

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

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

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