Python Script for Generating Tile Data
About
This is a python script that converts a picture file to a C tiles file. Command-line arguments specify the width and height of the image in tiles, as well as the tile width and height in pixels. It's not perfect, but it allows use Gimp/Photoshop for image editing. The PIL library also supports the majority of image types, so (in theory) it will support any of the image types supported by that library. This utility, however, has only been tested with PNG.
Prequisites
- Python 2.6 (will probably run under 2.4 or 2.5 if you must)
- Python Imaging Library
Use
The tool provides it's own online help. Simply run it with no arguments or the --help command:
Uzebox tile dataset generator V2 - creates source from image data (c) 2009 Eric Anderton Usage: convertgfx [options] IMAGEFILE -x --width <value> Width of each tile in the image (default 8) -y --height <value> Height of each tile in the image (default 8) -c --columns <value> Number of tiles to gather in a row -r --rows <values> Number of tile rows to gather -i --ident <identifier> Identifier of the data set on output -w --wide Output in wide format -l --lang c|asm Output in C format or AVR8 asm, respectively -p --pack Bit-pack monochrome font -h --help Show this message
Updates in this revision include an experimental 'monochrome bitpacked mode', and the ability to target AVR8 assembler as well as C for output. The assumptions of 6 and 8 pixel wide tile images has also been discarded in favor of explicit height and width values. Bounds checking has also been added for all of the size parameters. Code quality has also been dramatically reduced for your reading pleasure.
Example
First, make an image the contains your tile data. Uze's tileset sources in trunk/gfx is a great example of what things should look like. Then run it through the program:
python convertgfx.py -x 6 fonts.png python convertgfx.py -x 8 fonts_8x8.png
These will create "fonts.c" and "fonts_8x8.c" respectively.
convertgfx.py
Be sure to observe the prerequisites above.
"""
Copyright (c) 2009 Eric Anderton
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
from PIL import Image
import getopt
import sys
import os
helptext ="""
Uzebox tile dataset generator V2 - creates source from image data
(c) 2009 Eric Anderton
Usage: convertgfx [options] IMAGEFILE
-x --width <value> Width of each tile in the image (default 8)
-y --height <value> Height of each tile in the image (default 8)
-c --columns <value> Number of tiles to gather in a row
-r --rows <values> Number of tile rows to gather
-i --ident <identifier> Identifier of the data set on output
-w --wide Output in wide format
-l --lang c|asm Output in C format or AVR8 asm, respectively
-p --pack Bit-pack monochrome font
-h --help Show this message
"""
width = 8
height = 8
columns = 0
rows = 0
wideFormat = False
language = "c"
ident = None
bitpack = False
(optlist,args) = getopt.getopt(sys.argv[1:],"hwc:r:i:x:y:l:p",["help","wide","columns","rows","ident","width","height","lang","pack"])
for (opt,value) in optlist:
if opt == "-x" or opt == "--width":
width = int(value)
elif opt == "-y" or opt == "--height":
height = int(value)
elif opt == "-w" or opt == "--wide":
wideFormat = True
elif opt == "-c" or opt =="--columns":
columns = int(value)
elif opt == "-r" or opt == "--rows":
rows = int(value)
elif opt == "-i" or opt == "--ident":
ident = int(value)
elif opt == "-l" or opt == "--lang":
language = value
elif opt == "-p" or opt == "--pack":
bitpack = True
elif opt == "-h" or opt == "--help":
print helptext
exit(1)
if len(args) == 0:
(filedir,name) = os.path.split(sys.argv[0])
print "Error - No image specified."
print "Try `%s --help` for more information." % name
exit(1)
filename = args[0]
(filedir,name) = os.path.split(filename)
(base,ext) = os.path.splitext(name)
if ident == None:
ident = base
image = Image.open(filename).convert("RGB")
data = image.load()
#value checking
if language not in ["c","asm"]:
print "Error - unknown output language '%s'" % (language)
exit(1)
# bounds checking
(imgwidth,imgheight) = image.size
if columns > 0:
if width * columns > imgwidth:
print "Error - image is too narrow to contain the specified number of columns."
exit(1)
else:
columns = int(imgwidth / width)
if rows > 0:
if height * rows > imgheight:
print "Error - image is too narrow to contain the specified number of columns."
exit(1)
else:
rows = int(imgheight / height)
if bitpack:
print "Performing monochrome conversion"
image = image.convert("1")
print "Generating tile data: %d by %d tiles, at %d by %d pixels each." % (rows,columns,height,width)
# output stage
if language == "c":
outfile = open(filedir+base+".c","wt+")
outfile.write("/* Autogenerated by convertgfx.py - do not modify */\n")
outfile.write("/* %s - %d tiles each %d by %d pixels */\n" % (filename,rows*columns,height,width))
outfile.write("#include <avr/pgmspace.h>\n")
outfile.write("unsigned char %s[] PROGMEM = {" % ident)
tileno = 0
for row in range(0,rows):
yofs = row*height
for col in range(0,columns):
xofs = col*width
outfile.write("\n/* tile number: %d */\n\t" % tileno)
for y in range(0,height):
if bitpack:
bit = 0
byte = 0
for x in range(0,width):
if bit == 8:
outfile.write("0x%02x," % byte)
byte = 0
bit = 0
(r,g,b) = data[xofs+x,yofs+y]
if r > 0 or g > 0 or b > 0: byte = byte | (1 << bit)
bit = bit + 1
outfile.write("0x%02x," % byte)
else:
for x in range(0,width):
#print "(%d,%d) pixel: [%d,%d]" %(col,row,xofs+x,yofs+y)
(r,g,b) = data[xofs+x,yofs+y]
value = ((r & 0xe0) >> 5) | ((g & 0xe0) >> 2) | (b & 0xC0)
outfile.write("0x%02x," % value)
if not wideFormat:
outfile.write("\n\t")
tileno = tileno + 1
outfile.write("};\n")
outfile.close()
elif language == "asm":
outfile = open(filedir+base+".inc","wt+")
outfile.write("; Autogenerated by convertgfx.py - do not modify\n")
outfile.write("; %s - %d tiles each %d by %d pixels\n" % (filename,rows*columns,height,width))
tileno = 0
for row in range(0,rows):
yofs = row*height
for col in range(0,columns):
xofs = col*width
if wideFormat: outfile.write(".byte ")
else: outfile.write("; tile %d\n.byte " % tileno)
for y in range(0,height):
if not wideFormat and y > 0:
outfile.write("\n.byte ")
elif wideFormat and y > 0:
outfile.write(",")
if bitpack:
bit = 0
byte = 0
for x in range(0,width):
if bit == 8:
if x >= 8: outfile.write(",")
outfile.write("0x%02x" % byte)
byte = 0
bit = 0
(r,g,b) = data[xofs+x,yofs+y]
if r > 0 or g > 0 or b > 0: byte = byte | (1 << bit)
bit = bit + 1
if x >= 8: outfile.write(",")
outfile.write("0x%02x" % byte)
else:
for x in range(0,width):
#print "(%d,%d) pixel: [%d,%d]" %(col,row,xofs+x,yofs+y)
(r,g,b) = data[xofs+x,yofs+y]
value = ((r & 0xe0) >> 5) | ((g & 0xe0) >> 2) | (b & 0xC0)
outfile.write("0x%02x" % value)
if x < width-1:
outfile.write(",")
if wideFormat: outfile.write(" ;tile %d\n" % tileno)
else: outfile.write("\n\n")
tileno = tileno + 1
outfile.write("\n")
outfile.close()
note for linux users
python PIL can be installed by doing:
apt-get install python-imaging
Make sure to add "#!/usr/bin/python" as the top of the script. and set the file format to unix, in vi, it's as easy as:
:set fileformat=unix