stable-diffusion-webui

Форк
0
73 строки · 2.9 Кб
1
from PIL import Image, ImageFilter, ImageOps
2

3

4
def get_crop_region(mask, pad=0):
5
    """finds a rectangular region that contains all masked ares in an image. Returns (x1, y1, x2, y2) coordinates of the rectangle.
6
    For example, if a user has painted the top-right part of a 512x512 image, the result may be (256, 0, 512, 256)"""
7
    mask_img = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
8
    box = mask_img.getbbox()
9
    if box:
10
        x1, y1, x2, y2 = box
11
    else:  # when no box is found
12
        x1, y1 = mask_img.size
13
        x2 = y2 = 0
14
    return max(x1 - pad, 0), max(y1 - pad, 0), min(x2 + pad, mask_img.size[0]), min(y2 + pad, mask_img.size[1])
15

16

17
def expand_crop_region(crop_region, processing_width, processing_height, image_width, image_height):
18
    """expands crop region get_crop_region() to match the ratio of the image the region will processed in; returns expanded region
19
    for example, if user drew mask in a 128x32 region, and the dimensions for processing are 512x512, the region will be expanded to 128x128."""
20

21
    x1, y1, x2, y2 = crop_region
22

23
    ratio_crop_region = (x2 - x1) / (y2 - y1)
24
    ratio_processing = processing_width / processing_height
25

26
    if ratio_crop_region > ratio_processing:
27
        desired_height = (x2 - x1) / ratio_processing
28
        desired_height_diff = int(desired_height - (y2-y1))
29
        y1 -= desired_height_diff//2
30
        y2 += desired_height_diff - desired_height_diff//2
31
        if y2 >= image_height:
32
            diff = y2 - image_height
33
            y2 -= diff
34
            y1 -= diff
35
        if y1 < 0:
36
            y2 -= y1
37
            y1 -= y1
38
        if y2 >= image_height:
39
            y2 = image_height
40
    else:
41
        desired_width = (y2 - y1) * ratio_processing
42
        desired_width_diff = int(desired_width - (x2-x1))
43
        x1 -= desired_width_diff//2
44
        x2 += desired_width_diff - desired_width_diff//2
45
        if x2 >= image_width:
46
            diff = x2 - image_width
47
            x2 -= diff
48
            x1 -= diff
49
        if x1 < 0:
50
            x2 -= x1
51
            x1 -= x1
52
        if x2 >= image_width:
53
            x2 = image_width
54

55
    return x1, y1, x2, y2
56

57

58
def fill(image, mask):
59
    """fills masked regions with colors from image using blur. Not extremely effective."""
60

61
    image_mod = Image.new('RGBA', (image.width, image.height))
62

63
    image_masked = Image.new('RGBa', (image.width, image.height))
64
    image_masked.paste(image.convert("RGBA").convert("RGBa"), mask=ImageOps.invert(mask.convert('L')))
65

66
    image_masked = image_masked.convert('RGBa')
67

68
    for radius, repeats in [(256, 1), (64, 1), (16, 2), (4, 4), (2, 2), (0, 1)]:
69
        blurred = image_masked.filter(ImageFilter.GaussianBlur(radius)).convert('RGBA')
70
        for _ in range(repeats):
71
            image_mod.alpha_composite(blurred)
72

73
    return image_mod.convert("RGB")
74

75

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

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

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

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