-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
136 lines (109 loc) · 5.6 KB
/
main.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
# what this program needs to do
# 1. get most prominent colour of an image, split into XxY blocks (width = width/X, height = height/Y)
# 2. get most prominent colour of all emoji, put into array
# 3. match (maybe exclude white?) emoji to block
# 3.1 this may not work (maybe drawn emoji is detected as most dominant white), so second method is to force the matching using opencv magic
# 4. render final image made out of emoji
# my scripts
import get_colour
import investigate_emoji
import investigate_image
import match
import render
# other imports
import os
import cv2
import csv
import math
import errno
from operator import attrgetter # used for sorting
def main():
# settings
overwrite_pic = False # change to True every time after you change the image
overwrite_emoji = False # change to True every time after you modify subsample, or emojis
subsample = 2# default 1, if subsample = 2, then the no. of emojis that will be used is 2**2, as it divides both width and height by subsample ratio
# picture paths
print("### SETTING PATHS...")
base_repo_path = os.path.dirname(os.path.realpath(__file__))
target_pic_path = base_repo_path + "/example_pic/google2.0.0.jpg" # target pic to replace with emojis
emoji_folder_path = base_repo_path + "/emoji_128_non_transparent/" # where the emojis are located
# temp files to be created
target_csv_path = base_repo_path + "/tmp/pic_colour.csv"
emoji_csv_path = base_repo_path + "/tmp/emoji_colour.csv"
try:
os.makedirs(base_repo_path + "/tmp/")
except OSError as e:
if e.errno != errno.EEXIST:
raise
render_path = base_repo_path + "/render.png" # final image path
print("pic path is %s" % target_pic_path)
print("emoji path is %s" % emoji_folder_path)
print("########################")
# first select target image and get properties
try:
img, properties = investigate_image.get_image(target_pic_path)
except Exception as e:
print(str(e))
assert False, "No image loaded."
aspect_ratio = properties["width"] / properties["height"]
# find how many emojis there are
print("### GETTING LIST OF EMOJIS...")
list_emoji = investigate_emoji.get_list_emojis(emoji_folder_path)
num_emoji = len(list_emoji)*(subsample**2)
print("Detected %d emojis in %s." % (num_emoji, emoji_folder_path))
print("### DETERMINING OPTIMAL SECTOR SIZES...")
proposed_w_cut = int(math.floor(subsample * math.sqrt((num_emoji/subsample**2) / (1/aspect_ratio))))
proposed_h_cut = int(math.floor(subsample * math.sqrt((num_emoji/subsample**2) / (aspect_ratio))))
no_sectors = proposed_w_cut * proposed_h_cut
print("Propose wcut %d, hcut %d. Will use %d emojis, %d unused of %d." % (proposed_w_cut, proposed_h_cut, no_sectors, num_emoji-no_sectors, num_emoji))
# split the target image into chunks
print("### STARTING SPLIT IMAGE...")
split_img, final_cut_size = investigate_image.split_image(img, properties, proposed_w_cut, proposed_h_cut)
if split_img is None:
print("ERROR: no split image returned")
else:
# get most dominant colour for each chunk of target image
if overwrite_pic:
print("### STARTING ANALYSIS OF ORIGINAL IMAGE...")
with open(target_csv_path, 'w') as csvfile:
csvfile.write("r|g|b|start_x|end_x|start_y|end_y\n")
for i in range(proposed_h_cut):
for j in range(proposed_w_cut):
curr = i*proposed_w_cut + j + 1
print("\rProcessing sector {} of {} ({}%)...".format(curr, no_sectors, int(100*curr/no_sectors)), end="")
operate_on = split_img[i][j]
(r, g, b) = get_colour.get_dominant_colour(operate_on.image)
csvfile.write("%d|%d|%d|%d|%d|%d|%d\n" % (r, g, b, operate_on.start_x, operate_on.end_x, operate_on.start_y, operate_on.end_y))
print("\n")
csvfile.close()
# process emojis
if overwrite_emoji:
print("### STARTING PROCESS EMOJI...")
investigate_emoji.get_emoji_colour(list_emoji, emoji_csv_path, subsample=subsample)
# match
print("### STARTING MATCH...")
try:
with open(target_csv_path) as pic_csv, open(emoji_csv_path) as emoji_csv:
pic_list, emoji_list = match.parse_csv(pic_csv, emoji_csv)
pic_csv.close()
emoji_csv.close()
except FileNotFoundError:
print("ERROR: Please switch overwrite_* variables to True to generate the csv's.")
raise
pic_hsv, emoji_hsv = match.convert_list_to_hsv(pic_list, emoji_list)
# sort the picture csv and the emoji csv by their colour, represented by the hue
pic_sorted = sorted(pic_hsv, key=attrgetter('h'), reverse=False)
emoji_sorted = sorted(emoji_hsv, key=attrgetter('h'), reverse=False)
final = match.match(pic_sorted, emoji_sorted, randomize=False, base_repo_path=base_repo_path)
final = sorted(final, key=attrgetter('start_y', 'start_x'))
# use results of match to render
print("### STARTING FINAL RENDER...")
print("Final length {}, final cut size {}".format(len(final), final_cut_size))
rendered = render.generate_final_image(final, properties, target=final_cut_size)
#cv2.imshow('final', rendered)
#cv2.waitKey(0)
print("### SAVING FINAL RENDER TO DISK...")
cv2.imwrite(render_path, rendered)
print("Image saved to {}".format(render_path))
print("### COMPLETE")
main()