-
Notifications
You must be signed in to change notification settings - Fork 2
/
photomosaic_generator.py
370 lines (323 loc) · 14.4 KB
/
photomosaic_generator.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
'''
29/05/2020
Sarath Sajan
Terminologies
- main/target image denotes the image of which the photomosaic is to be made
- individual/filler images represents the smaller images that fit together to form the big picture
- every image used in this project is RGB color image, no grayscale image is used
'''
import time
import cv2
import numpy as np
import os, glob
import shutil
from PIL import Image
from datetime import datetime
import argparse #for implementing command-line interface
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action="store_true", help="specify --verbose flag in the command to get output with more details")
parser.add_argument("--visibility", action="store_true", help="specify --visibility flag in the command to retain the temporary files and folders created")
parser.add_argument("--res", type=int, help="Photomosaic output resolution (optimal value between 10 and 50) \nHigher value leads to low resolution photomosaic(small file size) and smaller value leads to high resolution photomosaic(large file size)")
parser.add_argument("--tile", type=int, help="resolution(in pixels) of the individual filler images that will used as the mosaic tiles (optimal value is between 50 and 100)")
args = parser.parse_args()
sdelay = 3
mdelay = 6
hdelay = 12
biar = 0 #blocks in a row
biac = 0 #blocks in a column
main_img_block_details = []
filler_img_details = []
matched_images = []
if type(args.res) == int:
divider = args.res
else: divider = 10
#area assigned to an individual img on main img
#higher value leads to low resolution output
#smaller value leads to high resolution output
if type(args.tile) == int:
size = args.tile, args.tile
else: size = 75, 75
#size of an individual square img or block size
#higher value leads to better quality individual imgs
#smaller value leads to low quality individual imgs
def initialise():
print ("Initialising...")
try:
if args.verbose: print ("Deleting previous 'main_image_blocks' folder")
shutil.rmtree("main_image_blocks")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting previous 'renamed_filler_images' folder")
shutil.rmtree("renamed_filler_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting previous 'cropped_filler_images' folder")
shutil.rmtree("cropped_filler_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting previous 'resized_filler_images' folder")
shutil.rmtree("resized_filler_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting previous 'matched_images' folder")
shutil.rmtree("matched_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
def split_main_image():
print ("\nSTEP 1 --> Splitting the Main image")
if args.verbose: time.sleep(sdelay)
os.mkdir("main_image_blocks") #create a new folder for storing the main image blocks
if (cv2.imread('./main_image/img.jpeg', 1)) == None:
img = cv2.imread('./main_image/img.jpg', 1) # load the color jpg image
else:
img = cv2.imread('./main_image/img.jpeg', 1) # load the color jpeg image
if args.verbose: print ("\nMain image shape")
if args.verbose: print (" height : ", img.shape[0])
if args.verbose: print (" width : ", img.shape[1])
if args.verbose: time.sleep(sdelay)
#after loading the main image, trim the extra rows/columns of main image for proper splitting so that
#the image can be perfectly divided with the DIVIDER value provided, without getting any partial splits
extra_row = img.shape[0] % divider
extra_column = img.shape[1] % divider
#trim the image for proper splitting
img = img[0:img.shape[0]-extra_row, 0:img.shape[1]-extra_column]
global biar
global biac
biar = int(img.shape[1]/divider) #biar : blocks in a row
biac = int(img.shape[0]/divider) #biac : blocks in a column
if args.verbose: print ("\nWhen the main image is splitted we get")
if args.verbose: print (" blocks in a row : ", biar)
if args.verbose: print (" blocks in a column : ", biac)
if args.verbose: print (" total blocks : ", biar*biac)
if args.verbose: print (" NOTE : removed {} extra rows and {} extra columns".format(extra_row, extra_column))
if args.verbose: time.sleep(mdelay)
#after trimming the image is split into (biar*biac) number of blocks
count = 0
xposition = 0
yposition = 0
for i in range(0, biar):
for j in range(0, biac):
temp_img = img[:][:]
temp_img = temp_img[yposition:yposition+divider, xposition:xposition+divider]
count += 1
if args.verbose: print ("STEP 1 --> ", count, '/', biar*biac, ' completed')
cv2.imwrite('./main_image_blocks/{}.jpg'.format(str(j)+'_'+str(i)), temp_img)
yposition += divider
yposition = 0
xposition += divider
if args.verbose: time.sleep(sdelay)
def rename_filler_images():
print ("\nSTEP 2 --> Renaming the filler images")
if args.verbose: time.sleep(sdelay)
os.mkdir("renamed_filler_images") #create a new folder for storing renamed filler images
# load the filler images to be renamed
total_img_count = 0
count = 0
for infile in glob.glob("./filler_images/*.*"):
total_img_count += 1
for infile in glob.glob("./filler_images/*.*"):
im = Image.open(infile)
count += 1
im.save("./renamed_filler_images/" + str(count) + ".jpg", "JPEG")
if args.verbose: print ("STEP 2 --> ", count, "/", total_img_count, "images renamed")
if args.verbose: time.sleep(sdelay)
def crop_filler_images():
print ("\nSTEP 3 --> Cropping the filler images into square shape")
if args.verbose: time.sleep(sdelay)
os.mkdir("cropped_filler_images") #create a new folder for storing the square filler images
#loading the renamed filler images to cropped into square shapes
count = 0
total_img_count = 0
for infile in glob.glob("./renamed_filler_images/*.jpg"):
total_img_count += 1
for infile in glob.glob("./renamed_filler_images/*.jpg"):
count += 1
img = cv2.imread('./renamed_filler_images/' + str(count) + '.jpg', 1)
if img.shape[0] < img.shape[1]: #landscape orientation
remove_value = img.shape[1] - img.shape[0]
img = img[:, :img.shape[1]-remove_value]
elif img.shape[0] > img.shape[1]: #potrait orientation
remove_value = img.shape[0] - img.shape[1]
img = img[:img.shape[0]-remove_value, :]
cv2.imwrite("./cropped_filler_images/" + str(count) + ".jpg", img)
if args.verbose: print ("STEP 3 --> ", count, '/', total_img_count, 'images cropped and made into squares')
if args.verbose: time.sleep(sdelay)
def resize_filler_images():
print ("\nSTEP 4 --> Resizing the filler images")
if args.verbose: time.sleep(sdelay)
os.mkdir("resized_filler_images") #create a new folder for storing the resized filler images
#load cropped filler images to be resized
total_img_count = 0
count = 0
for infile in glob.glob("./cropped_filler_images/*.jpg"):
total_img_count += 1
for infile in glob.glob("./cropped_filler_images/*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.thumbnail(size)
im.save("./resized_filler_images/" + str(count) + '.jpg', "JPEG")
count += 1
if args.verbose: print ("STEP 4 --> ", count, '/', total_img_count, 'filler images resized')
if args.verbose: time.sleep(sdelay)
def avg_rgb_filler_images():
print ("\nSTEP 5 --> Finding the average RGB value of each filler image")
if args.verbose: time.sleep(sdelay)
total_img_count = 0
count = 0
for infile in glob.glob("./resized_filler_images/*.jpg"):
total_img_count += 1
for infile in glob.glob("./resized_filler_images/*.jpg"):
filler_img = cv2.imread("resized_filler_images/" + str(count) + ".jpg", 1)
sum_red = sum_green = sum_blue = 0
pixel_count = 0
for i in range(filler_img.shape[0]):
for j in range(filler_img.shape[1]):
temp_red, temp_green, temp_blue = filler_img[i][j][2], filler_img[i][j][1], filler_img[i][j][0] #opencv loads images in BGR by default
sum_red += temp_red
sum_green += temp_green
sum_blue += temp_blue
pixel_count += 1
avg_red = sum_red/pixel_count
avg_green = sum_green/pixel_count
avg_blue = sum_blue/pixel_count
filler_img_details.append({
'file_name': str(count),
'avg_red': avg_red,
'avg_green': avg_green,
'avg_blue': avg_blue
})
count += 1
if args.verbose: print ("STEP 5 --> ", count, '/', total_img_count, "filler images' average RGB value calculated")
if args.verbose: time.sleep(sdelay)
def avg_rgb_main_image_blocks():
print ("\nSTEP 6 --> Finding the average RGB value of each main image block")
if args.verbose: time.sleep(sdelay)
total_img_count = 0
count = 0
for infile in glob.glob('./main_image_blocks/*.jpg'):
total_img_count += 1
for i in range(0, biar):
for j in range(0, biac):
main_img_block = cv2.imread('./main_image_blocks/{}.jpg'.format(str(j)+'_'+str(i)), 1)
sum_red = 0
sum_green = 0
sum_blue = 0
pixel_count = 0
for p in range(main_img_block.shape[0]):
for q in range(main_img_block.shape[1]):
temp_red, temp_green, temp_blue = main_img_block[p][q][2], main_img_block[p][q][1], main_img_block[p][q][0] #opencv loads images in BGR by default
sum_red += temp_red
sum_green += temp_green
sum_blue += temp_blue
pixel_count += 1
avg_red = sum_red/pixel_count
avg_green = sum_green/pixel_count
avg_blue = sum_blue/pixel_count
main_img_block_details.append({
'file_name': str(j)+'_'+str(i),
'avg_red': avg_red,
'avg_green': avg_green,
'avg_blue': avg_blue
})
count += 1
if args.verbose: print ("STEP 6 --> ", count, '/', total_img_count, "main image blocks' average RGB value calculated")
if args.verbose: time.sleep(sdelay)
def image_match():
print ('\nSTEP 7 --> Finding the matching filler images for the main image blocks based on the average RGB values')
os.mkdir('matched_images')
if args.verbose: time.sleep(sdelay)
total_img_count = 0
count = 0
for infile in glob.glob('./main_image_blocks/*.jpg'):
total_img_count += 1
for i in main_img_block_details:
count += 1
distance = 1000
flag = 0
for j in filler_img_details:
distance_candidate = (((i['avg_red']-j['avg_red'])**2)+((i['avg_green']-j['avg_green'])**2)+((i['avg_blue']-j['avg_blue'])**2)) ** 0.5
if distance_candidate < distance:
distance = distance_candidate
if flag == 1:
matched_images.pop()
matched_images.append({
'main_img_block_name': i['file_name'],
'matched_filler_img_name': j['file_name']
})
flag = 1
if args.verbose: print ("STEP 7 --> ", count, '/', total_img_count, "blocks of the main image matched")
for data in matched_images:
img = cv2.imread("./resized_filler_images/{}.jpg".format(data['matched_filler_img_name']), 1)
cv2.imwrite("./matched_images/{}.jpg".format(data['main_img_block_name']), img)
if args.verbose: time.sleep(sdelay)
def image_stitch():
print ('\nSTEP 8 --> Stitching the matched filler images to form the final photomosaic')
if args.verbose: time.sleep(sdelay)
row = []
count = 0
total_img_count = 0
temp_img = []
for i in glob.glob("./matched_images/*.jpg"):
total_img_count += 1
for i in range(0, biac):
for j in range(0, biar):
img = cv2.imread("./matched_images/{}.jpg".format(str(i)+'_'+str(j)), 1)
row.append(img)
count += 1
if args.verbose: print ("STEP 8 --> ", count, '/', total_img_count, "images stitched")
temp_img.append(np.hstack(row))
row = []
photomosaic = np.vstack(temp_img)
cv2.imwrite ("mosaic_{}.jpg".format(datetime.today().strftime("%Y%m%d_%H%M%S")), photomosaic)
if args.verbose: time.sleep(sdelay)
def remove_temp_files():
try:
if args.verbose: print ("Deleting temporary 'main_image_blocks' folder")
shutil.rmtree("main_image_blocks")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting temporary 'renamed_filler_images' folder")
shutil.rmtree("renamed_filler_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting temporary 'cropped_filler_images' folder")
shutil.rmtree("cropped_filler_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting temporary 'resized_filler_images' folder")
shutil.rmtree("resized_filler_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
try:
if args.verbose: print ("Deleting temporary 'matched_images' folder")
shutil.rmtree("matched_images")
except Exception as e:
if args.verbose: print (e)
if args.verbose: time.sleep(sdelay)
initialise()
split_main_image()
rename_filler_images()
crop_filler_images()
resize_filler_images()
avg_rgb_filler_images()
avg_rgb_main_image_blocks()
image_match()
image_stitch()
if args.visibility == False: remove_temp_files()
print ("\nDONE")