Looking at your data, the header is at offset 0x60, not 0x50. If I use
  infile.seek(0x60)
it works fine.

Kent

Aaron Elbaz wrote:
Hi,

My question is sort of on the difficult side, but I promise I'm a newb
;) So maybe it isn't..


Frederick Lundh himself gave me this chunk of code..and I can't get it to work. I was hoping someone could spot the error.

The goal is to extract a png image file from a binary simcity 4 file.
#import struct
#
#def pngcopy(infile, outfile):
#
#   # copy header
#   header = infile.read(8)
#   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("/home/flamesrock/Desktop/peachville.sc4", "rb")
#infile.seek(0x50)
#outfile = open("myimage.png", "wb")
#pngcopy(infile, outfile)
#outfile.close()
#infile.close()

It returns the IO error when I try it on this and other files:
http://simcitysphere.com/peachville.sc4

I've confirmed the position using a hex editor (its the first 'P' or
0x50 in the file)
http://simcitysphere.com/peachville.png

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to