Skip to content

Instantly share code, notes, and snippets.

@annidy
Last active December 20, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annidy/6137965 to your computer and use it in GitHub Desktop.
Save annidy/6137965 to your computer and use it in GitHub Desktop.
png资源提取器
#!/usr/bin/python
import os
import sys
import struct
ifp = sys.stdin
pngcount = 0
filename = ""
def findstart():
pos = ifp.tell()
while True:
magic = ifp.read(8)
pos = pos + len(magic)
if not magic:
return -1
if magic == "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a":
return pos-8
pos = pos - len(magic) + 1
ifp.seek(pos)
return -1
def findend():
pos = ifp.tell()
while True:
magic = ifp.read(8)
pos = pos + len(magic)
if not magic:
return -1
if magic[4:] == "IEND":
chunk, = struct.unpack("I", magic[0:4])
return pos - 8 + chunk
pos = pos - len(magic) + 1
ifp.seek(pos)
return -1
def save(s, e):
global pngcount
ofp = open("%s_%d.png"%(filename, pngcount), "wb+")
ifp.seek(s)
ofp.write(ifp.read(e-s))
ofp.close()
pngcount = pngcount + 1
return
def main():
while True:
s = findstart()
if s < 0:
break
e = findend()
if e < 0:
break
save(s, e)
pass
if __name__ == "__main__":
filename = os.path.basename(sys.argv[1])
ifp = open(sys.argv[1], "rb")
if not ifp:
print "No such a file", sys.argv[1]
else:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment