Commits

Michal Gron  committed dc82d5f

initial commit

  • Participants

Comments (0)

Files changed (4)

+.idea/
+*.iml
+*/*.iml
+out
+bin
+assets/ui_2048x1536-hd.*

File assets/assets.png

Added
New image

File assets/assets.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Created with TexturePacker http://www.codeandweb.com/texturepacker-->
+<!-- $TexturePacker:SmartUpdate:7a0e9d0cf12e0dea9da61a80d77db070:1/1$ -->
+<TextureAtlas imagePath="assets.png">
+    <SubTexture name="g100" x="54" y="2" width="48" height="40"/>
+    <SubTexture name="g101" x="2" y="2" width="50" height="58" frameX="0" frameY="-4" frameWidth="50" frameHeight="58"/>
+    <SubTexture name="g102" x="2" y="94" width="48" height="32" frameX="0" frameY="9" frameWidth="48" frameHeight="32"/>
+    <SubTexture name="g103" x="52" y="94" width="48" height="30" frameX="0" frameY="1" frameWidth="48" frameHeight="30"/>
+    <SubTexture name="g104" x="54" y="44" width="48" height="36" frameX="0" frameY="5" frameWidth="48" frameHeight="36"/>
+    <SubTexture name="g105" x="2" y="62" width="50" height="30" frameX="0" frameY="2" frameWidth="50" frameHeight="30"/>
+</TextureAtlas>

File decompose.py

+from __future__ import print_function
+import os
+from PIL import Image
+from xml.dom import minidom
+
+__author__ = 'mgron'
+
+
+class Decomposer():
+
+    def __init__(self, png, xml):
+        self.png = png
+        self.xml = xml
+
+    def decompose(self, output):
+        im = Image.open(self.png)
+        print(im.format, im.size, im.mode)
+
+        xmldoc = minidom.parse(self.xml)
+        subtextures = xmldoc.getElementsByTagName('SubTexture')
+        print('Found', len(subtextures), 'subtextures')
+
+        for subtexture in subtextures:
+            tex_rect = self.read_rectangle(subtexture)
+            tex_name = self.read_name(subtexture)
+
+            sprite = im.crop(tex_rect)
+            self.save(sprite, tex_name, output)
+
+    def read_rectangle(self, node):
+        x = int(node.attributes['x'].value)
+        y = int(node.attributes['y'].value)
+        w = int(node.attributes['width'].value)
+        h = int(node.attributes['height'].value)
+
+        return x, y, (x + w), (y + h)
+
+    def read_name(self, node):
+        return node.attributes['name'].value
+
+    def save(self, sprite, name, folder):
+        path = folder + os.sep + name + '.png'
+
+        if not os.path.exists(folder):
+            os.makedirs(folder)
+
+        try:
+            sprite.save(path)
+        except IOError:
+            print('cannot convert', path)
+
+if __name__ == '__main__':
+    png = 'assets/assets.png'
+    xml = 'assets/assets.xml'
+    decomposer = Decomposer(png, xml)
+    decomposer.decompose('bin')