-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoMosaic.py
395 lines (264 loc) · 12.2 KB
/
PhotoMosaic.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""Module to make photo mosaics - By: DaviAMSilva"""
import argparse
import PIL.Image
from glob import glob
from tqdm import tqdm
from math import floor, ceil, sqrt
# These are optional, but if they're not present you can't use CIELAB as the color mode
try:
from colormath.color_objects import LabColor, sRGBColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
except ModuleNotFoundError:
pass
def create_mosaic(
main_image_path:str,
size:int,
resolution:int,
samples_path:str = ".\\",
name:str = "Mosaic.jpg",
recursive:bool = False,
color_mode:str = "SIMPLIFIED",
resize_mode:str = "BICUBIC",
formats:list = []
) -> bool:
"""
### Creates a images mosaic combining many sample images to \
create a bigger image that resembles the original main image
#### Arguments:
- main_image_path {str} -- Path to the main image, source to the mosaic image
- size {int} -- Size of each tile in the main image (different from tile resolution)
- resolution {int} -- Resolution (size) of each tile in the final image
#### Keyword Arguments:
- samples_path {str} -- The path to the samples images (default: {".\"})
- recursive {bool} -- Whether or not to recursively search all the files in the sample images folder (default: {False})
- color_mode {str} -- Specifies how to calculate the distance between two colors (default: {"SIMPLIFIED"})
- resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
- formats {list} -- Aditional file formats to be accepted (dot required) (default: [])
#### Returns:
{bool} -- Whether or not the function exited successfully
"""
# Loading main image
try:
main_image:PIL.Image.Image = PIL.Image.open(main_image_path)
except FileNotFoundError:
print(f"The main image could not be found. Please try again. (Input: {main_image_path})")
return False
main_image_info = ImageInfo(main_image_path, main_image, average_color(main_image, resize_mode))
# Valid extensions
valid_extensions = [".jpg", ".jpeg", ".png"]
valid_extensions.extend(formats)
valid_extensions = tuple(valid_extensions)
# The list with all image names
path_name_list = []
# Adding the images
for ext in valid_extensions:
if recursive:
path_name_list.extend(glob(f"{samples_path}/**/*{ext}", recursive=True))
else:
path_name_list.extend(glob(f"{samples_path}/*{ext}", recursive=False))
# Removing all non valid image
path_name_list = list(filter(
lambda x:
x.endswith(valid_extensions),
path_name_list))
# Stores the information of all the sample images
sample_image_info = []
# Creating progress bar
pbar = tqdm(path_name_list, "Loading sample images", unit=" images", leave=False)
# Looping through all the images
for path_name in pbar:
# Updating current image
pbar.set_postfix({"Current": path_name[-25:]}, True)
# Opening the files
with PIL.Image.open(path_name) as image:
image = image.convert("RGBA")
# Getting info
sq_image = square_crop(image, resolution, resize_mode)
av_color = average_color(image, resize_mode)
# Saving info
sample_image_info.append(ImageInfo(path_name,sq_image,av_color))
# Creating the mosaic
mosaic_image = create_mosaic_image(main_image, sample_image_info, size, resolution, color_mode, resize_mode)
# Checking if the mosaic is valid
if mosaic_image == None:
return False
# Creating progress bar
pbar = tqdm(desc="Saving the mosaic... (This might take a while)", bar_format="{desc}", leave=False)
# Saving the image
mosaic_image.save(name)
# Closing the progress bar
pbar.close()
# Success
return True
def create_mosaic_image(
main_image:PIL.Image.Image,
sample_images_info,
size:int,
resolution:int,
color_mode:str = "SIMPLIFIED",
resize_mode:str = "BICUBIC"
) -> PIL.Image.Image:
"""
### Function to actually create the mosaic image
#### Arguments:
- main_image {PIL.Image.Image} -- Main image object
- sample_images_info {list} -- Info of all the sample images
- size {int} -- Size of each tile in the main image (different from tile resolution)
- resolution {int} -- Resolution (size) of each tile in the final image
- color_mode {str} -- Specifies how to calculate the distance between two colors (default: {"SIMPLIFIED"})
- resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
#### Returns:
PIL.Image.Image -- Pillow image object
"""
# Initializing some variables
width, height = main_image.size
xoff, yoff = width % size / 2, height % size / 2
xnum, ynum = width // size, height // size
pb = tqdm(desc="Creating a blank image... (This might take a while)", bar_format="{desc}", leave=False)
# Creating a blank image
mosaic_image = PIL.Image.new("RGB", (resolution*xnum, resolution*ynum), 255)
pb.close()
pbar = tqdm(desc="Drawing all the tiles", total=xnum*ynum, unit=" tiles", leave=False, smoothing=0.1)
# Looping through all the tiles
for i, x in enumerate(range(ceil(xoff), floor(width - xoff), size)):
for j, y in enumerate(range(ceil(yoff), floor(height - yoff), size)):
# Getting the average color of the current tile
av_color = average_color(main_image.crop((x, y, x + size, y + size)), resize_mode)
# Initializing some variables
lowest_value = 1_000_000_000
best_image:PIL.Image.Image = PIL.Image.new("RGB", (resolution, resolution), 255)
# Going through all the info from the sample images
for sample_img in sample_images_info:
# Determining which color distance estimatorr to use
if color_mode == "CIELAB":
try:
# * CIELAB: More realistic, but slower *
r,g,b,*_ = av_color
av_color_lab = convert_color(sRGBColor(r, g, b, True), LabColor)
sample_lab = convert_color(sRGBColor(*[*sample_img.color][0:3], True), LabColor)
result = delta_e_cie2000(sample_lab, av_color_lab)
except NameError:
pbar.leave = True
pbar.close()
print(f"\nNameError detected. Please verify that you have "+
"'colormath' installed to use the color mode 'CIELAB'.")
return
else:
# Initializing some variables
r,g,b,_ = sample_img.color
ar,ag,ab,*_ = av_color
if color_mode == "RGB":
# * Cartesian RGB: Not realistic, but faster *
result = sqrt(pow(r-ar,2) + pow(g-ag,2) + pow(b-ab,2))
else:
# * Simplified formula: Intermidiare realism and speed. *
# * Source: https://www.compuphase.com/cmetric.htm *
rmean = (r + ar) // 2;
dr,dg,db = int(r - ar), int(g - ag), int(b - ab)
result = sqrt((((512+rmean)*dr*dr)>>8) + 4*dg*dg + (((767-rmean)*db*db)>>8))
# Storing the image with the most similar colors to the current tile
if result < lowest_value:
lowest_value = result
best_image = sample_img.image
# Drawing the best image found in the current tile
mosaic_image.paste(best_image, (
i * resolution,
j * resolution,
(i + 1) * resolution,
(j + 1) * resolution
))
# Updating progress bar
pbar.update(1)
# TODO # Fix jittering by formating numbers
pbar.set_postfix({"Current": (i + 1, j + 1), "Grid": (xnum, ynum)}, True)
# Closing the progress bar
pbar.close()
# Returning the image
return mosaic_image
class ImageInfo:
"""Stores the information of each image"""
def __init__(self, path:str, image:PIL.Image.Image, color:tuple):
self.image:PIL.Image.Image = image
self.color:tuple = color
self.path:str = path
def __repr__(self) -> str:
return f"Path: {self.path}\nImage: {self.image}\nColor: {self.color}"
def square_crop(img:PIL.Image.Image, resolution:int, resize_mode:str="BICUBIC") -> PIL.Image.Image:
"""
### Crops a image with the biggest square possible
#### Arguments:
- img {PIL.Image.Image} -- Pillow image object
- resolution {int} -- The height/width of the return square image
- resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
#### Returns:
{PIL.Image.Image} -- Pillow image object
"""
# Dimensions
w, h = img.size
# Selecting the right crop box
if w == h:
box = (0, 0, w, h)
elif w > h:
box = ((w-h) // 2, 0, (w + h) // 2, h)
else:
box = (0, (h-w) // 2, w, (h + w) // 2)
# Returning the cropped image
return img.resize((resolution, resolution), getattr(PIL.Image, resize_mode), box)
def average_color(img:PIL.Image.Image, resize_mode:str="BICUBIC") -> tuple:
"""
### Gets the average color of a image
#### Arguments:
- img {Image.Image} -- Pillow image object
- resize_mode {str} -- Specifies a resampling filter for resizing images (default: {"BICUBIC"})
#### Returns:
{tuple} -- 3-Tuple with the RGB color
"""
# Returning the color
return img.resize((1, 1), getattr(PIL.Image, resize_mode)).getpixel((0, 0))
# ---------- ========== Main Script ========== ---------- #
if __name__ == "__main__":
# Starting
print("Starting...\n")
# Getting the parser
parser = argparse.ArgumentParser("PhotoMosaic", description="Use to create beautiful mosaic with your images")
# Adding the arguments
parser.add_argument("main_path", metavar="Image Path", type=str,
help="The path to the image that will be turned into a mosaic."
)
parser.add_argument("size", metavar="Tile Size", type=int,
help="The size of the each tile in the main image (different from the tile resolution)."
)
parser.add_argument("resolution", metavar="Tile Resolution", type=int,
help="The resolution (size) of each tile on the final mosaic."
)
parser.add_argument("-p", "--samplespath", metavar="PATH", type=str, default=".\\",
help="The path to the sample images that will form the final mosaic."
)
parser.add_argument("-n", "--name", type=str, default="Mosaic.jpg",
help="The name (with extension) of the final mosaic."
)
parser.add_argument("-r", "--recursive", action="store_true", default=False,
help="Whether or not to recursively search images in the sample images folder"
)
parser.add_argument("-c", "--colormode", type=str, default="SIMPLIFIED",
choices=["RGB", "SIMPLIFIED", "CIELAB"],
help="Specifies one from the different ways to compare the distance between colors."
)
parser.add_argument("-s", "--resizemode", type=str, default="BICUBIC",
choices=[ "BICUBIC", "NEAREST", "BOX", "BILINEAR", "HAMMING", "LANCZOS"],
help="Specifies the resampling filter for resizing images."
)
parser.add_argument("-f", "--formats", metavar="FORMAT", type=str, nargs="+", default=[],
help="Aditional file formats to be accepted (dot required)."
)
# Getting the arguments
args = parser.parse_args()
# Executing the main function
success = create_mosaic(args.main_path, args.size, args.resolution, args.samplespath,
args.name, args.recursive, args.colormode, args.resizemode, args.formats)
# Ending
if success:
input("\nThe program exited successfully. Press ENTER to close.\n")
else:
input("\nThe program didn't exit successfully. Press ENTER to close.\n")