Here is a version of kilobyte's script that reads your file "raw_data.log".
from PIL import Image, ImageDraw
def get_line(f):
"""Extract the binary data following an ESC* command.
This is a VERY naive implementation. Its main problem: The byte sequence
ESC* might appear in the data of some other command that needs binary data.
A proper parser for an ESC/POS data stream would be more robust.
"""
# See https://download4.epson.biz/sec_pubs/pos/reference_en/escpos/esc_asterisk.html
# for a description of the ESC* command.
cc = b'\x00'
while True:
# Search for the ESC* command.
while cc[0] != 27:
cc = f.read(1)
if len(cc) == 0:
# EOF
return None
cc = f.read(1)
if cc != b'*':
continue
# Next byte is called "m" in Epson's doc. Expected to be zero in this
# script. Bail out to prevent any confusion if anther value is read.
cc = f.read(1)
if cc != b'\x00':
raise RuntimeError(
f'Unexpected ESC* parameter: {cc}')
# Two length bytes follow.
linelength = int.from_bytes(f.read(2), 'little')
# ...and finally the bitmap data.
return f.read(linelength)
def get_bitmap(filename):
binary_data = []
with open(filename, 'rb') as f:
line_data = get_line(f)
while line_data != None:
binary_data.append(line_data)
line_data = get_line(f)
return binary_data
def create_bitmap(binary_data):
h = len(binary_data) * 8 # bitmap data 8 pixel height
w = len(binary_data[0])
print("W:", w, "H:", h)
#Create image
image = Image.new('RGB',(w,h),"white")
pixels = image.load()
# create image by setting pixel
idx_height = 0
for row in binary_data:
#print(row)
idx_width = 0
for byt in row:
# draw the 8 bit vertical
for n in range(8):
if (byt & 1<<(7-n)):
pixels[ idx_width, idx_height + n ] = (0,0,0)
idx_width += 1
idx_height += 8
print("save")
#image.save(path + image_name)
print("show")
# display image in viewer
image.show()
def main():
binary_data = get_bitmap('raw_data.log')
create_bitmap(binary_data)
if __name__ == '__main__':
main()
It could be changed to directly read from a serial interface, using the PySerial package. In that case youÄ'd need another way to detect end of the data stream. Maybe search for the byte sequence 0a 1b 33 ff 0a, which seems to appear only at the end of the data sent by the device.
And please take the note from the source code seriously: The extraction of the bitmap data is indeed VERY naive. But perhaps good enough for a start.