-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorAnalysis.py
64 lines (48 loc) · 2.23 KB
/
colorAnalysis.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
import glob
from PIL import Image
from collections import Counter
def berechne(): #color analysing function
listMostUsed = []
for file in glob.glob("*.jpg"):
img_file = Image.open(file)
#img = img_file.convert('P', palette=Image.ADAPTIVE, colors=100)
img = img_file.resize((300,300))
rgb_img = img.convert('RGB')
[xs, ys] = img.size
for x in range(0, xs):
for y in range(0, ys):
r, g, b = rgb_img.getpixel((x,y))
r = reduceColor(r)
g = reduceColor(g)
b = reduceColor(b)
#if 2*r != g+b:
a = '#%02x%02x%02x' % (r, g, b)
#listMostUsed.append((r,g,b))
if 2*g != r+b: #deletes grey tones as they supress all other colors, implement on and of!
listMostUsed.append((a))
if 2*b != r+g:
listMostUsed.append((a))
b = Counter(listMostUsed).most_common(125)
return b #returned list
def howmany(): #counts how many files will be processed
counter = 0
for file in glob.glob("*.jpg"):
counter += 1
return str(counter) + " images were analysed."
def reduceColor(a): #color reducing and rounding function
a /= 255
a = round(a, 2)
a /= (0.2)
a = round(a)
a = (a * 51)
return a
def readListOne(a, list): #split list into tuples with color and count values
return list[a] #return tuple on position a
def getColor(a): #return color values of tuple
color, number = (a)
return color
def getNumber(a): #returns count value of tuple
color, number = (a)
return number
def readList(a):
return getColor(readListOne(a, berechne()))