Skip to content

Instantly share code, notes, and snippets.

@will-hart
Last active March 22, 2024 04:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save will-hart/133814e92cf45745e9d1 to your computer and use it in GitHub Desktop.
Save will-hart/133814e92cf45745e9d1 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 PIL import Image
class ImageDeTiler(object):
##
### Change the following numbers to match your tile set
##
NUMBER_OF_FOLDERS = 32
NUMBER_OF_IMAGES_PER_FOLDER = 30
##
###
##
folders = [str(x) for x in range(0, NUMBER_OF_FOLDERS + 1)]
imageIds = [str(x) 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))
for i, x in enumerate(self.imageIds):
newPath = path + "\\" + x + ".png"
img = Image.open(newPath)
x, y= img.size
print "%s: %s, %s x %s" % (newPath, img.mode, x, y)
result.paste(img, (0, i * self.tileDim))
return result
if __name__ == "__main__":
ImageDeTiler()
@tigerhawkvok
Copy link

Thanks for this baseline; I needed this today to get me going.

I pretty heavily modified this and made it Python 3 compatible, if you need it: https://gist.github.com/tigerhawkvok/ff2f27c1e098fecba5702bc9782abf01

Most notably, it no longer needs any manual file configuration.

@frozenpandaman
Copy link

@tigerhawkvok Thank you for this!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment