source: trunk/PARPG/utilities/gfxsplit.py @ 67

Revision 67, 4.7 KB checked in by maximinus_parpg, 11 years ago (diff)

All code now PEP-8 standard, with spaces

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>
17
18import sys,pygame
19
20# place defines here
21
22TILE_WIDTH  =   72
23
24# this is very much a simple routine, but we still have a simple class
25
26class TileImage:
27    def __init__(self, picture, name):
28        self.image = picture
29        self.filename = name
30
31def writeXML(name):
32    """Write the XML file as well
33       Always the same small file so we do it automatically"""
34    # we need to strip off the entire path up to the last
35    # TODO: this code will not work on windows
36    # strip off the png part and replace with the XML
37    filename = name.split('/')[-1]
38    x_file = open(name[:-4]+".xml","wt")
39    x_file.write('''<?fife type="object"?>\n''')
40    x_file.write('''<object id="''')
41    x_file.write(filename[:-4])
42    x_file.write('''" namespace="PARPG" blocking="1" static="1">\n''')
43    x_file.write('''    <image source="''')
44    x_file.write(filename)
45    x_file.write('''" direction="0" />\n''')
46    # the \n\n is ESSENTIAL otherwise the XML parser in FIFE craps out!
47    x_file.write('''</object>\n\n''')
48    x_file.close
49
50def saveFiles(files):
51    """Given a list of TileImages, output them as seperate files
52       Returns True if it worked"""
53    # files is a list of TileImages
54    complete = []
55    for i in files:
56        try:
57            pygame.image.save(i.image, i.filename)
58            # output the XML file as well
59            writeXML(i.filename)
60        except:
61            print "Error: Failed to save",filename
62            # if we saved some anyway, then tell the user
63            if(complete != []):
64                print "  Managed to save",
65                for name in complete:
66                    print name,
67                print "\n"
68            return False
69        complete.append(i.filename)
70    # seems like all was ok
71    return True
72           
73def splitImage(image, filename):
74    """Quite complex this, as there are many differing layouts on the
75       hexes that we could be dealing with. However, for now we assume
76       that we blit from left to right, with the image x position increasing
77       by one and the y value staying the same (on the grid map)"""
78    xpos = 0
79    file_counter = 0
80    tiles = []
81    height = image.get_height()
82    while(xpos<image.get_width()):
83        # create a new surface the same height as the original but
84        # with a width of TILE_WIDTH, and with per-pixel alpha
85        new_surface = pygame.Surface((TILE_WIDTH, height),pygame.SRCALPHA,32)
86        # now blit a strip of the image across
87        if(xpos == 0):
88            new_surface.blit(image,(0,0),
89                             pygame.Rect(0,0, TILE_WIDTH, height))
90            # on the first time around, move ahead by the width of a tile
91            xpos += TILE_WIDTH
92        else:
93            # we need to offset into halfway through the tile on other blits
94            new_surface.blit(image, ((TILE_WIDTH/2)-1, 0),
95                pygame.Rect(xpos, 0, TILE_WIDTH/2, height))
96            xpos += (TILE_WIDTH/2)
97        # store the image for later
98        tiles.append(TileImage(new_surface,
99            filename + chr(ord('a')+file_counter) + ".png"))
100        file_counter += 1
101    return tiles
102           
103def convertFiles(filename):
104    """Take a file, slice into seperate images and then save these new images
105       as filename0, filename1 ... filenameN
106       Returns True if everything worked"""
107    # first off, load the file
108    try:
109        image=pygame.image.load(filename)
110    except(pygame.error):
111        print "Error: Couldn't load",filename
112        return False
113    # split into seperate files
114    # the [:-4] is used to split off the .png from the filename
115    images=splitImage(image,filename[:-4])
116    # save it and we are done
117    if(images == []):
118        # something funny happened
119        print "Error: Couldn't splice given image file"
120        return False
121    return(saveFiles(images))
122
123if __name__=="__main__":
124    # check we have some options
125    if(len(sys.argv) < 2):
126        sys.stderr.write("Error: No image given!\n")
127        sys.exit(False)
128    # ok, so init pygame and do it
129    pygame.init()
130    sys.exit(convertFiles(sys.argv[1]))
131
Note: See TracBrowser for help on using the repository browser.