Questa è una risposta completa per le bitmap monocromatiche.
import math, struct
mult4 = lambda n: int(math.ceil(n/4))*4
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+0x20), [0]*(mult4(wB)-wB)
s = li(mult4(w)*h+0x20)
return (b"BM" + s + b"\x00\x00\x00\x00\x20\x00\x00\x00\x0C\x00\x00\x00" +
lh(w) + lh(h) + b"\x01\x00\x01\x00\xff\xff\xff\x00\x00\x00" +
b"".join([bytes(row+pad) for row in reversed(rows)]))
Ad esempio:
FF XXXXXXXX
81 X......X
A5 X.X..X.X
81 X......X
A5 X.X..X.X
BD X.XXXX.X
81 X......X
FF XXXXXXXX
Quindi, codifica questo come una serie di righe:
smile = [[0xFF], [0x81], [0xA5], [0x81], [0xA5], [0xBD], [0x81], [0xFF]]
Render con:
bmp(smile, 8)
noti che è il responsabilità del programmatore di assicurare che il nu mber di byte sono presenti in ogni riga fornita.
Il colore nero è specificato in \ xff \ xff \ xff e il colore bianco è specificato nel seguente \ x00 \ x00 \ x00, se si desidera cambiarli.
Controlla il modulo ['struct'] (http://docs.python.org/library/struct.html). –
Inoltre, controlla [questo thread del forum] (http://pseentertainmentcorp.com/smf/index.php?topic=2034.0). – Chewie
Grazie, Chewie, è esattamente quello di cui ho bisogno –