-
Notifications
You must be signed in to change notification settings - Fork 2
/
icon.py
163 lines (136 loc) · 6.13 KB
/
icon.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
# Icon processing
# Resize each icon to a 4x4 image. To see if a given icon matches one we have, also
# resize it to a 4x4 image and compare each pixel
from __future__ import division
import Image
from os import listdir
from os.path import isfile, join
import pickle
import operator
import os
import ImageDraw
iconData = None
iconFolder = "icon/"
# Get the difference i color between two pixels. The difference is calculated by adding
# the sum of differences for each layer (RGB)
def pixelDiff(pixel, color):
# This is for performance purposes; this function is called thousands of times per second
if(pixel[0] > color[0]):
if(pixel[1] > color[1]):
if(pixel[2] > color[2]):
return (pixel[0] - color[0]) + (pixel[1] - color[1]) + (pixel[2] - color[2])
else:
return (pixel[0] - color[0]) + (pixel[1] - color[1]) - (pixel[2] - color[2])
else:
if(pixel[2] > color[2]):
return (pixel[0] - color[0]) - (pixel[1] - color[1]) + (pixel[2] - color[2])
else:
return (pixel[0] - color[0]) - (pixel[1] - color[1]) - (pixel[2] - color[2])
else:
if(pixel[1] > color[1]):
if(pixel[2] > color[2]):
return -(pixel[0] - color[0]) + (pixel[1] - color[1]) + (pixel[2] - color[2])
else:
return -(pixel[0] - color[0]) + (pixel[1] - color[1]) - (pixel[2] - color[2])
else:
if(pixel[2] > color[2]):
return -(pixel[0] - color[0]) - (pixel[1] - color[1]) + (pixel[2] - color[2])
else:
return -(pixel[0] - color[0]) - (pixel[1] - color[1]) - (pixel[2] - color[2])
def iconDataDiff(icon, data):
# Expanded out loop to save running time
return pixelDiff(icon[0], data[0]) + \
pixelDiff(icon[1], data[1]) + \
pixelDiff(icon[2], data[2]) + \
pixelDiff(icon[3], data[3]) + \
pixelDiff(icon[4], data[4]) + \
pixelDiff(icon[5], data[5]) + \
pixelDiff(icon[6], data[6]) + \
pixelDiff(icon[7], data[7]) + \
pixelDiff(icon[8], data[8]) + \
pixelDiff(icon[9], data[9]) + \
pixelDiff(icon[10], data[10]) + \
pixelDiff(icon[11], data[11]) + \
pixelDiff(icon[12], data[12]) + \
pixelDiff(icon[13], data[13]) + \
pixelDiff(icon[14], data[14]) + \
pixelDiff(icon[15], data[15]) + \
pixelDiff(icon[16], data[16]) + \
pixelDiff(icon[17], data[17]) + \
pixelDiff(icon[18], data[18]) + \
pixelDiff(icon[19], data[19]) + \
pixelDiff(icon[20], data[20]) + \
pixelDiff(icon[21], data[21]) + \
pixelDiff(icon[22], data[22]) + \
pixelDiff(icon[23], data[23]) + \
pixelDiff(icon[24], data[24])
def generateIconDataFile():
icons = [ f for f in listdir(iconFolder) if isfile(join(iconFolder,f)) and f.split(".")[-1] == "png" ]
icon_dat = {}
for i in icons:
imdat = imageToIconData(Image.open(iconFolder+i))
icon_dat[i.split(".")[0]] = imdat
pickle.dump(icon_dat, open(iconFolder+"icons.dat", "w"))
return icon_dat
def getIconData():
global iconData;
if iconData != None:
return iconData
if isfile(iconFolder+"icons.dat"):
iconData = pickle.load(open(iconFolder+"icons.dat", "r"))
else:
iconData = generateIconDataFile()
return iconData
def imageToIconData(im):
imdat = im.resize((5, 5), Image.ANTIALIAS).getdata()
dat = []
for i in range(0, 5):
for j in range(0, 5):
dat.append(imdat[i*5 + j][0:3])
return dat
# Convert an image to the name of an icon
def imageToIconName(im, restrict = None):
imdat = imageToIconData(im)
icon_dat = getIconData()
differences = {};
for _, name in enumerate(icon_dat):
if restrict and not name.startswith(restrict) and name != "blank":
continue
differences[name] = iconDataDiff(imdat, icon_dat[name])
matches = sorted(differences.iteritems(), key=operator.itemgetter(1))
#print matches[0][0], matches[0][1], matches[1][0], matches[1][1], matches[2][0], matches[2][1]
return matches[0][0]
# Utility function that is used to automatically generated icons for activated versions of items
# on cooldown
def generateActivatedItemIcons():
icons = [ f for f in listdir("icons_activated/") if isfile(join("icons_activated/",f)) and f.split(".")[-1] == "png" ]
for i in icons:
filename = ".".join(i.split(".")[0:-1])
im = Image.open("icons_activated/"+i).convert("RGBA")
color_layer = Image.new('RGBA', im.size, (0, 0, 0))
line_layer = Image.new('RGBA', im.size, (109, 109, 109))
alpha_mask = Image.new('L', im.size, 0)
alpha_mask_draw = ImageDraw.Draw(alpha_mask)
alpha_mask_draw.line([(im.size[0]//2, im.size[1]//2), (im.size[0]//2, 0)], fill=186, width = 3)
newim = Image.blend(im, color_layer, 0.51)
newim = Image.composite(line_layer, newim, alpha_mask)
newim.save("icons_activated/"+filename+"-activated.png")
newim = Image.blend(im, color_layer, 0.29)
newim = Image.composite(line_layer, newim, alpha_mask)
newim.save("icons_activated/"+filename+"-activated-done.png")
rect_layer = Image.new('RGBA', im.size, (0, 0, 0))
rect_alpha_mask = Image.new("L", im.size, 0)
rect_alpha_mask_draw = ImageDraw.Draw(rect_alpha_mask)
rect_alpha_mask_draw.rectangle((0, 0, im.size[0]//2, im.size[1]), fill=131)
rect_alpha_mask_draw.rectangle((im.size[0]//2+1, 0, im.size[0], im.size[1]), fill=75)
newim = Image.composite(rect_layer, im, rect_alpha_mask)
newim = Image.composite(line_layer, newim, alpha_mask)
newim.save("icons_activated/"+filename+"-activated-half.png")
def setIconFolder(folder = "icon/"):
global iconFolder
iconFolder = folder
if __name__ == "__main__":
print "Generating icon data file."
dat = generateIconDataFile()
print "Icon data file generated. "+str(len(dat))+" icons in database."
generateActivatedItemIcons()