Skip to content

Instantly share code, notes, and snippets.

@liu7yong
Created February 15, 2014 16:07
Show Gist options
  • Save liu7yong/9021372 to your computer and use it in GitHub Desktop.
Save liu7yong/9021372 to your computer and use it in GitHub Desktop.
unpack png from atlas images (such as flappy bird)
#/usr/local/bin/python
import os, sys, math
from PIL import Image
def config_to_dict(atlas_entry):
d = {}
list = atlas_entry.split(" ")
d["frameName"] = list[0]
d["width"] = int(list[1])
d["height"] = int(list[2])
d["x"] = math.floor(1024 * float(list[3]) + 0.1)
d["y"] = math.floor(1024 * float(list[4]) + 0.1)
return d
def gen_png_from_atlas(config_filename, png_filename):
file_path = png_filename.replace('.png', '')
big_image = Image.open(png_filename)
config_file = open(config_filename, 'r')
for line in config_file:
config_dict = config_to_dict(line)
box = (
int(config_dict["x"]),
int(config_dict["y"]),
int(config_dict["x"] + config_dict["width"]),
int(config_dict["y"] + config_dict["height"]),
)
rect_on_big = big_image.crop(box)
result_size = [config_dict["width"], config_dict["height"]]
result_box = (0, 0, config_dict["width"], config_dict["height"])
result_image = Image.new('RGBA', result_size, (0, 0, 0, 0))
result_image.paste(rect_on_big, result_box, mask=0)
if not os.path.isdir(file_path):
os.mkdir(file_path)
outfile = file_path + '/' + config_dict['frameName'] + '.png'
print outfile, "generated"
result_image.save(outfile)
if __name__ == '__main__':
filename = sys.argv[1]
config_filename = filename + ".txt"
png_filename = filename + ".png"
if (os.path.exists(config_filename) and os.path.exists(png_filename)):
gen_png_from_atlas(config_filename, png_filename)
else:
print "make sure you have both atlas config file and atlas png files in the same directory"
@moribito
Copy link

Thanks for this tool but you should have said that usage is >unpack_atlas.py atlas
Also in some game the picture size is 2048, I edited that and it worked like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment