File size: 3,852 Bytes
34dc268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import numpy as np
import random, os
from PIL import Image



def merge_images_horizontally(img1, img2):
    
    img1_width, img1_height = img1.size
    img2_width, img2_height = img2.size

    merged_width = img1_width + img2_width
    merged_height = max(img1_height, img2_height)
    merged_image = Image.new('RGBA', (merged_width + 20, merged_height))

   
    merged_image.paste(img1, (0, 0))
    merged_image.paste(img2, (img1_width + 20, 0))
  
    return merged_image
   


def outpainting_generator_rectangle(image, box_width_ratio=0.35, mask_random_start=125):
    '''
    image: PIL Image file
    sqr: True or False, decide the shape of cropped images
    '''
    
    # sqr = random.choice([True, False])
    
    image = image.resize((512, 512))
    width, height = image.size  
   
    box_height = height 
    box_width = width * box_width_ratio #* random.uniform(0.35, 0.4)
    
    x = np.random.randint(0, int(width - box_width))
    y = 0

    left = x
    upper = y
    right = x + box_width
    lower = y + box_height

    box = (left, upper, right, lower)
    small_image = image.crop(box)

    large_box_width, large_box_height = 512, 512  
    small_box_height = 512
    small_box_width = 256
    small_image = small_image.resize((small_box_width, small_box_height))


    large_image = Image.new('RGB', (large_box_width, large_box_height), "black")
    mask_0 =  Image.new('RGB', (small_box_width, small_box_height), "black")
    mask_1 = Image.new('RGB', (large_box_width, large_box_height), "white")

    max_x = large_box_width - small_box_width
    
    random_x = mask_random_start # np.random.randint(0, max_x)
    random_y = 0


    large_image.paste(small_image, (random_x, random_y))
    mask_1.paste(mask_0, (random_x, random_y))
    # large_image.save(os.path.join('mask_color.png'))
    # mask_1.save(os.path.join('mask.png'))

    return large_image, mask_1


def outpainting_generator(image, sqr):
    '''
    image: PIL Image file
    sqr: True or False, decide the shape of cropped images
    '''
    
    # sqr = random.choice([True, False])
    
    image = image.resize((512, 512))
    width, height = image.size  
    if sqr:
        size = height * random.uniform(0.15, 0.25) # size = height * random.uniform(0.7, 0.8)
        box_height, box_width = size, size
    else:
        box_height = height 
        box_width = width * random.uniform(0.35, 0.4)
    
    x = np.random.randint(0, int(width - box_width))
    if sqr:
        y = np.random.randint(0, int(height - box_height))
    else:
        y = 0

    left = x
    upper = y
    right = x + box_width
    lower = y + box_height

    box = (left, upper, right, lower)
    small_image = image.crop(box)

    large_box_width, large_box_height = 512, 512  

    if sqr:
        size = random.randint(300, 350)
        small_box_width, small_box_height = size, size
    else:
        # ratio = box_width / box_height
        # small_box_height = 512
        # small_box_width = int(ratio * box_height)
        small_box_height = 512
        small_box_width = 256
    small_image = small_image.resize((small_box_width, small_box_height))


    large_image = Image.new('RGB', (large_box_width, large_box_height), "black")
    mask_0 =  Image.new('RGB', (small_box_width, small_box_height), "black")
    mask_1 = Image.new('RGB', (large_box_width, large_box_height), "white")

    max_x = large_box_width - small_box_width
    max_y = large_box_height - small_box_height

    random_x = np.random.randint(0, max_x)
    if sqr:
        random_y = np.random.randint(0, max_y)
    else:
        random_y = 0


    large_image.paste(small_image, (random_x, random_y))
    mask_1.paste(mask_0, (random_x, random_y))
    large_image.save(os.path.join('mask_color.png'))
    mask_1.save(os.path.join('mask.png'))

    return large_image, mask_1