-
Notifications
You must be signed in to change notification settings - Fork 0
/
pngreader.py
executable file
·58 lines (49 loc) · 1.54 KB
/
pngreader.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
import png, sys
def main():
if len(sys.argv) == 1:
print("Please provide a png file")
elif len(sys.argv) < 4:
inputFile = sys.argv[1]
inF = open(inputFile, 'rb')
r = png.Reader(inF)
outputFile = 'out.txt'
if len(sys.argv) == 3:
outputFile = sys.argv[2]
outF = open(outputFile, 'w')
values = readFile(r)
for i in values:
value = i + "\t" + str(values[i]) + "\n"
outF.write(value)
else:
print("Too many arguments")
def toHex(rgb):
s = str(hex(rgb))[2:].upper()
if len(s) < 2:
s = "0" + s
return s
def readFile(r):
readerObj = r.read()
arrayObjList = list(readerObj[2])
allValues = ""
palDict = {}
for i in range(0,len(arrayObjList)):
arrayObj = arrayObjList[i]
rowRGBValues = arrayObj.tolist()
j = 0
for j in range(0,len(rowRGBValues),4):
string = ""
r = toHex(rowRGBValues[j])
g = toHex(rowRGBValues[j+1])
b = toHex(rowRGBValues[j+2])
a = toHex(rowRGBValues[j+3])
# string = r + " " + g + " " + b + " " + a + "\n"
# string = r + g + b + a + "\n"
# string = r + g + b + a
string = r + g + b
if string in palDict:
palDict[string] = palDict[string] + 1
else:
palDict[string] = 1
allValues = allValues + string
return palDict
main()