5
5
import imghdr
6
6
import numpy as np
7
7
8
- #-----------------------------------------------------------------------------------
8
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
9
9
10
- def Average (image ):
10
+ def Average (image ): # function to get the image width, heidht and dimension.
11
11
12
- im = np .array (image )
13
- width ,height ,dimension = im .shape
12
+ im = np .array (image ) # to get array of the image
13
+ width ,height ,dimension = im .shape #shape of the image
14
14
15
- return tuple (np .average (im .reshape (width * height , dimension ), axis = 0 ))
15
+ return tuple (np .average (im .reshape (width * height , dimension ), axis = 0 )) # get average value
16
16
17
- # ----------------------------------------------------------------------------------
17
+ #------------------------------------------------------------------ ----------------------------------------------------------------------------------
18
18
19
- def Split (image , size ):
19
+
20
+ def Split (image , size ): # function to get row*column list of image
20
21
21
- Wid , Hei = image .size [0 ],image .size [1 ]
22
+ Wid , Hei = image .size [0 ],image .size [1 ]
22
23
row , column = size
23
24
w , h = int (Wid / column ), int (Hei / row )
24
25
25
- img = []
26
+ img = [] # list of images
26
27
27
- for j in range (row ):
28
+ for j in range (row ): # generate list dimension
28
29
for i in range (column ):
29
- img .append (image .crop ((i * w , j * h , (i + 1 )* w , (j + 1 )* h )))
30
+ img .append (image .crop ((i * w , j * h , (i + 1 )* w , (j + 1 )* h ))) #append the croped image
30
31
return img
31
32
32
- #-----------------------------------------------------------------------------------
33
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
34
+
33
35
34
- def Get (image_dir ):
36
+ def Get (image_dir ): # function to get list of image
35
37
36
38
file = os .listdir (image_dir )
37
- images = []
39
+ images = [] # image list
38
40
39
- for i in file :
41
+ for i in file : # path to the files
40
42
filepath = os .path .abspath (os .path .join (image_dir , i ))
41
-
43
+
42
44
try :
43
- f = open (filepath , 'rb' )
44
- im = Image .open (f )
45
+ f = open (filepath , 'rb' ) # opening the file
46
+ im = Image .open (f ) # opening hte image
45
47
images .append (im )
46
- im .load ()
47
- f .close
48
+ im .load () #loading the image from file
49
+ f .close # closing the file
48
50
except :
49
51
print ("Invalid Image" , filepath )
50
52
51
53
return images
52
54
53
- #-----------------------------------------------------------------------------------
55
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
56
+
54
57
55
- def Filename (image_dir ):
58
+ def Filename (image_dir ): # function to get list of image file names
56
59
57
60
file = os .listdir (image_dir )
58
- f_names = []
61
+ f_names = [] # ilst of file names
59
62
60
- for i in file :
63
+ for i in file : # path to the file
61
64
filepath = os .path .abspath (os .path .join (image_dir , i ))
62
65
63
66
try :
@@ -69,11 +72,12 @@ def Filename(image_dir):
69
72
70
73
return f_names
71
74
72
- #-----------------------------------------------------------------------------------
75
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
76
+
73
77
74
- def Match (i_avg , avgs ):
78
+ def Match (i_avg , avgs ): # function to get the image index to place the image
75
79
76
- avg = i_avg
80
+ avg = i_avg # average input
77
81
78
82
ind = 0
79
83
min_ind = 0
@@ -83,40 +87,42 @@ def Match(i_avg, avgs):
83
87
dist = ((i [0 ] - avg [0 ])* (i [0 ] - avg [0 ])+ (i [1 ] - avg [1 ])* (i [1 ] - avg [1 ]) +
84
88
(i [2 ] - avg [2 ])* (i [2 ] - avg [2 ]))
85
89
86
- if dist < min_dist :
90
+ if dist < min_dist : #caluclate min distance
87
91
min_dist = dist ;
88
92
min_ind = ind
89
93
ind += 1
90
94
91
95
return min_ind
92
96
93
- #-----------------------------------------------------------------------------------
97
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
94
98
95
- def Create (images , dims ):
99
+
100
+ def Create (images , dims ): # function to create grid for easy fitting images
96
101
97
102
row , column = dims
98
103
99
104
assert row * column == len (images )
100
105
101
- width = max ([img .size [0 ] for img in images ])
102
- height = max ([img .size [1 ] for img in images ])
106
+ width = max ([img .size [0 ] for img in images ]) # maimum width
107
+ height = max ([img .size [1 ] for img in images ]) # maimum height
103
108
104
- grid_img = Image .new ('RGB' , (column * width , row * height ))
109
+ grid_img = Image .new ('RGB' , (column * width , row * height )) # formating output image
105
110
106
111
107
- for i in range (len (images )):
112
+ for i in range (len (images )): # Fitting the images in the grid
108
113
row = int (i / column )
109
114
col = i - column * row
110
115
grid_img .paste (images [i ], (col * width , row * height ))
111
116
112
117
return grid_img
113
118
114
- #-----------------------------------------------------------------------------------
119
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
120
+
115
121
116
- def Mosaic (t_img , inp_img , grid_size , reuse = True ):
122
+ def Mosaic (t_img , inp_img , grid_size , reuse = True ): #Functtion to create photomosaic
117
123
118
- t_img = Split (t_img , grid_size )
119
- out_img = []
124
+ t_img = Split (t_img , grid_size ) # Split function is used
125
+ out_img = [] #output images
120
126
121
127
c = 0
122
128
b_size = int (len (t_img )/ 10 )
@@ -128,21 +134,23 @@ def Mosaic(t_img, inp_img, grid_size, reuse=True):
128
134
for img in t_img :
129
135
avg = Average (img )
130
136
131
- match_index = Match (avg , avgs )
137
+ match_index = Match (avg , avgs ) # to find the matching index
132
138
out_img .append (inp_img [match_index ])
133
139
134
140
if c > 0 and b_size > 10 and count % b_size is 0 :
135
141
c += 1
136
- if not reuse :
142
+ if not reuse : #if flag set remove selected image
137
143
inp_img .remove (match )
138
144
139
145
mosaic_image = Create (out_img , grid_size )
140
146
return mosaic_image
141
147
142
- #-----------------------------------------------------------------------------------
148
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
149
+
143
150
144
151
def main ():
145
152
153
+ # parser arguments
146
154
parser = argparse .ArgumentParser (description = 'Creates a photomosaic from input images' )
147
155
parser .add_argument ('--target-image' , dest = 'target_image' , required = True )
148
156
parser .add_argument ('--input-folder' , dest = 'input_folder' , required = True )
@@ -151,19 +159,19 @@ def main():
151
159
152
160
args = parser .parse_args ()
153
161
154
- target_image = Image .open (args .target_image )
162
+ target_image = Image .open (args .target_image ) # target the image
155
163
156
- input_image = Get (args .input_folder )
164
+ input_image = Get (args .input_folder ) # input image
157
165
158
166
if input_image == []:
159
167
print ('No input images found in %s. Exiting.' % (args .input_folder , ))
160
168
exit ()
161
169
162
170
random .shuffle (input_image )
163
171
164
- grid_size = (int (args .grid_size [0 ]), int (args .grid_size [1 ]))
172
+ grid_size = (int (args .grid_size [0 ]), int (args .grid_size [1 ])) #Grid size
165
173
166
- output_filename = 'mosaic.png'
174
+ output_filename = 'mosaic.png' # output image
167
175
if args .outfile :
168
176
output_filename = args .outfile
169
177
@@ -179,17 +187,22 @@ def main():
179
187
180
188
for img in input_image :
181
189
img .thumbnail (dims )
190
+
182
191
192
+ # creating mosaic images
183
193
mosaic_image = Mosaic (target_image , input_image , grid_size , reuse_images )
184
194
185
195
mosaic_image .save (output_filename , 'PNG' )
196
+
197
+ # user understanding message
186
198
print ("saved output to %s" % (output_filename ,))
187
199
print ('done.' )
188
200
189
- #-----------------------------------------------------------------------------------
201
+ #----------------------------------------------------------------------------------------------------------------------------------------------------
202
+
190
203
191
204
if __name__ == '__main__' :
192
- main ()
205
+ main () # call the main program
193
206
194
207
195
208
0 commit comments