INFILE = 'infile.bmp' OUTFILE = 'outfile' # .chr from PIL import Image im = Image.open(INFILE) ch = open(OUTFILE + '.chr', 'wb') assert im.size == (256, 112) l = list(im.getdata()) for i in range(256): cx, cy = (i % 32) * 8, (i / 32) * 14 for j in range(14): pos = (cy + j) * 256 + cx byte = l[pos:pos+8] byte = sum(int(x > 0) << 7 - i for (i, x) in enumerate(byte)) ch.write(chr(byte)) ch.close()
Image to 80x50 .pal and .mzm:
INFILE = 'infile' OUTFILE = 'outfile' # .mzm and .pal import struct from PIL import Image def palettecolors(im): l = im.resize((16, 1)) l.putdata(range(16)) return l.convert('RGB').getdata() im = Image.open(INFILE).convert('P', palette=Image.ADAPTIVE, colors=16) width, height = im.size # .pal pal = [] for c in palettecolors(im): for i in c: pal.append(chr(i/4)) pal = ''.join(pal) open(OUTFILE + '.pal', 'wb').write(pal) # .mzm mzm = open(OUTFILE + '.mzm', 'wb') mzm.write('MZM2') mzm.write(struct.pack('<2H', width, height / 2)) mzm.write('\0\0\0\0\0\1\0\0') data = list(im.getdata()) for i in range(0, height, 2): top = data[(i + 0) * width:(i + 1) * width] bottom = data[(i + 1) * width:(i + 2) * width] for bg, fg in zip(top, bottom): mzm.write(struct.pack('2B', 0xDC, fg + (bg << 4))) mzm.close()
The latter is pretty specific, but hey. Also, you'll need PIL: http://www.pythonwar...m/products/pil/
This post has been edited by nooodl: 11 October 2011 - 08:50 PM