In case anybody is interested in the solution to importing the images as a bitmap "font", this is what I did, using the pyglet library and Python 3.3. The money function here is generate_sprite_string() which not only returns the correct images, it also returns them as sprites specifying their positions on the screen relative to the x,y coordinates where you want to plot them. Also maybe of interest is generate_colored_tile(), which generates a new image (not a reference to the image grid) with any foreground/background colors you want.
I haven't yet bothered with implementing a cursor and text input.
import pyglet
"imports a tileset from a 16x16 image grid; includes functions for turning text into ascii-character images/sprites"
# IMPORT GRAPHICS
tilegrid = pyglet.resource.image("curses_800x600.png")
tileset = pyglet.image.ImageGrid(tilegrid,16,16) # split into equal sized tiles, assuming they are in a 16x16 grid
# pyglet starts from the bottom left, not top left, so we need to re-order the list just a bit
tileset = tuple(zip(*[iter(tileset)]*16))[::-1] # this one-liner should split the sequence into (rows) groups of 16 and then reverse their order
#tileset = [tileset[x:x+16] for x in range(0,len(tileset),16)][::-1] # alternate way to do the same thing
#access the images by tileset[row][column]
tw,th = tilegrid.width//16, tilegrid.height//16 # tile width/height, assuming a 16x16 grid
def get_char_tile(a,b=None):
"returns the (white on black) tile for a given ascii code (1 arg) or row,column position (2 args)"
if b==None:
return tileset[a//16][a%16] # "a" indexes tileset as if it were one long array of tiles
else:
return tileset[a][b] # "a,b" indexes row, column
def get_tile_string(text):
"returns a list of tiles for a given string; does not keep track of text wrapping or anything like that"
return [get_char_tile(ord(x)) for x in text]
def generate_sprite_string(text,x,y,batch):
"returns a set of sprites (with position and batch assigned) for a text string; lower-left corner at x,y"
return [pyglet.sprite.Sprite( get_char_tile(ord(text[i])), x=x+(i*tw), y=y, batch=batch) for i in range(len(text))]
def generate_colored_tile(a,b=None,fg_color=b'\xff\xff\xff',bg_color=b'\x00\x00\x00'): #a=ascii code or a,b = row, column position in grid
"returns a new image by transforming the foreground and background colors of one of the default (white on balck) tiles"
pic = get_char_tile(a,b) # get_char_tile works with ascii code or row,column position
width, height = pic.width, pic.height
format = "RGBA"
picdata = pic.image_data.get_data(format,width*4) # as a byte string of RGBA bytes
pixels = [picdata[x:x+4] for x in range(0,len(picdata),4)] # split that string into 4-byte strings representing pixels
blackcolor = b'\x00\x00\x00\xff'
whitecolor = b'\xff\xff\xff\xff'
bgs = [y for y in range(len(pixels)) if pixels[y]==blackcolor] # indexes of background pixels
fgs = [y for y in range(len(pixels)) if pixels[y]==whitecolor] # indexes of foreground pixels
for i in bgs: pixels[i] = bg_color + b'\xff' # transform background; bg_color expects a 3-byte string representing RGB values
for i in fgs: pixels[i] = fg_color + b'\xff' # transform foreground; fg_color expects a 3-byte string representing RGB values
pixel_out = b''
for j in pixels: pixel_out += j # merge back into a single byte string
return pyglet.image.ImageData(width,height,format,pixel_out) # generate a new image with the new data