Skip to content

Instantly share code, notes, and snippets.

@THeK3nger
Created February 8, 2016 21:12
Show Gist options
  • Save THeK3nger/f79e03f5c55f7129a69d to your computer and use it in GitHub Desktop.
Save THeK3nger/f79e03f5c55f7129a69d to your computer and use it in GitHub Desktop.
Convert an image into a MovingAI .map file.
# Convert an image into a MovingAI .map file.
# @author Davide Aversa <thek3nger@gmail.com>
# @version 1.0.0
import sys
from PIL import Image
def load_image(filename):
"""
Load an image file.
@param filename The image filename
@return an Image object.
"""
return Image.open(filename)
def color_to_char(color):
"""
Map each pixel color to a particular map tile.
"""
if color == (0, 0, 0):
return "@"
if color == (255, 255, 255):
return "."
if color == (0, 255, 0):
return "T"
return "@"
def img2movingai(filename, output=None):
"""
The main algorithm.
"""
if output is None:
output = filename + ".map"
img = load_image(filename)
img_matrix = img.load()
width, height = img.size
with open(output, 'w') as f:
f.write("type octile\n")
f.write("height " + str(height) + "\n")
f.write("width " + str(width) + "\n")
f.write("map\n")
for y in range(height):
line = ""
for x in range(width):
line += color_to_char(img_matrix[x,y])
line += "\n"
f.write(line)
if __name__ == '__main__':
filename = sys.argv[1]
img2movingai(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment