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

Revision 47, 3.8 KB checked in by maximinus_parpg, 11 years ago (diff)

Added code to split gfx into seperate tiles.
New code /utilities/gfxsplit.py

  • 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  =   70
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 SaveFiles(files):
32    """Given a list of TileImages, output them as seperate files
33       Returns True if it worked"""
34    # files is a list of TileImages
35    complete=[]
36    for i in files:
37        try:
38            pygame.image.save(i.image,i.filename)
39        except:
40            print "Error: Failed to save",filename
41            # if we saved some anyway, then tell the user
42            if(complete!=[]):
43                print "  Managed to save",
44                for name in complete:
45                    print name,
46                print "\n"
47            return False
48        complete.append(i.filename)
49    # seems like all was ok
50    return True
51           
52def SplitImage(image,filename):
53    """Quite complex this, as there are many differing layouts on the
54       hexes that we could be dealing with. However, for now we assume
55       that we blit from left to right, with the image x position increasing
56       by one and the y value staying the same (on the grid map)"""
57    xpos=0
58    file_counter=0
59    tiles=[]
60    height=image.get_height()
61    while(xpos<image.get_width()):
62        # create a new surface the same height as the original but
63        # with a width of TILE_WIDTH, and with per-pixel alpha
64        new_surface=pygame.Surface((TILE_WIDTH,height),pygame.SRCALPHA,32)
65        # now blit a strip of the image across
66        if(xpos==0):
67            new_surface.blit(image,(0,0),pygame.Rect(0,0,TILE_WIDTH,height))
68            # on the first time around, move ahead by the width of a tile
69            xpos+=TILE_WIDTH
70        else:
71            # we need to offset into halfway through the tile on other blits
72            new_surface.blit(image,((TILE_WIDTH/2)-1,0),
73                pygame.Rect(xpos,0,TILE_WIDTH/2,height))
74            xpos+=(TILE_WIDTH/2)
75        # store the image for later
76        tiles.append(TileImage(new_surface,filename+str(file_counter)+".png"))
77        file_counter+=1
78    return tiles
79           
80def ConvertFiles(filename):
81    """Take a file, slice into seperate images and then save these new images
82       as filename0, filename1 ... filenameN
83       Returns True if everything worked"""
84    # first off, load the file
85    try:
86        image=pygame.image.load(filename)
87    except(pygame.error):
88        print "Error: Couldn't load",filename
89        return False
90    # split into seperate files
91    # the [:-4] is used to split off the .png from the filename
92    images=SplitImage(image,filename[:-4])
93    # save it and we are done
94    if(images==[]):
95        # something funny happened
96        print "Error: Couldn't splice given image file"
97        return False
98    return(SaveFiles(images))
99
100if __name__=="__main__":
101    # check we have some options
102    if(len(sys.argv)<2):
103        sys.stderr.write("Error: No image given!\n")
104        sys.exit(False)
105    # ok, so init pygame and do it
106    pygame.init()
107    sys.exit(ConvertFiles(sys.argv[1]))
108
Note: See TracBrowser for help on using the repository browser.