Howto Extract PNG from binary file @ 0x80?

flamesrock flamesrock at gmail.com
Mon Jan 3 19:03:58 EST 2005


Well, I've been working on getting this code to work, and I think I
*almost* have it...

First, according to ghex, it seems the PNG starts at the 97th byte of
the file
infile = open("mysimcityfile.sc4", "rb")
infile.seek(97)
print (infile.read(4))
output:
flamesrock at flames:~/score$ python sc4png.py
PNG
(for 5 bytes it outputs an extra line, and for 6- 
(the biology symbol for a male)) So to debug further, I took out the
exception and modified the code like this:

#import struct
#
#def pngcopy(infile, outfile):
#
#   # copy header
#   header = infile.read(4)
#   #if header != "\211PNG\r\n\032\n":
#   #    raise IOError("not a valid PNG file")
#   outfile.write(header)
#
#   # copy chunks, until IEND
#   while 1:
#       chunk = infile.read(8)
#       size, cid = struct.unpack("!l4s", chunk)
#       outfile.write(chunk)
#       outfile.write(infile.read(size))
#       outfile.write(infile.read(4)) # checksum
#       if cid == "IEND":
#           break
#
#
#infile = open("mysimcityfile.sc4", "rb")
#infile.seek(97)
#outfile = open("myimage.png", "wb")
#pngcopy(infile, outfile)
#outfile.close()
#infile.close()

returning the following output:
flamesrock at flames:~/score$ python sc4png.py
Traceback (most recent call last):
File "sc4png.py", line 26, in ?
pngcopy(infile, outfile)
File "sc4png.py", line 14, in pngcopy
size, cid = struct.unpack("!l4s", chunk)
struct.error: unpack str size does not match format

Any ideas on how to fix it? If I understand this page correctly,
http://www.python.org/doc/current/lib/module-struct.html
a png is basically a 'big endian string of 14 chars'? Changing it to
!14b" gives a"ValueError: unpack tuple of wrong size"
-thanks in advance for any help




More information about the Python-list mailing list