-
Notifications
You must be signed in to change notification settings - Fork 0
/
pngparser.py
executable file
·42 lines (39 loc) · 1.05 KB
/
pngparser.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
#!/usr/bin/env python
import sys
import os
def usage():
sys.stderr.write("{0}: usage: {0} <filename>\n".format(os.path.basename(sys.argv[0])))
sys.exit(1)
def length2int(size):
n, k = 0, 1
for i in size[::-1]:
n += ord(i) * k
k *= 0x100
return n
def main():
if len(sys.argv) == 1:
usage()
filename = sys.argv[1]
try:
f = open(filename, "r")
except IOError, s:
sys.stderr.write(str(s) + "\n")
sys.exit(2)
sign = f.read(8)
if sign[1:4] != "PNG":
sys.stderr.write("{0}: {1} is not a PNG file\n".format(os.path.basename(sys.argv[0]), filename))
f.close()
sys.exit(3)
sys.stdout.write(sign)
while True:
chunkrawlength = f.read(4)
chunklength = length2int(chunkrawlength)
chunktype = f.read(4)
chunkdata = f.read(chunklength)
chunkcrc = f.read(4)
""" working with chunks here """
if chunktype == "IEND":
break
f.close()
if __name__ == "__main__":
sys.exit(main())