-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptprint.py
76 lines (51 loc) · 1.67 KB
/
ptprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import argparse
import binascii
def mirrorBits(byte):
return chr(int(format(int(binascii.hexlify(byte), 16), '09b')[8:0:-1], 2))
class Image:
def __init__(self, imagedata):
rawwidth = ''
rawlength = ''
n = 3 # set start character of bitmap file for parsing
while imagedata[n] != ' ':
rawwidth += imagedata[n]
n += 1
width = int(rawwidth)
while imagedata[n] != '\n':
rawlength += imagedata[n]
n += 1
self.length = int(rawlength)
self.pixeldata = imagedata[n+1:]
self.width_bytes = width / 8
class PtData:
def __init__(self, imagedata):
self.imagedata = imagedata
def create(self):
image = Image(self.imagedata)
self.data = '\x00' * 100
self.data += '\x1B@'
self.data += '\x1BiM\x40'
self.data += '\x1BiK\x09'
self.data += 'M\x02'
line = 0
while line < image.length:
startByte = line * image.width_bytes
endByte = line * image.width_bytes + image.width_bytes - 1
paddingA = (16 - image.width_bytes) / 2 # padding bytes from one side
paddingB = 16 - image.width_bytes - paddingA # padding bytes from other side
self.data += 'G\x11\x00\x0F' # line initialization
self.data += ('\x00' * paddingA) # padding side A
for j in image.pixeldata[endByte:startByte-1:-1]: # read bytes for line in reverse order
self.data += mirrorBits(j) # add mirrored bits in bytes to print data
self.data += ('\x00' * paddingB) # padding side B
line += 1
self.data += '\x00\x1A'
def getData(self):
return self.data
def main():
ptData = PtData(open('output.pbm', 'rb').read())
ptData.create()
datafile = open('printfile.prn', 'wb').write(ptData.getData())
if __name__ == '__main__':
main()