Commits

Anton Shestakov committed 0e9c057

Initial commit.

Comments (0)

Files changed (1)

+#!/usr/bin/env python2
+# -*- coding: UTF-8 -*-
+import os.path
+from subprocess import Popen, PIPE
+from optparse import OptionParser
+
+
+def main():
+    parser = OptionParser(
+        usage='''Generate sprite image and ready-to-use CSS from separate images.
+
+See that 'ready-to-use' remark I used for CSS only? That's because you still
+have to optimize the image yourself with your favorite tool (optipng, 
+for instance).
+        
+Usage: %prog [OPTIONS] [SRC]... [DEST]''')
+    
+    parser.add_option('--geometry', dest='geometry', metavar='WxH', default='16x16',
+                help='tile size in resulting sprite (see montage -geometry option)')
+                
+    parser.add_option('--base-class', dest='baseclass', metavar='CLASS', default='sprite-icon',
+                help='general sprite selector name for resulting CSS '
+                    '(contains all background properties except background-position: '
+                    'it will be in sub-selector for specific tile)')
+                
+    params, args = parser.parse_args()
+    
+    Popen(['montage', '-geometry', params.geometry, '-tile', '1x', 
+        '-background', 'none'] + args, stdout=PIPE).communicate()[0]
+    
+    tile_width, tile_height = params.geometry.split('x')
+    tile_width = int(tile_width)
+    tile_height = int(tile_height)
+    
+    print(
+'''.%(baseclass)s {
+   width: %(width)ipx;
+   height: %(height)ipx;
+   background-color: transparent;
+   background-repeat: no-repeat;
+   background-position: 0 -10000px;
+   background-image: url('%(sprite)s');
+}''' % {
+        'baseclass': params.baseclass, 
+        'width': tile_width, 
+        'height': tile_height, 
+        'sprite': args[-1]
+    })
+
+    for index, arg in enumerate(args[:-1]):
+        print(
+'''.%(tileclass)s {
+    background-position: %(posx)ipx %(posy)ipx;
+}''' % {
+        'tileclass': os.path.basename(arg).partition('-')[2].rpartition('.')[0],
+        'posx': 0,
+        'posy': -index * tile_height
+    })
+
+
+if __name__ == '__main__':
+    main()