-
Notifications
You must be signed in to change notification settings - Fork 13
/
make_img.py
1365 lines (1153 loc) · 56.2 KB
/
make_img.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import time
import math
import random
import argparse
import itertools
import traceback
import multiprocessing as mp
from fractions import Fraction
from typing import Any, Callable, List, Tuple
from collections import defaultdict
from io_utils import stdout_redirector, JVOutWrapper
import cv2
import imagesize
import numpy as np
cp = np
from tqdm import tqdm
from lapjv import lapjv
Grid = Tuple[int, int] # grid size = (width, height)
BackgroundRGB = Tuple[int, int, int]
if mp.current_process().name != "MainProcess":
sys.stdout = open(os.devnull, "w")
sys.stderr = sys.stdout
pbar_ncols = None
LIMIT = 2**32
class _PARAMETER:
def __init__(self, type: Any, help: str, default=None, nargs=None, choices: List[Any]=None) -> None:
self.type = type
self.default = default
self.help = help
self.nargs = nargs
self.choices = choices
# We gather parameters here so they can be reused else where
class PARAMS:
path = _PARAMETER(help="Path to the tiles", type=str)
recursive = _PARAMETER(type=bool, default=False, help="Whether to read the sub-folders for the specified path")
num_process = _PARAMETER(type=int, default=mp.cpu_count() // 2, help="Number of processes to use for parallelizable operations")
out = _PARAMETER(default="result.png", type=str, help="The filename of the output collage/photomosaic")
size = _PARAMETER(type=int, nargs="+", default=(50,),
help="Width and height of each tile in pixels in the resulting collage/photomosaic. "
"If two numbers are specified, they are treated as width and height. "
"If one number is specified, the number is treated as the width "
"and the height is inferred from the aspect ratios of the images provided. ")
quiet = _PARAMETER(type=bool, default=False, help="Do not print progress message to console")
auto_rotate = _PARAMETER(type=int, default=0, choices=[-1, 0, 1],
help="Options to auto rotate tiles to best match the specified tile size. 0: do not auto rotate. "
"1: attempt to rotate counterclockwise by 90 degrees. -1: attempt to rotate clockwise by 90 degrees")
resize_opt = _PARAMETER(type=str, default="center", choices=["center", "stretch", "fit"],
help="How to resize each tile so they have the desired aspect ratio and size, "
"which can be specified fully or partially by --size. "
"Center: crop the largest rectangle from the center. Stretch: stretch the tile. "
"Fit: pad the tiles with white background")
gpu = _PARAMETER(type=bool, default=False,
help="Use GPU acceleration. Requires cupy to be installed and a capable GPU. Note that USUALLY this is useful when you: "
"1. have a lot of tiles (typically > 10000), and"
"2. are using the unfair mode, and"
"3. (for photomosaic videos only) only have few cpu cores"
"Also note: enabling GPU acceleration will disable multiprocessing on CPU for videos"
)
mem_limit = _PARAMETER(type=int, default=4096,
help="The APPROXIMATE memory limit in MB when computing a photomosaic in unfair mode. Applicable both CPU and GPU computing. "
"If you run into memory issues when using GPU, try reduce this memory limit")
tile_info_out = _PARAMETER(type=str, default="",
help="Path to save the list of tile filenames for the collage/photomosaic. If empty, it will not be saved.")
# ---------------- sort collage options ------------------
ratio = _PARAMETER(type=int, default=(16, 9), help="Aspect ratio of the output image", nargs=2)
sort = _PARAMETER(type=str, default="bgr_sum", help="Sort method to use", choices=[
"none", "bgr_sum", "av_hue", "av_sat", "av_lum", "rand"
])
rev_row = _PARAMETER(type=bool, default=False, help="Whether to use the S-shaped alignment.")
rev_sort = _PARAMETER(type=bool, default=False, help="Sort in the reverse direction.")
# ---------------- photomosaic common options ------------------
dest_img = _PARAMETER(type=str, default="", help="The path to the destination image that you want to build a photomosaic for")
colorspace = _PARAMETER(type=str, default="lab", choices=["hsv", "hsl", "bgr", "lab", "luv"],
help="The colorspace used to calculate the metric")
metric = _PARAMETER(type=str, default="euclidean", choices=["euclidean", "cityblock", "chebyshev", "cosine"],
help="Distance metric used when evaluating the distance between two color vectors")
transparent = _PARAMETER(type=bool, default=False,
help="Enable transparency masking. The transparent regions of the destination image will be maintained in the photomosaic"
"Cannot be used together with --salient")
# ---- unfair tile assignment options -----
unfair = _PARAMETER(type=bool, default=False,
help="Whether to allow each tile to be used different amount of times (unfair tile usage). ")
max_width = _PARAMETER(type=int, default=80,
help="Maximum width of the collage. This option is only valid if unfair option is enabled")
freq_mul = _PARAMETER(type=float, default=0.0,
help="Frequency multiplier to balance tile fairless and mosaic quality. Minimum: 0. "
"More weight will be put on tile fairness when this number increases.")
dither = _PARAMETER(type=bool, default=False,
help="Whether to enabled dithering. You must also specify --deterministic if enabled. ")
deterministic = _PARAMETER(type=bool, default=False,
help="Do not randomize the tiles. This option is only valid if unfair option is enabled")
# --- fair tile assignment options ---
dup = _PARAMETER(type=float, default=1,
help="If a positive integer: duplicate the set of tiles by how many times. Can be a fraction")
# ---- saliency detection options ---
salient = _PARAMETER(type=bool, default=False, help="Make photomosaic for salient objects only")
lower_thresh = _PARAMETER(type=float, default=0.5,
help="The threshold for saliency detection, between 0.0 (no object area = blank) and 1.0 (maximum object area = original image)")
# ---- blending options ---
blending = _PARAMETER(type=str, default="alpha", choices=["alpha", "brightness"],
help="The types of blending used. alpha: alpha (transparency) blending. Brightness: blending of brightness (lightness) channel in the HSL colorspace")
blending_level = _PARAMETER(type=float, default=0.0,
help="Level of blending, between 0.0 (no blending) and 1.0 (maximum blending). Default is no blending")
video = _PARAMETER(type=bool, default=False, help="Make a photomosaic video from dest_img which is assumed to be a video")
skip_frame = _PARAMETER(type=int, default=1, help="Make a photomosaic every this number of frames")
# https://stackoverflow.com/questions/26598109/preserve-custom-attributes-when-pickling-subclass-of-numpy-array
class InfoArray(np.ndarray):
def __new__(cls, input_array, info=''):
# Input array is an already formed ndarray instance
# We first cast to be our class type
obj = np.asarray(input_array).view(cls)
# add the new attribute to the created instance
obj.info = info
# Finally, we must return the newly created object:
return obj
def __array_finalize__(self, obj):
# see InfoArray.__array_finalize__ for comments
if obj is None: return
self.info = getattr(obj, 'info', None)
def __reduce__(self):
# Get the parent's __reduce__ tuple
pickled_state = super(InfoArray, self).__reduce__()
# Create our own tuple to pass to __setstate__
new_state = pickled_state[2] + (self.info,)
# Return a tuple that replaces the parent's __setstate__ tuple with our own
return (pickled_state[0], pickled_state[1], new_state)
def __setstate__(self, state):
self.info = state[-1] # Set the info attribute
# Call the parent's __setstate__ with the other tuple elements.
super(InfoArray, self).__setstate__(state[0:-1])
ImgList = List[InfoArray]
cupy_available = False
def fast_sq_euclidean(Asq, Bsq, AB):
AB *= -2
AB += Asq
AB += Bsq
return AB
def fast_cityblock(A, B, axis, out):
Z = A - B
np.abs(Z, out=Z)
return np.sum(Z, axis=axis, out=out)
def fast_chebyshev(A, B, axis, out):
Z = A - B
np.abs(Z, out=Z)
return np.max(Z, axis=axis, out=out)
def to_cpu(X: np.ndarray) -> np.ndarray:
return X.get() if cupy_available else X
def bgr_sum(img: np.ndarray) -> float:
"""
compute the sum of all RGB values across an image
"""
return np.sum(img)
def av_hue(img: np.ndarray) -> float:
"""
compute the average hue of all pixels in HSV color space
"""
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
return np.mean(hsv[:, :, 0])
def av_sat(img: np.ndarray) -> float:
"""
compute the average saturation of all pixels in HSV color space
"""
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
return np.mean(hsv[:, :, 1])
lum_coeffs = np.array([0.241, 0.691, 0.068], dtype=np.float32)[np.newaxis, np.newaxis, :]
def av_lum(img) -> float:
"""
compute the average luminosity
"""
lum = img * lum_coeffs
np.sqrt(lum, out=lum)
return np.mean(lum)
def rand(img: np.ndarray) -> float:
"""
generate a random number for each image
"""
return random.random()
def calc_grid_size(rw: int, rh: int, num_imgs: int, shape: Tuple[int, int, int]) -> Grid:
"""
:param rw: the width of the target image
:param rh: the height of the target image
:param num_imgs: number of images available
:param shape: the shape of a tile
:return: an optimal grid size
"""
possible_wh = []
th, tw, _ = shape
for width in range(1, num_imgs):
height = math.ceil(num_imgs / width)
possible_wh.append((width * tw / (th * height), width, height))
dest_ratio = rw / rh
grid = min(possible_wh, key=lambda x: (x[0] - dest_ratio) ** 2)[1:]
print("Tile shape:", (tw, th))
print("Calculated grid size based on the aspect ratio of the destination image:", grid)
print(f"Collage size will be {grid[0] * tw}x{grid[1] * th}. ")
return grid
def make_collage_helper(grid: Grid, sorted_imgs: ImgList, rev=False, ridx=None, cidx=None, file=None):
grid = grid[::-1]
th, tw, tc = sorted_imgs[0].shape
tile_info = np.full(grid, "background", dtype=str)
if ridx is None or cidx is None:
combined_img = np.empty((grid[0] * th, grid[1] * tw, 4), dtype=np.uint8)
# use an array of references to avoid copying individual tiles
tiles = np.array([None] * len(sorted_imgs), dtype=object)
tiles[:] = sorted_imgs
tiles.shape = grid
if rev:
tiles[1::2] = tiles[1::2, ::-1]
# while we can use a few lines of transpose + reshape to do this,
# we just use an ordinary for loop in order to show the progress
for i, j in tqdm(itertools.product(range(grid[0]), range(grid[1])),
desc="[Aligning tiles]", ncols=pbar_ncols, total=np.prod(grid), file=file):
tile = tiles[i, j]
# if this tile already have an alpha channel, use it
if tile.shape[2] == 4:
combined_img[i * th:(i + 1)*th, j * tw:(j + 1)*tw] = tile
else:
combined_img[i * th:(i + 1)*th, j * tw:(j + 1)*tw, :3] = tile
combined_img[i * th:(i + 1)*th, j * tw:(j + 1)*tw, 3] = 255
tile_info[i, j] = tile.info
else:
assert not rev
combined_img = np.full((grid[0] * th, grid[1] * tw, 4), [255, 255, 255, 0], dtype=np.uint8)
for k in tqdm(range(len(ridx)), desc="[Aligning tiles]", ncols=pbar_ncols, file=file):
i = ridx[k]
j = cidx[k]
combined_img[i * th:(i + 1)*th, j * tw:(j + 1)*tw, :tc] = sorted_imgs[k]
combined_img[i * th:(i + 1)*th, j * tw:(j + 1)*tw, 3] = 255
tile_info[i, j] = sorted_imgs[k].info
return combined_img, f"Grid dimension: {grid[::-1]}\n" + '\n'.join(tile_info.flatten())
def make_collage(grid: Grid, sorted_imgs: ImgList, rev=False):
"""
:param grid: grid size
:param sorted_imgs: list of images sorted in correct position
:param rev: whether to have opposite alignment for consecutive rows
:return: a collage
"""
total = np.prod(grid)
diff = total - len(sorted_imgs)
if diff > 0:
print(f"Note: {diff} transparent tiles will be added to the grid.")
sorted_imgs.extend([InfoArray(
np.full((*sorted_imgs[0].shape[:2], 4), [255, 255, 255, 0], dtype=np.uint8), 'background')] * diff)
elif len(sorted_imgs) > total:
print(f"Note: {len(sorted_imgs) - total} tiles will be dropped from the grid.")
del sorted_imgs[total:]
return make_collage_helper(grid, sorted_imgs, rev)
def alpha_blend(combined_img: np.ndarray, dest_img: np.ndarray, alpha=0.9):
if alpha == 1.0:
return combined_img
if dest_img.shape[2] == 4:
dest_img = cv2.cvtColor(dest_img, cv2.COLOR_BGRA2BGR)
dest_img = dest_img * np.float32(1 - alpha)
dest_img = cv2.resize(dest_img, combined_img.shape[1::-1])
combined_img = combined_img * np.array([alpha, alpha, alpha, 1], dtype=np.float32).reshape(1, 1, 4)
combined_img[:, :, :3] += dest_img
return combined_img.astype(np.uint8)
def brightness_blend(combined_img: np.ndarray, dest_img: np.ndarray, alpha=0.9):
"""
blend the 2 imgs in the lightness channel (L in HSL)
"""
if alpha == 1.0:
return combined_img
if dest_img.shape[2] == 4:
dest_img = cv2.cvtColor(dest_img, cv2.COLOR_BGRA2BGR)
dest_img = cv2.cvtColor(dest_img, cv2.COLOR_BGR2HLS)
dest_l = dest_img[:, :, 1] * np.float32(1 - alpha)
dest_l = cv2.resize(dest_l, combined_img.shape[1::-1])
combined_img_hls = cv2.cvtColor(combined_img[:, :, :3], cv2.COLOR_BGR2HLS)
comb_l = combined_img_hls[:, :, 1] * np.float32(alpha)
comb_l += dest_l
combined_img_hls[:, :, 1] = comb_l
combined_img = combined_img.copy()
combined_img[:, :, :3] = cv2.cvtColor(combined_img_hls, cv2.COLOR_HLS2BGR)
return combined_img
def sort_collage(imgs: ImgList, ratio: Grid, sort_method="pca_lab", rev_sort=False) -> Tuple[Grid, np.ndarray]:
"""
:param imgs: list of images
:param ratio: The aspect ratio of the collage
:param sort_method:
:param rev_sort: whether to reverse the sorted array
:return: [calculated grid size, sorted image array]
"""
t = time.time()
grid = calc_grid_size(ratio[0], ratio[1], len(imgs), imgs[0].shape)
if sort_method == "none":
return grid, imgs
print("Sorting images...")
sort_function = eval(sort_method)
indices = np.array(list(map(sort_function, imgs))).argsort()
if rev_sort:
indices = indices[::-1]
print("Time taken: {}s".format(np.round(time.time() - t, 2)))
return grid, [imgs[i] for i in indices]
def solve_lap(cost_matrix: np.ndarray, v=-1):
if v == -1:
v = sys.__stderr__
"""
solve the linear sum assignment (LAP) problem with progress info
"""
print("Computing optimal assignment on a {}x{} matrix...".format(cost_matrix.shape[0], cost_matrix.shape[1]))
wrapper = JVOutWrapper(v, pbar_ncols)
with stdout_redirector(wrapper):
_, cols, cost = lapjv(cost_matrix, verbose=1)
cost = cost[0]
print("Total assignment cost:", cost)
return cols
def solve_lap_greedy(cost_matrix: np.ndarray, v=None):
assert cost_matrix.shape[0] == cost_matrix.shape[1]
print("Computing greedy assignment on a {}x{} matrix...".format(cost_matrix.shape[0], cost_matrix.shape[1]))
row_idx, col_idx = np.unravel_index(np.argsort(cost_matrix, axis=None), cost_matrix.shape)
cost = 0
row_assigned = np.full(cost_matrix.shape[0], -1, dtype=np.int32)
col_assigned = np.full(cost_matrix.shape[0], -1, dtype=np.int32)
pbar = tqdm(ncols=pbar_ncols, total=cost_matrix.shape[0])
for ridx, cidx in zip(row_idx, col_idx):
if row_assigned[ridx] == -1 and col_assigned[cidx] == -1:
row_assigned[ridx] = cidx
col_assigned[cidx] = ridx
cost += cost_matrix[ridx, cidx]
pbar.update()
if pbar.n == pbar.total:
break
pbar.close()
print("Total assignment cost:", cost)
return col_assigned
def compute_block_map(thresh_map: np.ndarray, block_width: int, block_height: int, lower_thresh: int):
"""
Find the indices of the blocks that contain salient pixels according to the thresh_map
returns [row indices, column indices, resized threshold map] of sizes [(N,), (N,), (W x H)]
"""
height, width = thresh_map.shape
dst_size = (width - width % block_width, height - height % block_height)
if thresh_map.shape[::-1] != dst_size:
thresh_map = cv2.resize(thresh_map, dst_size)
row_idx, col_idx = np.nonzero(thresh_map.reshape(
dst_size[1] // block_height, block_height, dst_size[0] // block_width, block_width).max(axis=(1, 3)) >= lower_thresh
)
return row_idx, col_idx, thresh_map
def dup_to_meet_total(imgs: ImgList, total: int):
"""
note that this function modifies imgs in place
"""
orig_len = len(imgs)
if total < orig_len:
print(f"{total} tiles will be used 1 time. {orig_len - total}/{orig_len} tiles will not be used. ")
del imgs[total:]
return imgs
full_count = total // orig_len
remaining = total % orig_len
imgs *= full_count
if remaining > 0:
print(f"{orig_len - remaining} tiles will be used {full_count} times. {remaining} tiles will be used {full_count + 1} times. Total tiles: {orig_len}.")
imgs.extend(imgs[:remaining])
else:
print(f"Total tiles: {orig_len}. All of them will be used {full_count} times.")
return imgs
def _cosine(A, B):
return 1 - cp.inner(A / cp.linalg.norm(A, axis=1, keepdims=True), B)
def _euclidean(A, B, BsqT):
Asq = cp.sum(A**2, axis=1, keepdims=True)
return fast_sq_euclidean(Asq, BsqT, A.dot(B.T))
def _other(A, B, dist_func, row_stride):
total = A.shape[0]
dist_mat = cp.empty((total, B.shape[1]), dtype=cp.float32)
i = 0
while i < total - row_stride:
next_i = i + row_stride
dist_func(A[i:next_i, cp.newaxis, :], B, out=dist_mat[i:next_i], axis=2)
i = next_i
if i < total:
dist_func(A[i:, cp.newaxis, :], B, out=dist_mat[i:], axis=2)
return dist_mat
def strip_alpha(dest_img: np.ndarray) -> np.ndarray:
if dest_img.shape[2] == 4:
return cv2.cvtColor(dest_img, cv2.COLOR_BGRA2BGR)
return dest_img
def thresh_map_transp(dest_img: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
assert dest_img.shape[2] == 4, "You need an image with transparent background to do this"
return strip_alpha(dest_img), dest_img[:, :, 3] > 0.0
class CachedCDist:
def __init__(self, metric: str, B: np.ndarray):
"""
Simple implementation of scipy.spatial.distance.cdist
"""
if metric == "cosine":
self.args = [B / cp.linalg.norm(B, axis=1, keepdims=True)]
self.func = _cosine
elif metric == "euclidean":
self.args = [B, cp.sum(B**2, axis=1, keepdims=True).T]
self.func = _euclidean
else:
row_stride = LIMIT // (B.size * 4)
B = B[cp.newaxis]
if metric == "cityblock":
self.args = [B, fast_cityblock, row_stride]
elif metric == "chebyshev":
self.args = [B, fast_chebyshev, row_stride]
else:
raise ValueError(f"invalid metric {metric}")
self.func = _other
def __call__(self, A: np.ndarray) -> np.ndarray:
return self.func(A, *self.args)
class MosaicCommon:
def __init__(self, imgs: ImgList, colorspace="lab") -> None:
self.imgs = imgs
self.normalize_first = False
if colorspace == "bgr":
self.flag = None
elif colorspace == "hsv":
self.flag = cv2.COLOR_BGR2HSV
self.normalize_first = True
elif colorspace == "hsl":
self.flag = cv2.COLOR_BGR2HLS
self.normalize_first = True
elif colorspace == "lab":
self.flag = cv2.COLOR_BGR2LAB
elif colorspace == "luv":
self.flag = cv2.COLOR_BGR2LUV
else:
raise ValueError("Unknown colorspace " + colorspace)
def make_photomosaic(self, assignment: np.ndarray, file=None):
return make_collage_helper(self.grid, [self.imgs[i] for i in assignment], file=file)
def make_photomosaic_mask(self, assignment: np.ndarray, ridx: np.ndarray, cidx: np.ndarray, file=None):
return make_collage_helper(self.grid, [self.imgs[i] for i in assignment], False, ridx, cidx, file=file)
def convert_colorspace(self, img: np.ndarray):
if self.flag is None:
return
cv2.cvtColor(img, self.flag, dst=img)
if self.normalize_first:
# for hsv/hsl, h is in range 0~360 while other channels are in range 0~1
# need to normalize
img[:, :, 0] *= 1 / 360.0
def compute_block_size(self, dest_shape: Tuple[int, int, int], grid: Grid):
self.grid = grid
self.block_height = round(dest_shape[0] / grid[1])
self.block_width = round(dest_shape[1] / grid[0])
th, tw, _ = self.imgs[0].shape
if self.block_width > tw or self.block_height > th:
m = max(tw / self.block_width, th / self.block_height)
self.block_width = math.floor(self.block_width * m)
self.block_height = math.floor(self.block_height * m)
self.flat_block_size = self.block_width * self.block_height * 3
print("Block size:", (self.block_width, self.block_height))
self.target_sz = (grid[0] * self.block_width, grid[1] * self.block_height)
print(f"Resizing dest image from {dest_shape[1]}x{dest_shape[0]} to {self.target_sz[0]}x{self.target_sz[1]}")
def imgs_to_flat_blocks(self, metric: str):
img_keys = np.zeros((len(self.imgs), self.block_height, self.block_width, 3), dtype=np.uint8)
for i in range(len(self.imgs)):
cv2.resize(self.imgs[i], (self.block_width, self.block_height), dst=img_keys[i])
img_keys.shape = (-1, self.block_width, 3)
img_keys = img_keys * np.float32(1 / 255.0)
self.convert_colorspace(img_keys)
img_keys.shape = (-1, self.flat_block_size)
img_keys = cp.asarray(img_keys)
self.cdist = CachedCDist(metric, img_keys)
self.img_keys = img_keys
return img_keys
def dest_to_flat_blocks(self, dest_img: np.ndarray):
dest_img = cv2.resize(dest_img, self.target_sz)
dest_img = dest_img * np.float32(1 / 255.0)
self.convert_colorspace(dest_img)
dest_img = cp.asarray(dest_img)
dest_img.shape = (self.grid[1], self.block_height, self.grid[0], self.block_width, 3)
return dest_img.transpose((0, 2, 1, 3, 4)).reshape(-1, self.flat_block_size)
def dest_to_flat_blocks_mask(self, dest_img: np.ndarray, ridx: np.ndarray, cidx: np.ndarray):
dest_img = dest_img * np.float32(1 / 255.0)
self.convert_colorspace(dest_img)
dest_img.shape = (self.grid[1], self.block_height, self.grid[0], self.block_width, 3)
dest_img = dest_img[ridx, :, cidx, :, :]
dest_img.shape = (-1, self.flat_block_size)
print(f"Salient blocks/total blocks = {len(ridx)}/{np.prod(self.grid)}")
return cp.asarray(dest_img)
def calc_salient_col_even(dest_img: np.ndarray, imgs: ImgList, dup=1, colorspace="lab",
metric="euclidean", lower_thresh=0.5, transparent=False, v=None):
"""
Compute the optimal assignment between the set of images provided and the set of pixels constitute of salient objects of the
target image, with the restriction that every image should be used the same amount of times
non salient part of the target image will be transparent
"""
t = time.time()
print("Duplicating {} times".format(dup))
height, width, _ = dest_img.shape
# this is just the initial (minimum) grid size
total = round(len(imgs) * dup)
grid = calc_grid_size(width, height, total, imgs[0].shape)
if transparent:
dest_img, orig_thresh_map = thresh_map_transp(dest_img)
orig_thresh_map = orig_thresh_map.astype(np.float32)
lower_thresh = 0.5
else:
dest_img = strip_alpha(dest_img)
_, orig_thresh_map = cv2.saliency.StaticSaliencyFineGrained_create().computeSaliency(dest_img)
bh_f = height / grid[1]
bw_f = width / grid[0]
# DDA-like algorithm to decrease block size while preserving aspect ratio
if bw_f > bh_f:
bw_delta = 1
bh_delta = bh_f / bw_f
else:
bh_delta = 1
bw_delta = bh_f / bw_f
imgs = imgs.copy()
while True:
block_width = int(bw_f)
block_height = int(bh_f)
if block_width <= 0 or block_height <= 0:
print(f"Warning: Salient area is too small to put down all tiles given the duplication factor of {dup}. "
"You can try to increase the saliency threshold if this is not desired.")
block_width = max(block_width, 1)
block_height = max(block_height, 1)
ridx, cidx, thresh_map = compute_block_map(orig_thresh_map, block_width, block_height, lower_thresh)
break
ridx, cidx, thresh_map = compute_block_map(orig_thresh_map, block_width, block_height, lower_thresh)
if len(ridx) >= total:
break
bw_f -= bw_delta
bh_f -= bh_delta
print(len(ridx), lower_thresh)
dup_to_meet_total(imgs, len(ridx))
mos = MosaicCommon(imgs, colorspace)
mos.block_width = block_width
mos.block_height = block_height
mos.flat_block_size = block_width * block_height * 3
mos.grid = (thresh_map.shape[1] // block_width, thresh_map.shape[0] // block_height)
print("Block size:", (block_width, block_height))
print("Grid size:", mos.grid)
mos.imgs_to_flat_blocks(metric)
dest_img = cv2.resize(dest_img, thresh_map.shape[::-1])
dest_img = mos.dest_to_flat_blocks_mask(dest_img, ridx, cidx)
assignment = solve_lap(to_cpu(mos.cdist(dest_img).T), v)
print("Time taken: {}s".format((np.round(time.time() - t, 2))))
return mos.make_photomosaic_mask(assignment, ridx, cidx)
class MosaicFairSalient:
def __init__(self, *args, **kwargs) -> None:
self.args = args
self.kwargs = kwargs
def process_dest_img(self, dest_img: np.ndarray):
return calc_salient_col_even(dest_img, *self.args[1:], **self.kwargs)
class MosaicFair(MosaicCommon):
def __init__(self, dest_shape: Tuple[int, int, int], imgs: ImgList, dup=1, colorspace="lab",
metric="euclidean", grid=None) -> None:
"""
Compute the optimal assignment between the set of images provided and the set of pixels of the target image,
with the restriction that every image should be used the same amount of times
"""
if grid is not None:
print("Use the provided grid size:", grid)
dup = np.prod(grid) // len(imgs) + 1
else:
# Compute the grid size based on the number images that we have
grid = calc_grid_size(dest_shape[1], dest_shape[0], round(len(imgs) * dup), imgs[0].shape)
total = np.prod(grid)
imgs = dup_to_meet_total(imgs.copy(), total)
if total > 10000:
print("Warning: this may take longer than 5 minutes to compute")
super().__init__(imgs, colorspace)
self.compute_block_size(dest_shape, grid)
self.imgs_to_flat_blocks(metric)
def process_dest_img(self, dest_img: np.ndarray, file=None):
dest_img = self.dest_to_flat_blocks(strip_alpha(dest_img))
cols = solve_lap(to_cpu(self.cdist(dest_img).T), file)
return self.make_photomosaic(cols)
class MosaicUnfair(MosaicCommon):
def __init__(self, dest_shape: Tuple[int, int, int], imgs: ImgList, max_width: int, colorspace: str, metric: str,
lower_thresh: float, freq_mul: float, randomize: bool, dither=False, transparent=False) -> None:
# Because we don't have a fixed total amount of images as we can used a single image
# for arbitrary amount of times, we need user to specify the maximum width in order to determine the grid size.
dh, dw, _ = dest_shape
th, tw, _ = imgs[0].shape
grid = (max_width, round(dh * (max_width * tw / dw) / th))
print("Calculated grid size based on the aspect ratio of the image provided:", grid)
print("Collage size:", (grid[0] * tw, grid[1] * th))
super().__init__(imgs, colorspace)
self.compute_block_size(dest_shape, grid)
img_keys = self.imgs_to_flat_blocks(metric)
# number of rows in the cost matrix
# note here we compute the cost matrix chunk by chunk to limit memory usage
# a bit like sklearn.metrics.pairwise_distances_chunked
num_rows = int(np.prod(grid))
num_cols = img_keys.shape[0]
print(f"Distance matrix size: {(num_rows, num_cols)} = {num_rows * num_cols * 4 / 2**20}MB")
self.row_stride = (LIMIT - (img_keys.size + num_rows * (1 + self.flat_block_size)) * 4) // (num_cols * 4)
if self.row_stride >= num_rows:
print("No chunking will be performed on the distance matrix calculation")
else:
print(f"Chunk size: {self.row_stride*num_cols* 4 / 2**20}MB | {self.row_stride}/{num_rows}")
if freq_mul > 0:
self.row_stride //= 16
self.indices_freq = cp.empty(num_cols, dtype=cp.float32)
self.row_range = cp.arange(0, self.row_stride, dtype=cp.int32)[:, cp.newaxis]
self.temp = cp.arange(0, num_cols, dtype=cp.float32)
else:
self.row_stride //= 4
self.freq_mul = freq_mul
self.lower_thresh = lower_thresh
self.randomize = randomize
self.transparent = transparent
self.saliency = False
self.dither = dither
_saliency_enabled = lower_thresh is not None
if transparent:
self.lower_thresh = 0.5
if dither:
print("Warning: dithering is not supported when transparency masking is on. Dithering will be turned off")
self.dither = False
if _saliency_enabled:
print("Warning: saliency is not supported when transparency masking is on. Saliency will be turned off")
elif self.dither:
if _saliency_enabled:
print("Warning: saliency is not supported when dithering is on. Saliency will be turned off")
if cp is not np:
print("Warning: dithering is typically slower with --gpu enabled")
if randomize:
print("Warning: dithering is not supported when randomization is enabled. Randomization will be turned off.")
self.randomize = False
elif _saliency_enabled:
self.saliency = cv2.saliency.StaticSaliencyFineGrained_create()
def process_dest_img(self, dest_img: np.ndarray, file=None):
if self.saliency or self.transparent:
dest_img = cv2.resize(dest_img, self.target_sz)
if self.saliency:
dest_img = strip_alpha(dest_img)
_, thresh_map = self.saliency.computeSaliency(dest_img)
else:
dest_img, thresh_map = thresh_map_transp(dest_img)
ridx, cidx, thresh_map = compute_block_map(thresh_map, self.block_width, self.block_height, self.lower_thresh)
dest_img = self.dest_to_flat_blocks_mask(dest_img, ridx, cidx)
else:
# strip alpha in case a transparent image is passed in but --transparent flag is not enabled
dest_img = self.dest_to_flat_blocks(strip_alpha(dest_img))
total = dest_img.shape[0]
assignment = cp.empty(total, dtype=cp.int32)
if self.dither:
dest_img.shape = (*self.grid[::-1], -1)
grid_assignment = assignment.reshape(self.grid[::-1])
coeffs = cp.array([0.4375, 0.1875, 0.3125, 0.0625])[..., cp.newaxis]
pbar = tqdm(desc="[Computing assignments]", total=total, ncols=pbar_ncols, file=file)
i = 0
row_stride = self.row_stride
if self.freq_mul > 0:
_indices = np.arange(0, total, dtype=np.int32)
if self.randomize:
np.random.shuffle(_indices)
dest_img = dest_img[_indices] # reorder the rows of dest img
indices_freq = self.indices_freq
indices_freq.fill(0.0)
freq_mul = self.freq_mul
if self.dither:
for i in range(0, dest_img.shape[0] - 1):
j = 0
dist = self.cdist(dest_img[i, j:j+1])[0]
dist[cp.argsort(dist)] = self.temp
dist += indices_freq
best_i = grid_assignment[i, j] = cp.argmin(dist)
indices_freq[best_i] += freq_mul
pbar.update()
for j in range(1, dest_img.shape[1] - 1):
block = dest_img[i, j]
dist = self.cdist(block[cp.newaxis])[0]
dist[cp.argsort(dist)] = self.temp
dist += indices_freq
best_i = grid_assignment[i, j] = cp.argmin(dist)
indices_freq[best_i] += freq_mul
quant_error = (block - self.img_keys[best_i])[cp.newaxis, ...] * coeffs
dest_img[i, j + 1] += quant_error[0]
dest_img[i + 1, j - 1:j + 2] += quant_error[1:]
pbar.update()
j += 1
dist = self.cdist(dest_img[i, j:j+1])[0]
dist[cp.argsort(dist)] = self.temp
dist += indices_freq
best_i = grid_assignment[i, j] = cp.argmin(dist)
indices_freq[best_i] += freq_mul
pbar.update()
# last row
dist_mat = self.cdist(dest_img[-1])
dist_mat[cp.arange(0, dest_img.shape[1], dtype=cp.int32)[:, cp.newaxis], cp.argsort(dist_mat, axis=1)] = self.temp
for j in range(0, dest_img.shape[1]):
row = dist_mat[j, :]
row += indices_freq
idx = cp.argmin(row)
grid_assignment[-1, j] = idx
indices_freq[idx] += freq_mul
pbar.update()
else:
while i < total - row_stride:
dist_mat = self.cdist(dest_img[i:i+row_stride])
dist_mat[self.row_range, cp.argsort(dist_mat, axis=1)] = self.temp
j = 0
while j < row_stride:
row = dist_mat[j, :]
row += indices_freq
idx = cp.argmin(row)
assignment[i] = idx
indices_freq[idx] += freq_mul
i += 1
j += 1
pbar.update()
if i < total:
dist_mat = self.cdist(dest_img[i:])
dist_mat[self.row_range[:total - i], cp.argsort(dist_mat, axis=1)] = self.temp
j = 0
while i < total:
row = dist_mat[j, :]
row += indices_freq
idx = cp.argmin(row)
assignment[i] = idx
indices_freq[idx] += freq_mul
i += 1
j += 1
pbar.update()
assignment[_indices] = assignment.copy()
else:
if self.dither:
for i in range(0, dest_img.shape[0] - 1):
grid_assignment[i, 0] = cp.argmin(self.cdist(dest_img[i, 0:1])[0])
pbar.update()
for j in range(1, dest_img.shape[1] - 1):
block = dest_img[i, j]
dist_mat = self.cdist(block[cp.newaxis])
best_i = cp.argmin(dist_mat[0])
grid_assignment[i, j] = best_i
quant_error = (block - self.img_keys[best_i])[cp.newaxis, ...] * coeffs
dest_img[i, j + 1] += quant_error[0]
dest_img[i + 1, j - 1:j + 2] += quant_error[1:]
pbar.update()
grid_assignment[i, -1] = cp.argmin(self.cdist(dest_img[i, -1:])[0])
pbar.update()
# last row
cp.argmin(self.cdist(dest_img[-1]), axis=1, out=grid_assignment[-1])
pbar.update(dest_img.shape[1])
else:
while i < total - row_stride:
next_i = i + row_stride
dist_mat = self.cdist(dest_img[i:next_i])
cp.argmin(dist_mat, axis=1, out=assignment[i:next_i])
pbar.update(row_stride)
i = next_i
if i < total:
dist_mat = self.cdist(dest_img[i:])
cp.argmin(dist_mat, axis=1, out=assignment[i:])
pbar.update(total - i)
pbar.close()
assignment = to_cpu(assignment)
if self.saliency or self.transparent:
return self.make_photomosaic_mask(assignment, ridx, cidx, file=file)
return self.make_photomosaic(assignment, file=file)
def imwrite(filename: str, img: np.ndarray) -> None:
ext = os.path.splitext(filename)[1]
result, n = cv2.imencode(ext, img)
assert result, "Error saving the collage"
n.tofile(filename)
def save_img(img: np.ndarray, path: str, suffix: str, file=None) -> None:
if len(path) == 0:
path = "result.png"
if len(suffix) == 0:
print("Saving to", path, file=file)
imwrite(path, img)
else:
file_path, ext = os.path.splitext(path)
path = f'{file_path}{suffix}{ext}'
print("Saving to", path, file=file)
imwrite(path, img)
def get_size(img):
try:
w, h = imagesize.get(img)
return int(w), int(h)
except:
return 0, 0
def get_size_slow(filename: str):
img = imread(filename)
if img is None:
return 0, 0
return img.shape[1::-1]
def infer_size(pool, files: List[str], infer_func: Callable[[str], Tuple[int, int]], i_type: str):
sizes = defaultdict(int)
for w, h in tqdm(pool.imap_unordered(infer_func, files, chunksize=64),
total=len(files), desc=f"[Inferring size ({i_type})]", ncols=pbar_ncols):
if w == 0 or h == 0: # skip zero size images
continue
sizes[Fraction(w, h)] += 1
sizes = [(args[1], args[0].numerator / args[0].denominator) for args in sizes.items()]
sizes.sort()
return sizes
def read_images(pic_path: str, img_size: List[int], recursive, pool, flag="stretch", auto_rotate=0) -> ImgList:
assert os.path.isdir(pic_path), "Directory " + pic_path + "is non-existent"
files = []
print("Scanning files...")
for root, _, file_list in os.walk(pic_path):
for f in file_list:
files.append(os.path.join(root, f))
if not recursive:
break
if len(img_size) == 1:
sizes = infer_size(pool, files, get_size, "fast")
if len(sizes) == 0:
print("Warning: unable to infer image size through metadata. Will try reading the entire image (slow!)")
sizes = infer_size(pool, files, get_size_slow, "slow")
assert len(sizes) > 0, "Fail to infer size. All of your images are in an unsupported format!"
# print("Aspect ratio (width / height, sorted by frequency) statistics:")
# for freq, ratio in sizes:
# print(f"{ratio:6.4f}: {freq}")
most_freq_ratio = 1 / sizes[-1][1]
img_size = (img_size[0], round(img_size[0] * most_freq_ratio))
print("Inferred tile size:", img_size)
else:
assert len(img_size) == 2
img_size = (img_size[0], img_size[1])
read_img = read_img_other
if flag == "center":
read_img = read_img_center
if flag == "fit":
read_img = read_img_fit
result = [
r for r in tqdm(
pool.imap_unordered(
read_img,
zip(files, itertools.repeat(img_size, len(files)), itertools.repeat(auto_rotate, len(files))),
chunksize=32),
total=len(files), desc="[Reading files]", unit="file", ncols=pbar_ncols)
if r is not None
]
print(f"Read {len(result)} images. {len(files) - len(result)} files cannot be decoded as images.")
return result
def imread(filename: str, flag=cv2.IMREAD_COLOR) -> np.ndarray:
"""
like cv2.imread, but can read images whose path contain unicode characters
"""
try:
f = np.fromfile(filename, np.uint8)
if not f.size:
return None
return cv2.imdecode(f, flag)
except:
return None
def read_img_center(args: Tuple[str, Tuple[int, int], int]):
# crop the largest rectangle from the center
img_file, img_size, rot = args
img = imread(img_file)
if img is None:
return None
ratio = img_size[0] / img_size[1]
# rotate the image if possible to preserve more area
h, w, _ = img.shape
if rot != 0 and abs(h / w - ratio) < abs(w / h - ratio):
img = np.rot90(img, k=rot)
w, h = h, w
cw = round(h * ratio) # cropped width
ch = round(w / ratio) # cropped height