-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_bg_overlay.py
337 lines (314 loc) · 16.5 KB
/
card_bg_overlay.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import sys, os, glob
import cv2
import math
import numpy as npy
from numpy.random import randint
def get_pic_list(pic_dir: str):
# To get a list of pictures stored at pic_dir
pic_list = []
pic_list_temp = glob.glob(pic_dir + "*.[jpg][jpeg][png]")
for p in pic_list_temp:
pic_list.append(os.path.basename(p))
return pic_list
def gen_pic_name(pic_name: str, pic_suffix: str = ".jpg"):
# To generate picture filename with suffix
return pic_name + pic_suffix
def gen_rand_pic_single(origin_pic_list: list):
# To generate the name of a random picture
rand_id = randint(0, len(origin_pic_list))
return origin_pic_list[rand_id]
def gen_rand_pic_list(origin_pic_list: list, pic_num: int):
# To generate the name of a list of random pictures
rand_pic_list = []
for i in range(0, pic_num):
rand_id = randint(0, len(origin_pic_list))
rand_pic_list.append(origin_pic_list[rand_id])
return rand_pic_list
def get_pic(pic_dir: str, pic_file: str):
# To read and attain the picture suitable for openCV
# Note that pic_file must have a suffix like '.jpg', '.png', etc.
if os.path.isfile(pic_dir + pic_file) == True:
return cv2.imread(pic_dir + pic_file,
cv2.IMREAD_UNCHANGED)
else: return None
def show_cv_pic(pic: npy.ndarray, pic_name: str):
# To show a picture in window
cv2.imshow(pic_name, pic); cv2.waitKey(0)
cv2.destroyAllWindows()
def save_cv_pic(pic: npy.ndarray, pic_dir: str,
pic_name: str, suffix: str = ".jpg"):
# To save a picture at pic_dir with the name pic_name
cv2.imwrite(pic_dir + pic_name + suffix, pic,
[cv2.IMWRITE_JPEG_QUALITY, 100])
def add_alpha(pic: npy.ndarray):
# To add an alpha channel for the picture
b_channel, g_channel, r_channel = cv2.split(pic)
alpha_channel = npy.ones(b_channel.shape,
dtype = b_channel.dtype) * 255
pic_with_alpha = cv2.merge((b_channel, g_channel,
r_channel, alpha_channel))
return pic_with_alpha
def rotate_pic(pic: npy.ndarray, deg: float):
# To rotate a picture
if pic.shape[2] == 3: pic = add_alpha(pic)
(pic_h, pic_w) = pic.shape[:2]
pic_center = (pic_w // 2, pic_h // 2)
M = cv2.getRotationMatrix2D(pic_center, deg, 1)
cos_deg = npy.abs(M[0, 0]); sin_deg = npy.abs(M[0, 1])
new_h = int(pic_w * sin_deg + pic_h * cos_deg)
new_w = int(pic_h * sin_deg + pic_w * cos_deg)
M[0, 2] += (new_w - pic_w) / 2
M[1, 2] += (new_h - pic_h) / 2
pic_rotated = cv2.warpAffine(pic, M, (new_w, new_h),
borderMode = cv2.BORDER_CONSTANT,
borderValue = (255, 255, 255, 0))
return pic_rotated
def get_coord_rotated(coord: npy.ndarray,
center: npy.ndarray, deg: float):
# To attain the coordinate of four corners of a rotated rectangle
# Note that the rotating direction of get_coord_rotated
# It is oppsite to rotate_pic (or cv2.getRotationMatrix2D)
cos_d = npy.cos(deg * npy.pi / 180)
sin_d = npy.sin(deg * npy.pi / 180)
coord_rotated = npy.zeros(coord.shape, dtype = "float")
coord_rotated[:, 0] = center[0] + (cos_d * (coord[:, 0] - center[0])
- sin_d * (coord[:, 1] - center[1]))
coord_rotated[:, 1] = center[1] + (sin_d * (coord[:, 0] - center[0])
+ cos_d * (coord[:, 1] - center[1]))
return coord_rotated.astype("int")
def get_coord_translated(coord: npy.ndarray,
trans_vect: npy.ndarray):
# To attain the coordinate of four corners of a translated rectangle
coord_translated = npy.zeros(coord.shape, dtype = "int")
coord_translated[:, 0] = coord[:, 0] + trans_vect[0]
coord_translated[:, 1] = coord[:, 1] + trans_vect[1]
return coord_translated
def overlay_pic(pic: npy.ndarray, bg: npy.ndarray,
ol_x: int, ol_y: int, with_alpha: bool):
# To overlay a picture on a specific background
# Can choose whether to add an alpha channel or not
if (pic.shape[2] == 3): pic = add_alpha(pic)
if (bg.shape[2] == 3): bg = add_alpha(bg)
(pic_h, pic_w) = pic.shape[:2]
(bg_h, bg_w) = bg.shape[:2]
ol_mx = ol_x + pic_w - 1; ol_my = ol_y + pic_h - 1
if ((ol_x < 0) or (ol_mx >= bg_w)):
return None
elif ((ol_y < 0) or (ol_my >= bg_h)):
return None
else:
alpha_pic = pic[0: pic_h, 0: pic_w, 3] / 255.0
alpha_bg = 1 - alpha_pic
for ch in range(0, 4):
bg[ol_y: ol_my + 1, ol_x: ol_mx + 1, ch] = (
pic[0: pic_h, 0: pic_w, ch] * alpha_pic
+ bg[ol_y: ol_my + 1, ol_x: ol_mx + 1, ch] * alpha_bg)
if (with_alpha == True): return bg
else: return bg[:, :, 0: 3]
def overlay_one_card_chaos(card_pic_dir: str = ".\\pics\\",
card_pic_name: str = "90809975.jpg",
bg_pic_dir: str = ".\\background\\",
bg_pic_name: str = "ref_background_black.jpg",
overlaid_pic_dir: str = ".\\overlaid_pics\\",
overlaid_pic_name: str = "overlaid_pic_example"):
# To overlay a card on a specific background
# And then save the overlaid picture
card = get_pic(card_pic_dir, card_pic_name)
# show_cv_pic(card, "input")
(card_h, card_w) = card.shape[:2]
card_minlen = min(card_h, card_w)
card_maxlen = max(card_h, card_w)
bg = get_pic(bg_pic_dir, bg_pic_name)
(bg_h, bg_w) = bg.shape[:2]
bg_minlen = min(bg_h, bg_w)
bg_maxlen = min(bg_h, bg_w)
if (bg_minlen < card_minlen):
print("Failed to overlay card!"); return
elif ((bg_minlen == card_minlen)
and (bg_maxlen < card_maxlen)):
print("Failed to overlay card!"); return
else:
card_rotated = rotate_pic(card, randint(0, 360))
# show_cv_pic(card_rotated, "rotated")
overlaid_pic = None
while (overlaid_pic is None):
overlaid_pic = overlay_pic(card_rotated, bg,
randint(0, bg_w), randint(0, bg_h), True)
# show_cv_pic(overlaid_pic, "overlaid")
save_cv_pic(overlaid_pic, overlaid_pic_dir, overlaid_pic_name, ".jpg")
print("Successfully overlay card!")
def overlay_multiple_cards_order(card_pic_dir: str = ".\\pics\\",
card_pic_name: list = ["90809975.jpg"],
bg_pic_dir: str = ".\\background\\",
bg_pic_name: str = "ref_background_black.jpg",
overlaid_pic_dir: str = ".\\overlaid_pics\\",
overlaid_pic_name: str = "overlaid_pic_example",
ins_h_init: int = 0, ins_w_init: int = 0,
blank_h: int = 20, blank_w: int = 20,
orientation: float = 0):
# To overlay a list of cards with a specific direction on background
# And then save the overlaid picture
# Therefore you can choose insertion position pic[ins_h_init][ins_w_init]
# You can also choose blank between external rectangles of rotated cards
# Note that the ndarray use the expression pic[h][w] to describe a pixel
# In Cartesian coordination it stands for point (w, h)
bg = get_pic(bg_pic_dir, bg_pic_name)
(bg_h, bg_w) = bg.shape[:2]
overlaid_pic = bg
(ins_h, ins_w) = (ins_h_init + blank_h, ins_w_init + blank_w)
(border_h, border_w) = (ins_h_init + blank_h, ins_w_init + blank_w)
for cname in card_pic_name:
card = get_pic(card_pic_dir, cname)
card_rotated = rotate_pic(card, orientation)
(card_rotated_h, card_rotated_w) = card_rotated.shape[:2]
if (border_w + card_rotated_w + blank_w >= bg_w):
if (border_h + card_rotated_h + blank_h >= bg_h):
print("Failed to overlay card!"); break
else:
ins_h = border_h; ins_w = ins_w_init + blank_w
overlaid_pic = overlay_pic(card_rotated, overlaid_pic,
ins_w, ins_h, True)
ins_w += (blank_w * 2 + card_rotated_w)
border_h += (blank_h * 2 + card_rotated_h)
border_w = ins_w_init + blank_w * 2 + card_rotated_w
# show_cv_pic(overlaid_pic, "overlaid")
print("Successfully overlay card!")
else:
overlaid_pic = overlay_pic(card_rotated, overlaid_pic,
ins_w, ins_h, True)
ins_w += (blank_w * 2 + card_rotated_w)
border_w += (blank_w * 2 + card_rotated_w)
border_h = max(ins_h + blank_h * 2 + card_rotated_h, border_h)
# show_cv_pic(overlaid_pic, "overlaid")
print("Successfully overlay card!")
save_cv_pic(overlaid_pic, overlaid_pic_dir, overlaid_pic_name, ".jpg")
def overlay_multiple_cards_chaos(card_pic_dir: str = ".\\pics\\",
card_pic_name: list = ["90809975.jpg"],
bg_pic_dir: str = ".\\background\\",
bg_pic_name: str = "ref_background_black.jpg",
overlaid_pic_dir: str = ".\\overlaid_pics\\",
overlaid_pic_name: str = "overlaid_pic_example",
max_trial: int = 100):
# To overlay a list of cards with random directions on background
# And then save the overlaid picture
# You can set max_trial for trial times of every card
# This function ban overlapped cards
# If you want overlapped cards, use overlay_multiple_cards_lunacy instead
bg = add_alpha(get_pic(bg_pic_dir, bg_pic_name))
(bg_h, bg_w) = bg.shape[:2]
bg_minlen = min(bg_h, bg_w)
bg_maxlen = min(bg_h, bg_w)
used_area = npy.zeros(bg.shape, dtype = "uint8")
for cpn in card_pic_name:
card = get_pic(card_pic_dir, cpn)
# show_cv_pic(card, "input")
(card_h, card_w) = card.shape[:2]
card_minlen = min(card_h, card_w)
card_maxlen = max(card_h, card_w)
if (bg_minlen < card_minlen):
print("Failed to overlay card!"); return
elif ((bg_minlen == card_minlen) and
(bg_maxlen < card_maxlen)):
print("Failed to overlay card!"); return
else:
cnt = 0
while (cnt <= max_trial):
deg = randint(0, 360) # deg = 45
ins_w = randint(0, bg_w); ins_h = randint(0, bg_h)
origin_coord = npy.asarray([[0, 0], [card_w - 1, 0],
[card_w -1, card_h - 1], [0, card_h - 1]])
rotated_coord = get_coord_rotated(origin_coord,
npy.asarray([card_w / 2, card_h / 2]), -deg)
delta_w = -npy.min(rotated_coord[:, 0])
delta_h = -npy.min(rotated_coord[:, 1])
translated_coord = get_coord_translated(rotated_coord,
npy.asarray([ins_w + delta_w, ins_h + delta_h]))
# translated_coord = npy.asarray([[283, 0], [693, 410], [410, 693], [0, 283]])
if ((npy.max(translated_coord[:, 0]) >= bg_w) or
(npy.max(translated_coord[:, 1]) >= bg_h)):
cnt += 1; continue
mask = npy.zeros(bg.shape[: 2], dtype = "uint8")
mask = cv2.fillPoly(mask, [translated_coord], (255, 255, 255, 255))
# show_cv_pic(mask, "mask")
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(used_area[:,:,3],
mask = mask)
if (max_val > 0):
cnt += 1; continue
else:
# masked_pic = cv2.bitwise_and(used_area, used_area,
# mask = mask)
# show_cv_pic(masked_pic, "masked_pic")
card_rotated = rotate_pic(card, deg)
used_area = overlay_pic(card_rotated, used_area,
ins_w, ins_h, True)
print("Successfully overlay card!")
# show_cv_pic(used_area, "overlaid")
break
if (cnt > max_trial):
print("Reach max trial counts, failed to overlay card!")
continue
overlaid_pic = overlay_pic(used_area, bg, 0, 0, True)
save_cv_pic(overlaid_pic, overlaid_pic_dir, overlaid_pic_name, ".jpg")
def overlay_multiple_cards_lunacy(card_pic_dir: str = ".\\pics\\",
card_pic_name: list = ["90809975.jpg"],
bg_pic_dir: str = ".\\background\\",
bg_pic_name: str = "ref_background_black.jpg",
overlaid_pic_dir: str = ".\\overlaid_pics\\",
overlaid_pic_name: str = "overlaid_pic_example"):
# To overlay a list of cards with random directions on background
# And then save the overlaid picture
# This function does allow overlapped cards
bg = get_pic(bg_pic_dir, bg_pic_name)
(bg_h, bg_w) = bg.shape[:2]
bg_minlen = min(bg_h, bg_w)
bg_maxlen = min(bg_h, bg_w)
for cpn in card_pic_name:
card = get_pic(card_pic_dir, cpn)
# show_cv_pic(card, "input")
(card_h, card_w) = card.shape[:2]
card_minlen = min(card_h, card_w)
card_maxlen = max(card_h, card_w)
if (bg_minlen < card_minlen):
print("Failed to overlay card!"); return
elif ((bg_minlen == card_minlen)
and (bg_maxlen < card_maxlen)):
print("Failed to overlay card!"); return
else:
card_rotated = rotate_pic(card, randint(0, 360))
# show_cv_pic(card_rotated, "rotated")
overlaid_pic = None
while (overlaid_pic is None):
overlaid_pic = overlay_pic(card_rotated, bg,
randint(0, bg_w), randint(0, bg_h), True)
# show_cv_pic(overlaid_pic, "overlaid")
bg = overlaid_pic
print("Successfully overlay card!")
save_cv_pic(bg, overlaid_pic_dir, overlaid_pic_name, ".jpg")
def main():
os.chdir(sys.path[0])
card_pic_list = get_pic_list(".\\pics\\")
bg_pic_list = get_pic_list(".\\background\\")
overlay_multiple_cards_order(".\\pics\\", gen_rand_pic_list(card_pic_list, 10),
".\\background\\", gen_rand_pic_single(bg_pic_list),
".\\overlaid_pics\\", "overlaid_pic_example", 0, 0, 20, 20, -90)
if __name__ == "__main__":
main()
"""
Example:
Overlay one card on one background randomly:
overlay_one_card_chaos(".\\pics\\", gen_rand_pic_single(card_pic_list),
".\\background\\", gen_rand_pic_single(bg_pic_list),
".\\overlaid_pics\\", "overlaid_pic_example")
Overlay multiple cards on one background in order:
overlay_multiple_cards_order(".\\pics\\", gen_rand_pic_list(card_pic_list, 10),
".\\background\\", gen_rand_pic_single(bg_pic_list),
".\\overlaid_pics\\", "overlaid_pic_example", 0, 0, 20, 20, 90)
Overlay multiple cards on one background randomly, overlap cards banned:
overlay_multiple_cards_chaos(".\\pics\\", gen_rand_pic_list(card_pic_list, 10),
".\\background\\", gen_rand_pic_single(bg_pic_list),
".\\overlaid_pics\\", "overlaid_pic_example", 100)
Overlay multiple cards on one background randomly, overlap cards permitted:
overlay_multiple_cards_lunacy(".\\pics\\", gen_rand_pic_list(card_pic_list, 10),
".\\background\\", gen_rand_pic_single(bg_pic_list),
".\\overlaid_pics\\", "overlaid_pic_example")
"""