Skip to content

Instantly share code, notes, and snippets.

@patrickgh3
Created September 9, 2014 00:29
Show Gist options
  • Save patrickgh3/6a0c0c9874ff45649e43 to your computer and use it in GitHub Desktop.
Save patrickgh3/6a0c0c9874ff45649e43 to your computer and use it in GitHub Desktop.
Simple tileset generator that was useful and good practice to write.
# generate dem tiles
# author: Patrick Traynor 9/8/14
# Generates a tileset image from a source image of the solid, edge, outside
# corner, and inside corner sub-tiles (mini or corner tiles). Sub-tiles are
# rotated, as opposed to supplying variations of each sub-tile for each
# orientation.
# I hope somebody finds use of this script or draws some inspiration from it
# or something. It was useful to me, so I figured I may as well share it.
import sys
from PIL import Image
usage = "Usage: python " + sys.argv[0] + " sourceimage destimage\n + " + """
Image names are relative to current directory and do not include extension.
Source image must be a PNG where width = 4 * height.
__________________________________
| --------r-------@ |
| solid edge |outside inside |
| |corner corner |
|________________|_______________|
[ ][ ][ ][ ] """
if len(sys.argv) != 3:
print(usage)
exit(1)
try:
sourceImage = Image.open(sys.argv[1] + ".png")
except:
print("No file named " + sys.argv[1] + ".png.")
exit(1)
if sourceImage.size[0] != sourceImage.size[1] * 4:
print("Source image must have dimensions such that witdh = 4 * height")
exit(1)
# crop a tile
def c(im, x, y):
return im.crop((x * ts, y * ts, (x + 1) * ts, (y + 1) * ts))
# paste a tile
def p(im, x, y):
output.paste(im, (x * ts, y * ts))
# paste 4 tiles in a square
def pt(imul, imur, imbl, imbr, x, y):
p(imul, x, y)
p(imur, x + 1, y)
p(imbl, x, y + 1)
p(imbr, x + 1, y + 1)
# rotate and paste 4 tiles in a square at the end of the current row
def ptcr(imul, rul, imur, rur, imbl, rbl, imbr, rbr):
global currentx
pt(imul.rotate(rul), imur.rotate(rur), imbl.rotate(rbl), imbr.rotate(rbr),
currentx, currenty)
currentx += 2
# start pasting in a new row
def newline():
global currentx, currenty
currenty += 2
currentx = 0
ts = sourceImage.size[1]
output = Image.new("RGBA", (8 * ts, 10 * ts))
solid = c(sourceImage, 0, 0)
edge = c(sourceImage, 1, 0)
outcorner = c(sourceImage, 2, 0)
incorner = c(sourceImage, 3, 0)
up = 0
left = 90
down = 180
right = 270
currentx = 0
currenty = 0
ptcr(outcorner, up, edge, up, edge, left, solid, up)
ptcr(edge, up, edge, up, solid, up, solid, up)
ptcr(edge, up, outcorner, right, solid, up, edge, right)
ptcr(outcorner, up, outcorner, right, edge, left, edge, right)
newline()
ptcr(edge, left, solid, up, edge, left, solid, up)
ptcr(solid, up, solid, up, solid, up, solid, up)
ptcr(solid, up, edge, right, solid, up, edge, right)
ptcr(edge, left, edge, right, edge, left, edge, right)
newline()
ptcr(edge, left, solid, up, outcorner, left, edge, down)
ptcr(solid, up, solid, up, edge, down, edge, down)
ptcr(solid, up, edge, right, edge, down, outcorner, down)
ptcr(edge, left, edge, right, outcorner, left, outcorner, down)
newline()
ptcr(outcorner, up, edge, up, outcorner, left, edge, down)
ptcr(edge, up, edge, up, edge, down, edge, down)
ptcr(edge, up, outcorner, right, edge, down, outcorner, down)
ptcr(outcorner, up, outcorner, right, outcorner, left, outcorner, down)
newline()
ptcr(incorner, up, solid, up, solid, up, solid, up)
ptcr(solid, up, incorner, right, solid, up, solid, up)
ptcr(solid, up, solid, up, incorner, left, solid, up)
ptcr(solid, up, solid, up, solid, up, incorner, down)
try:
output.save(sys.argv[2] + ".png")
except:
print("Unable to write file named " + sys.argv[2] + ".png.")
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment