Skip to content

Instantly share code, notes, and snippets.

@unti1x
Created October 8, 2015 10:58
Show Gist options
  • Save unti1x/cc4168b62141b190d835 to your computer and use it in GitHub Desktop.
Save unti1x/cc4168b62141b190d835 to your computer and use it in GitHub Desktop.
Split sprite to separate images
#!/usr/bin/env python
import Image
import argparse
import os
def main(filename, width, height, skip_width, skip_height):
cur_x = 0
cur_y = 0
outfile, outext = os.path.splitext(filename)
sprite = Image.open(filename)
img_width, img_height = sprite.size
tile_region = (0, 0, width, height)
i = 0
while cur_y + height <= img_height:
cur_x = 0
while cur_x + width <= img_width:
i+=1
tile_output = outfile + '-' + str(i) + outext
tile = Image.new('RGBA', (width, height))
box = (cur_x, cur_y, cur_x + width, cur_y + height)
region = sprite.crop(box)
tile.paste(region, tile_region)
tile.save(tile_output)
cur_x += width + skip_width
cur_y += height + skip_height
print "%d tile(s) successfully extracted" % (i)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Split sprite to separate images",
version="0.1"
)
parser.add_argument('filename', help="input file")
parser.add_argument('width', help="width of tile", type=int)
parser.add_argument('height', help="height of tile", type=int)
parser.add_argument('skip_width', nargs='?', type=int, default=0,
help="horizontal space between tiles (default: %(default)s)")
parser.add_argument('skip_height', nargs='?', type=int, default=0,
help="vertical space between tiles (default: %(default)s)")
args = parser.parse_args()
main(args.filename, args.width, args.height, args.skip_width, args.skip_height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment