mult8 = lambda n: int(math.ceil(n / 8)) * 8
lh = lambda n: struct.pack('<h', n)
li = lambda n: struct.pack('<i', n)

def bmp(rows, w):
    h, wB = len(rows), int(mult8(w) / 8)
s, pad = li(mult4(wB) * h + 32), [0] * (mult4(wB) - wB)
s = li(mult4(w) * h + 32)
return 'BM' + s + '\x00\x00\x00\x00 \x00\x00\x00\x0c\x00\x00\x00' + lh(w) + lh(
    h) + '\x01\x00\x01\x00\xff\xff\xff\x00\x00\x00' + ''.join([bytes(row +
    pad) for row in reversed(rows)])
How do I create a BMP file with pure python?