December 30th, 2004, 07:51 PM
Extracting PNG from Binary @ 0x50
Frederick Lundh himself gave me this chunk of code..and I can't get it to work. I was hoping one of you could spot the error.
The goal is to extract a png image file from a binary simcity 4 file.
Screenshot of the hex editor in action:
def pngcopy(infile, outfile):
# copy header
header = infile.read(8)
if header != "\211PNG\r\n\032\n":
raise IOError("not a valid PNG file")
# copy chunks, until IEND
chunk = infile.read(8)
size, cid = struct.unpack("!l4s", chunk)
outfile.write(infile.read(4)) # checksum
if cid == "IEND":
infile = open("/home/flamesrock/Desktop/peachville.sc4", "rb")
outfile = open("myimage.png", "wb")
The simcity 4 binary file(for anyone who wants to try extracting):
Any ideas on why this isn't working? It makes perfect sense to me, even as a relative newb, but it doesn't work?
-thanks in advance
December 30th, 2004, 08:22 PM
- 0x50 isn't the location in the file, it's the hexadecimal representation of the ASCII code for the uppercase character "P", so the seek() call wont be going to the right place...
Second, your screenshot shows "...PNG" (0000 8950 4E47 0D0A...), but the function is looking for "\211PNG\r\n..." (02 3131 504E47 0D0A...) which is, I think, why it immediately throws the IOException.
As for what to do about it... I don't know.
January 3rd, 2005, 05:40 AM
Mucho gracias and sorry for the late reply.
Shouldn't seek() goto the PNG since it contains the first P in the binary file?
Ahhhh...good eye. I'm starting to think there might be some sort of compression...
I'll post back as soon as I find a solution to this..if ever.
(observations or pointers still welcome!)
January 3rd, 2005, 08:20 AM
No - it seeks for the file position you specify, e.g. move 8 bytes backwards or move 20 bytes from the start of the file, it doesn't look at the content at all.