########################################################################### # # Collection of routines related to Windows BMP images # # ########################################################################### import struct # helper function: converts an integer n to binary string occupying # specified number of bytes b (doesn't check for errors!) def istr(n, b): return struct.pack(' 0] # create a colormap, open image file and write out BMP header def init_image(fname, x, y): f = file(fname, 'w') # BMP header hdr = 'BM'+istr(26+y*(3*x+pad4(3*x)), 4)+4*'\0'+istr(26, 4) # pixmap info (OS/2 V1 header) hdr += istr(12, 4)+istr(x, 2)+istr(y, 2)+istr(1, 2)+istr(24, 2) f.write(hdr) return f, len(hdr) # obtain properly padded buffer containing image row data def padded_row(f, cmap, rdata): row = ''.join(map(lambda x: cmap[x], rdata)) return row+pad4(len(row))*'\0' # store any single row of image in file def write_row(f, hsize, cmap, rnum, rdata): f.seek(hsize+len(rdata)+pad4(3*len(rdata))*rnum) f.write(padded__row(f, cmap, rdata)) # save image in BMP format, converting every pixel into RGB color value def write_image(fname, xs, ys, data, ncolors): cmap = init_cmap(ncolors) f, hsz = init_image(fname, xs, ys) for row in data: f.write(padded_row(f, cmap, row))