-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphotomosaic.py
69 lines (54 loc) · 2.37 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
"""
@author: Thang Nguyen <[email protected]>
"""
import argparse
from importlib.resources import path
import cv2
import numpy as np
import glob
from itertools import product
import sys
def get_args():
parser = argparse.ArgumentParser("Viet Nguyen Photomosaic")
parser.add_argument("--input", type=str,
default="data/input.jpg", help="Path to input image")
parser.add_argument("--output", type=str,
default="data/output.jpg", help="Path to output image")
parser.add_argument("--pool", type=str, default="image_pool/Flower",
help="Path to directory containing component images")
parser.add_argument("--stride", type=int, default=30,
help="size of each component image")
args = parser.parse_args()
return args
def get_component_images(path, size):
images = []
avg_colors = []
for image_path in glob.glob("{}/*.png".format(path)) + glob.glob("{}/*.jpg".format(path)):
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
image = cv2.resize(image, (size, size))
images.append(image)
avg_colors.append(np.sum(np.sum(image, axis=0), axis=0) / (size ** 2))
return images, np.array(avg_colors)
def main(opt):
input_image = cv2.imread(opt.input, cv2.IMREAD_COLOR)
height, width, num_channels = input_image.shape
blank_image = np.zeros((height, width, 3), np.uint8)
images, avg_colors = get_component_images(opt.pool, opt.stride)
# Making PhotoMosaic
for i, j in product(range(int(width / opt.stride)), range(int(height / opt.stride))):
partial_input_image = input_image[j * opt.stride: (j + 1) * opt.stride,
i * opt.stride: (i + 1) * opt.stride, :]
partial_avg_color = np.sum(
np.sum(partial_input_image, axis=0), axis=0) / (opt.stride ** 2)
distance_matrix = np.linalg.norm(
partial_avg_color - avg_colors, axis=1)
idx = np.argmin(distance_matrix)
blank_image[j * opt.stride: (j + 1) * opt.stride, i *
opt.stride: (i + 1) * opt.stride, :] = images[idx]
# Adding (blending) two images code
img = cv2.addWeighted(input_image, float(
70/100), blank_image, float(30/100), 0)
cv2.imwrite(opt.output, img)
if __name__ == '__main__':
opt = get_args()
main(opt)