Skip to content

Instantly share code, notes, and snippets.

@chrisamow
Forked from will-hart/README.md
Last active April 25, 2021 10:26
Show Gist options
  • Save chrisamow/ac5b82c7acb7f5581c7a980a401a7e49 to your computer and use it in GitHub Desktop.
Save chrisamow/ac5b82c7acb7f5581c7a980a401a7e49 to your computer and use it in GitHub Desktop.
Stitch together tilemaps into a single image

Why?

This is a simple Python / PIL utility for taking a series of images in a tile map set and stitching them together into a single image. This is being used to convert these http://forums.bistudio.com/showthread.php?178671-Tiled-maps-Google-maps-compatible-(WIP) for www.anvilproject.com.

How?

  1. Drop the stitcher.py file into the root directory of your tile map set, where all the numbered folders are
  2. Edit two lines in the file, these are commented - one for the number of folders and one for the number of images in each folder
  3. Run the script python stitcher.py
  4. Your output will eventually appear in output.png
# -*- coding: utf-8 -*-
"""
A simple script which converts all the images in the folder it is run from
into a single image. Images should be in "tile map set" folder format with
0-based numbered folders and images running from 0 - n, named as e.g. "0.png"
Used for http://forums.bistudio.com/showthread.php?178671-Tiled-maps-Google-maps-compatible-(WIP)
License: public domain
"""
from os import sep
from PIL import Image
# zero start was assumed previously;;
TILEOFFSET = 431365
FOLDEROFFSET = 238907
class ImageDeTiler(object):
##
### Change the following numbers to match your tile set
##
NUMBER_OF_FOLDERS = 19 #32
NUMBER_OF_IMAGES_PER_FOLDER = 17 #30 # this is actually num-1
##
###
##
folders = [str(x+FOLDEROFFSET) for x in range(0, NUMBER_OF_FOLDERS + 1)]
imageIds = [str(x+TILEOFFSET) for x in range(NUMBER_OF_IMAGES_PER_FOLDER, -1, -1)]
# dimensions of a single tile, change if required
tileDim = 256
finalWidth = tileDim * len(folders)
finalHeight = tileDim * len(folders)
def __init__(self):
"""
Automatically traverses the directory the script is run from
and tiles all the images together into a massive super image
"""
print "----------------------------------------------"
print " Preparing output image, %s by %s" % (
self.finalWidth, self.finalHeight
)
print "----------------------------------------------"
result = Image.new("RGBA", (self.finalWidth, self.finalHeight))
for i, x in enumerate(self.folders):
print "----------------------------------------------"
print " Processing row " + str(i)
print "----------------------------------------------"
row = self.get_tile(x)
result.paste(row, (i * self.tileDim, 0))
result.save("output.png")
print "----------------------------------------------\n" * 2
def get_tile(self, path):
"""
Takes a path and returns an image which contains all the tiled images
"""
result = Image.new("RGBA", (self.tileDim, self.finalHeight))
rows = len(self.imageIds)
for i, x in enumerate(self.imageIds):
newPath = path + sep + x + ".png"
img = Image.open(newPath)
x, y= img.size
print "%s: %s, %s x %s" % (newPath, img.mode, x, y)
# the order needed to be reversed, so subtract from num rows
result.paste(img, (0, (rows-i) * self.tileDim))
return result
if __name__ == "__main__":
ImageDeTiler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment