Update generate_dataset.py
Browse files- generate_dataset.py +140 -0
generate_dataset.py
CHANGED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import random, os
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def merge_images_horizontally(img1, img2):
|
8 |
+
|
9 |
+
img1_width, img1_height = img1.size
|
10 |
+
img2_width, img2_height = img2.size
|
11 |
+
|
12 |
+
merged_width = img1_width + img2_width
|
13 |
+
merged_height = max(img1_height, img2_height)
|
14 |
+
merged_image = Image.new('RGBA', (merged_width + 20, merged_height))
|
15 |
+
|
16 |
+
|
17 |
+
merged_image.paste(img1, (0, 0))
|
18 |
+
merged_image.paste(img2, (img1_width + 20, 0))
|
19 |
+
|
20 |
+
return merged_image
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
def outpainting_generator_rectangle(image, box_width_ratio=0.35, mask_random_start=125):
|
25 |
+
'''
|
26 |
+
image: PIL Image file
|
27 |
+
sqr: True or False, decide the shape of cropped images
|
28 |
+
'''
|
29 |
+
|
30 |
+
# sqr = random.choice([True, False])
|
31 |
+
|
32 |
+
image = image.resize((512, 512))
|
33 |
+
width, height = image.size
|
34 |
+
|
35 |
+
box_height = height
|
36 |
+
box_width = width * box_width_ratio #* random.uniform(0.35, 0.4)
|
37 |
+
|
38 |
+
x = np.random.randint(0, int(width - box_width))
|
39 |
+
y = 0
|
40 |
+
|
41 |
+
left = x
|
42 |
+
upper = y
|
43 |
+
right = x + box_width
|
44 |
+
lower = y + box_height
|
45 |
+
|
46 |
+
box = (left, upper, right, lower)
|
47 |
+
small_image = image.crop(box)
|
48 |
+
|
49 |
+
large_box_width, large_box_height = 512, 512
|
50 |
+
small_box_height = 512
|
51 |
+
small_box_width = 256
|
52 |
+
small_image = small_image.resize((small_box_width, small_box_height))
|
53 |
+
|
54 |
+
|
55 |
+
large_image = Image.new('RGB', (large_box_width, large_box_height), "black")
|
56 |
+
mask_0 = Image.new('RGB', (small_box_width, small_box_height), "black")
|
57 |
+
mask_1 = Image.new('RGB', (large_box_width, large_box_height), "white")
|
58 |
+
|
59 |
+
max_x = large_box_width - small_box_width
|
60 |
+
|
61 |
+
random_x = mask_random_start # np.random.randint(0, max_x)
|
62 |
+
random_y = 0
|
63 |
+
|
64 |
+
|
65 |
+
large_image.paste(small_image, (random_x, random_y))
|
66 |
+
mask_1.paste(mask_0, (random_x, random_y))
|
67 |
+
# large_image.save(os.path.join('mask_color.png'))
|
68 |
+
# mask_1.save(os.path.join('mask.png'))
|
69 |
+
|
70 |
+
return large_image, mask_1
|
71 |
+
|
72 |
+
|
73 |
+
def outpainting_generator(image, sqr):
|
74 |
+
'''
|
75 |
+
image: PIL Image file
|
76 |
+
sqr: True or False, decide the shape of cropped images
|
77 |
+
'''
|
78 |
+
|
79 |
+
# sqr = random.choice([True, False])
|
80 |
+
|
81 |
+
image = image.resize((512, 512))
|
82 |
+
width, height = image.size
|
83 |
+
if sqr:
|
84 |
+
size = height * random.uniform(0.15, 0.25) # size = height * random.uniform(0.7, 0.8)
|
85 |
+
box_height, box_width = size, size
|
86 |
+
else:
|
87 |
+
box_height = height
|
88 |
+
box_width = width * random.uniform(0.35, 0.4)
|
89 |
+
|
90 |
+
x = np.random.randint(0, int(width - box_width))
|
91 |
+
if sqr:
|
92 |
+
y = np.random.randint(0, int(height - box_height))
|
93 |
+
else:
|
94 |
+
y = 0
|
95 |
+
|
96 |
+
left = x
|
97 |
+
upper = y
|
98 |
+
right = x + box_width
|
99 |
+
lower = y + box_height
|
100 |
+
|
101 |
+
box = (left, upper, right, lower)
|
102 |
+
small_image = image.crop(box)
|
103 |
+
|
104 |
+
large_box_width, large_box_height = 512, 512
|
105 |
+
|
106 |
+
if sqr:
|
107 |
+
size = random.randint(300, 350)
|
108 |
+
small_box_width, small_box_height = size, size
|
109 |
+
else:
|
110 |
+
# ratio = box_width / box_height
|
111 |
+
# small_box_height = 512
|
112 |
+
# small_box_width = int(ratio * box_height)
|
113 |
+
small_box_height = 512
|
114 |
+
small_box_width = 256
|
115 |
+
small_image = small_image.resize((small_box_width, small_box_height))
|
116 |
+
|
117 |
+
|
118 |
+
large_image = Image.new('RGB', (large_box_width, large_box_height), "black")
|
119 |
+
mask_0 = Image.new('RGB', (small_box_width, small_box_height), "black")
|
120 |
+
mask_1 = Image.new('RGB', (large_box_width, large_box_height), "white")
|
121 |
+
|
122 |
+
max_x = large_box_width - small_box_width
|
123 |
+
max_y = large_box_height - small_box_height
|
124 |
+
|
125 |
+
random_x = np.random.randint(0, max_x)
|
126 |
+
if sqr:
|
127 |
+
random_y = np.random.randint(0, max_y)
|
128 |
+
else:
|
129 |
+
random_y = 0
|
130 |
+
|
131 |
+
|
132 |
+
large_image.paste(small_image, (random_x, random_y))
|
133 |
+
mask_1.paste(mask_0, (random_x, random_y))
|
134 |
+
large_image.save(os.path.join('mask_color.png'))
|
135 |
+
mask_1.save(os.path.join('mask.png'))
|
136 |
+
|
137 |
+
return large_image, mask_1
|
138 |
+
|
139 |
+
|
140 |
+
|