有些时候需要把 AGS 切图生成的缓存合并成一张大图。主要目的一般是作为展示或者是 PPT 里面的图片,所以不要求带坐标。用到 PIL 库。
# -*- coding: cp936 -*- import Image, os, re path = r'J:/工作数据/201101/缓存/Layers' # 缓存路径,这个目录下应该有 conf.xml 文件 level = 'L03' # 想要合并的级别 big_width = 0 big_height = 0 conf = open(os.path.join(path, 'conf.xml')).read() width_pixel = int(re.search('<TileRows>(.*)</TileRows>', conf).group(1)) height_pixel = int(re.search('<TileRows>(.*)</TileRows>', conf).group(1)) format = re.search('<CacheTileFormat>(.*)</CacheTileFormat>', conf).group(1) image_path = os.path.join(path, '_alllayers//' + level) for root, dirs, files in os.walk(image_path): if not dirs: if len(files) > big_width: big_width = len(files) big_height += 1 big_image = Image.new('RGB', (width_pixel * big_width, height_pixel * big_height)) stepY = 0 for root, dirs, files in os.walk(image_path): if not dirs: stepX = 0 for name in files: small_image = Image.open(os.path.join(root, name)) big_image.paste(small_image, (0 + stepX, 0 + stepY, width_pixel + stepX, height_pixel + stepY)) stepX += width_pixel stepY += height_pixel big_image.save(os.path.join(path, level + '.' + format))