Skip to content

Instantly share code, notes, and snippets.

@iskolbin
Created March 2, 2018 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iskolbin/0bcdff4c4546f6ab003556aeae69649d to your computer and use it in GitHub Desktop.
Save iskolbin/0bcdff4c4546f6ab003556aeae69649d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from PIL import Image
import os
import sys
from xml.dom import minidom
def main():
if len( sys.argv ) < 2:
print( 'needed argument: path to image and xml' )
print( 'like ./AtlasSplit.py images/atlas' )
print( 'if atlas.png and atlas.xml in the "images" folder' )
return False
im = Image.open( sys.argv[1] + '.png' )
for sxml in minidom.parse( sys.argv[1] + '.xml' ).getElementsByTagName('SubTexture'):
s = { 'name': sxml.getAttribute('name'), 'x': sxml.getAttribute('x'), 'y': sxml.getAttribute('y'), 'width': sxml.getAttribute('width'), 'height': sxml.getAttribute('height')}
x, y, width, height = int( sxml.getAttribute('x')), int( sxml.getAttribute('y')), int( sxml.getAttribute('width')), int( sxml.getAttribute('height'))
name = sxml.getAttribute('name')
subim = Image.new( 'RGBA', ( width, height ), (0,0,0,0) )
subim.paste( im.crop(( x, y, x+width, y+height )))
subim.save( '%s.png' % name )
print 'Success'
return True
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment