/
mask4.py
176 lines (132 loc) · 4.37 KB
/
mask4.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python3.4
import numpy as np
import sys
from PIL import Image, ImageOps, ImageDraw
import os, glob
import logging
_N_SIDES = 4
class NumSidesError(Exception):
def __init__(self, N):
self.N = N
def __str__(self):
return type(self).__name__ + ': required 4 sides, given ' + repr(self.N)
class PathNotExistsError(Exception):
def __init__(self, path):
self.path = path
def __str__(self):
return type(self).__name__ + ': required camera path not exists ' + self.path
class PathUnavailableError(Exception):
def __init__(self, path):
self.path = path
def __str__(self):
return type(self).__name__ + ': output directory not exists or permission denied: ' + self.path
class MissingImageError(Exception):
def __init__(self, path):
self.path = path
def __str__(self):
return type(self).__name__ + ': required image path not exists ' + self.path
class InconsistentSizeError(Exception):
def __init__(self):
pass
def __str__(self):
return type(self).__name__ + ': image sizes must be equal'
def get_ab(size):
w, h = size
a = h//4
if 8*a < w:
b = (w-8*a)//2
else: # at least give 2a at the top
a = w//8
b = a
return a, b
def prepare_side_img(img, a, b):
(w, h) = img.size
p1 = [(0, h), (3*a, h), (0, h-3*a)] # first triangle
p2 = [(w, h), (w-3*a, h), (w, h-3*a)] # second one
mask = Image.new('L', (w, h), 255) # generate mask
ImageDraw.Draw(mask).polygon(p1, outline=1, fill=0)
ImageDraw.Draw(mask).polygon(p2, outline=1, fill=0)
output = ImageOps.fit(img, mask.size) # crop image
output.putalpha(mask) # put alpha to cropped part
return ImageOps.flip(output)
def merge_sides(sides):
w, h = sides[0].size # side size
a, b = get_ab((w, h))
nh = nw = 2*h + 2*b # nh,nw=2h+2b
logging.debug("Resolution (WxH) = (%dx%d)", nw, nh)
a3 = 3*a
offsets = [(h-a3, nh-h), (0, h-a3), (h-a3, 0), (nh-h, h-a3)]
res = Image.new('RGBA', (nw, nh), (0, 0, 0, 255))
logging.debug('Result size: (%d, %d)', res.size[0], res.size[1])
for i in range(_N_SIDES): # paste all sides
res.paste(sides[i], offsets[i], sides[i])
return res
def generate_holo_img(paths):
if len(paths) != _N_SIDES: # check there are required images
raise NumSidesError(len(paths))
logging.debug('Img paths: %s', repr(paths))
imgs = []
for p in paths: # if they dont exists this will flip
im = Image.open(p)
imgs.append(im)
size = imgs[0].size # since all must to be equal sized
for i in range(1, _N_SIDES): # check all are equal sized or flip
if size != imgs[i].size:
raise InconsistentSizeError()
a, b = get_ab(size) # get a b values
cropped = []
rots = [0, Image.ROTATE_270, Image.ROTATE_180, Image.ROTATE_90]
for i in range(_N_SIDES):
tmp = prepare_side_img(imgs[i], a, b)
if i>0:
tmp = tmp.transpose(rots[i]) # Image.ROTATE_90 = 2, 180 = 3 ...
cropped.append(tmp)
return merge_sides(cropped)
def generate_holo(root_dir):
root_dir = os.path.abspath(root_dir)
logging.info('Root dir: %s', root_dir)
frm_cnt = 0
# camera paths are predetermined as c1, c2, c3, c4 and all must exists
for ci in range(1, _N_SIDES+1): # check all camera image paths exists
cpath = os.path.join(root_dir, 'c'+str(ci))
if not os.path.exists(cpath):
raise PathNotExistsError(cpath)
# generate output folder
out_dir = os.path.join(root_dir, 'holo')
if not os.path.exists(out_dir):
os.mkdir(out_dir)
elif not os.path.isdir(out_dir) or not os.access(out_dir, os.W_OK):
raise PathUnavailableError(out_dir)
# get sorted image list by traverse because format might change for blender
cpath = os.path.join(root_dir, 'c1')
os.chdir(cpath)
img_list = sorted(glob.glob('*.png'))
# scan all images in all camera folders, merge them, and save them to outdir
for iname in img_list:
img_paths = []
for i in range(1, _N_SIDES+1):
ipath = root_dir + '/c' + str(i) + '/' + iname
if not os.path.exists(ipath):
raise MissingImageError(ipath)
img_paths.append(ipath)
hol_img = generate_holo_img(img_paths)
out_path = os.path.join(out_dir, iname)
hol_img.save(out_path)
logging.info('Saved holo, path: %s %s', iname, out_path)
try:
# setup logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# generate holo frames
generate_holo(sys.argv[1])
except Exception as e:
print(e)
"""
If cameras are in clockwise from the top
Image order for parameters (paths of side images):
\ 2 / .
\ / .
1 * 3 .
/ \ .
/ 0 \ .
But this will be auto generated by blender plugin
"""