Here's a little script I wrote to extract a whole lot of .png
files from a compiled sprite sheet.
The python script reads the .bin
file as binary, finds the starting header of a .png
file (89504E47
) and the footer (49454E44AE426082
) and separates it into individual images.
This may not be the worlds best or most useful script, but it saved me several hours of copy and paste.
import binascii
import re
import os
for directory, subdirectories, files in os.walk('.'):
for file in files:
if not file.endswith('.bin'):
continue
filenumber = 0
with open(os.path.join(directory, file)) as f:
hexaPattern = re.compile(
r'(89504E47.*?49454E44AE426082)',
re.IGNORECASE
)
for match in hexaPattern.findall(binascii.hexlify(f.read())):
with open('{}-{}.png'.format(file, filenumber), 'wb+') as f:
f.write(binascii.unhexlify(match))
filenumber += 1