Skip to content

Instantly share code, notes, and snippets.

@zeraien
Created April 26, 2012 21:59
Show Gist options
  • Save zeraien/2503530 to your computer and use it in GitHub Desktop.
Save zeraien/2503530 to your computer and use it in GitHub Desktop.
Python script that will convert a given image into tiles.
import Image
import sys
image = Image.open(sys.argv[1])
tile_width = int(sys.argv[2])
tile_height = int(sys.argv[3])
zoom_level = sys.argv[4]
if image.size[0] % tile_width == 0 and image.size[1] % tile_height ==0 :
currentx = 0
currenty = 0
while currenty < image.size[1]:
while currentx < image.size[0]:
print currentx,",",currenty
tile = image.crop((currentx,currenty,currentx + tile_width,currenty + tile_height))
tile.save("x" + str(currentx) + "y" + str(currenty) + "z" + zoom_level + ".png","PNG")
currentx += tile_width
currenty += tile_height
currentx = 0
else:
print "sorry your image does not fit neatly into",tile_width,"*",tile_height,"tiles"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment