Skip to content

Commit 748dee3

Browse files
authored
Update Mosaic_generator.py
1 parent d861390 commit 748dee3

File tree

1 file changed

+60
-47
lines changed

1 file changed

+60
-47
lines changed

ImageProcessingScripts/Collage Mosaic Generator/Mosaic_generator.py

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,62 @@
55
import imghdr
66
import numpy as np
77

8-
#-----------------------------------------------------------------------------------
8+
#----------------------------------------------------------------------------------------------------------------------------------------------------
99

10-
def Average(image):
10+
def Average(image): # function to get the image width, heidht and dimension.
1111

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
1414

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
1616

17-
# ----------------------------------------------------------------------------------
17+
#----------------------------------------------------------------------------------------------------------------------------------------------------
1818

19-
def Split(image, size):
19+
20+
def Split(image, size): # function to get row*column list of image
2021

21-
Wid, Hei = image.size[0],image.size[1]
22+
Wid, Hei = image.size[0],image.size[1]
2223
row, column = size
2324
w, h =int(Wid/column), int(Hei/row)
2425

25-
img = []
26+
img = [] # list of images
2627

27-
for j in range(row):
28+
for j in range(row): # generate list dimension
2829
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
3031
return img
3132

32-
#-----------------------------------------------------------------------------------
33+
#----------------------------------------------------------------------------------------------------------------------------------------------------
34+
3335

34-
def Get(image_dir):
36+
def Get(image_dir): # function to get list of image
3537

3638
file = os.listdir(image_dir)
37-
images = []
39+
images = [] # image list
3840

39-
for i in file:
41+
for i in file: # path to the files
4042
filepath = os.path.abspath(os.path.join(image_dir, i))
41-
43+
4244
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
4547
images.append(im)
46-
im.load()
47-
f.close
48+
im.load() #loading the image from file
49+
f.close # closing the file
4850
except:
4951
print("Invalid Image" , filepath)
5052

5153
return images
5254

53-
#-----------------------------------------------------------------------------------
55+
#----------------------------------------------------------------------------------------------------------------------------------------------------
56+
5457

55-
def Filename(image_dir):
58+
def Filename(image_dir): # function to get list of image file names
5659

5760
file = os.listdir(image_dir)
58-
f_names = []
61+
f_names = [] # ilst of file names
5962

60-
for i in file:
63+
for i in file: # path to the file
6164
filepath = os.path.abspath(os.path.join(image_dir, i))
6265

6366
try:
@@ -69,11 +72,12 @@ def Filename(image_dir):
6972

7073
return f_names
7174

72-
#-----------------------------------------------------------------------------------
75+
#----------------------------------------------------------------------------------------------------------------------------------------------------
76+
7377

74-
def Match(i_avg, avgs):
78+
def Match(i_avg, avgs): # function to get the image index to place the image
7579

76-
avg = i_avg
80+
avg = i_avg # average input
7781

7882
ind = 0
7983
min_ind = 0
@@ -83,40 +87,42 @@ def Match(i_avg, avgs):
8387
dist = ((i[0] - avg[0])*(i[0] - avg[0])+(i[1] - avg[1])*(i[1] - avg[1]) +
8488
(i[2] - avg[2])*(i[2] - avg[2]))
8589

86-
if dist < min_dist:
90+
if dist < min_dist: #caluclate min distance
8791
min_dist = dist;
8892
min_ind = ind
8993
ind+=1
9094

9195
return min_ind
9296

93-
#-----------------------------------------------------------------------------------
97+
#----------------------------------------------------------------------------------------------------------------------------------------------------
9498

95-
def Create(images, dims):
99+
100+
def Create(images, dims): # function to create grid for easy fitting images
96101

97102
row, column = dims
98103

99104
assert row*column == len(images)
100105

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
103108

104-
grid_img = Image.new('RGB', (column*width, row*height))
109+
grid_img = Image.new('RGB', (column*width, row*height)) # formating output image
105110

106111

107-
for i in range(len(images)):
112+
for i in range(len(images)): # Fitting the images in the grid
108113
row = int(i/column)
109114
col = i - column*row
110115
grid_img.paste(images[i], (col*width, row*height))
111116

112117
return grid_img
113118

114-
#-----------------------------------------------------------------------------------
119+
#----------------------------------------------------------------------------------------------------------------------------------------------------
120+
115121

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
117123

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
120126

121127
c = 0
122128
b_size = int(len(t_img)/10)
@@ -128,21 +134,23 @@ def Mosaic(t_img, inp_img, grid_size, reuse=True):
128134
for img in t_img:
129135
avg = Average(img)
130136

131-
match_index = Match(avg, avgs)
137+
match_index = Match(avg, avgs) # to find the matching index
132138
out_img.append(inp_img[match_index])
133139

134140
if c > 0 and b_size > 10 and count % b_size is 0:
135141
c += 1
136-
if not reuse:
142+
if not reuse: #if flag set remove selected image
137143
inp_img.remove(match)
138144

139145
mosaic_image = Create(out_img, grid_size)
140146
return mosaic_image
141147

142-
#-----------------------------------------------------------------------------------
148+
#----------------------------------------------------------------------------------------------------------------------------------------------------
149+
143150

144151
def main():
145152

153+
# parser arguments
146154
parser = argparse.ArgumentParser(description='Creates a photomosaic from input images')
147155
parser.add_argument('--target-image', dest='target_image', required=True)
148156
parser.add_argument('--input-folder', dest='input_folder', required=True)
@@ -151,19 +159,19 @@ def main():
151159

152160
args = parser.parse_args()
153161

154-
target_image = Image.open(args.target_image)
162+
target_image = Image.open(args.target_image) # target the image
155163

156-
input_image = Get(args.input_folder)
164+
input_image = Get(args.input_folder) # input image
157165

158166
if input_image == []:
159167
print('No input images found in %s. Exiting.' % (args.input_folder, ))
160168
exit()
161169

162170
random.shuffle(input_image)
163171

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
165173

166-
output_filename = 'mosaic.png'
174+
output_filename = 'mosaic.png' # output image
167175
if args.outfile:
168176
output_filename = args.outfile
169177

@@ -179,17 +187,22 @@ def main():
179187

180188
for img in input_image:
181189
img.thumbnail(dims)
190+
182191

192+
# creating mosaic images
183193
mosaic_image = Mosaic(target_image, input_image, grid_size, reuse_images)
184194

185195
mosaic_image.save(output_filename, 'PNG')
196+
197+
# user understanding message
186198
print("saved output to %s" % (output_filename,))
187199
print('done.')
188200

189-
#-----------------------------------------------------------------------------------
201+
#----------------------------------------------------------------------------------------------------------------------------------------------------
202+
190203

191204
if __name__ == '__main__':
192-
main()
205+
main() # call the main program
193206

194207

195208

0 commit comments

Comments
 (0)