-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.py
More file actions
77 lines (65 loc) · 2.48 KB
/
cube.py
File metadata and controls
77 lines (65 loc) · 2.48 KB
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
"""
Created Wed Dec 19 09:56:52 2018
@author: tlemon
"""
from PIL import Image
import numpy as np
def dist(pt,ref):
r1,g1,b1 = pt
r2,g2,b2 = ref
d = np.sqrt((r2-r1)**2 + (g2-g1)**2 + (b2-b1)**2)
return d
def getSquareColors(image):
im = Image.open(image)
size = im.size
pix = im.load()
skip = 100
pixMap = []
for i in range(0,size[1],skip):
hold = []
for j in range(0,size[0],skip):
p = pix[j,i]
hold.append(p)
pixMap.append(hold)
pixMap = np.array(pixMap)
tl = pixMap[0:int(len(pixMap[0])/3)+1,0:int(len(pixMap)/3)+1]
tm = pixMap[0:int(len(pixMap[0])/3)+1,\
int(len(pixMap)/3)+1:2*int(len(pixMap)/3)+1]
tr = pixMap[0:int(len(pixMap[0])/3)+1,\
2*int(len(pixMap)/3)+1:int(len(pixMap))+1]
ml = pixMap[int(len(pixMap[0])/3)+1:2*int(len(pixMap[0])/3)+1,\
0:int(len(pixMap)/3)+1]
mm = pixMap[int(len(pixMap[0])/3)+1:2*int(len(pixMap[0])/3)+1,\
int(len(pixMap)/3)+1:2*int(len(pixMap)/3)+1]
mr = pixMap[int(len(pixMap[0])/3)+1:2*int(len(pixMap[0])/3)+1,\
2*int(len(pixMap)/3)+1:int(len(pixMap))+1]
bl = pixMap[2*int(len(pixMap[0])/3)+1:int(len(pixMap[0]))+1,\
0:int(len(pixMap)/3)+1]
bm = pixMap[2*int(len(pixMap[0])/3)+1:int(len(pixMap[0]))+1,\
int(len(pixMap)/3)+1:2*int(len(pixMap)/3)+1]
br = pixMap[2*int(len(pixMap[0])/3)+1:int(len(pixMap[0]))+1,\
2*int(len(pixMap)/3)+1:int(len(pixMap))+1]
squares = [[tl,tm,tr],[ml,mm,mr],[bl,bm,br]]
refColors = [['W',[255,255,255]],['Y',[255,255,0]],['O',[255,50,0]],\
['R',[255,0,0]],['G',[0,255,0]],['B',[0,0,255]]]
txtColors = []
for row in squares:
hold = []
for sq in row:
rMean = gMean = bMean = count = 0
for line in sq:
for item in line:
rMean += item[0]
gMean += item[1]
bMean += item[2]
count += 1
rMean,gMean,bMean = rMean/count,gMean/count,bMean/count
closestRef = []
for ref in refColors:
closestRef.append(dist([rMean,gMean,bMean],ref[1]))
match = closestRef.index(min(closestRef))
hold.append(refColors[match][0])
txtColors.append(hold)
txtColors = np.array(txtColors)
return txtColors
print("hello")