Skip to content

Instantly share code, notes, and snippets.

@aminhusni
Last active January 12, 2019 18:35
Show Gist options
  • Save aminhusni/d4b6a5b02b415007fa063515d98f5c69 to your computer and use it in GitHub Desktop.
Save aminhusni/d4b6a5b02b415007fa063515d98f5c69 to your computer and use it in GitHub Desktop.
Convert raw image to binary file for thermal Nippon Printer
#This was made specifically using the spec sheet for NP-2511D (2 inch version)
#Spec sheet can be found here
#https://archive.org/details/NP2511D23511D2DF10116COMR100P1ENG
print("")
print("This program prepares your raw image file for thermal printing")
print("Insert your .raw file and convert into .txt binary file")
print("")
fileinput = input('Input file: ')
width = int(input('Width in px (must be multiple of 8): '))
height = int(input('Height in px: '))
cutflag = input('Do you want to cut after printing? (y/n): ')
print("Processing...")
fileoutput = fileinput[:-3] + "txt"
in_file = open(fileinput, 'rb')
out_file = open(fileoutput, 'wb')
widthhex = width/8
#Convert back to int without decimal place
widthhex = int(widthhex)
#Convert to little endian
heighthex = height.to_bytes(2, 'little')
widthhex = [widthhex]
heighthex = [heighthex[0], heighthex[1]]
settings = [0x1d, 0x57, 0x00, 0x4d, 0x1d, 0x53, 0x00]
printheader = [0x1b, 0x62]
cut = [0x1b, 0x69]
settings.extend(printheader)
settings.extend(widthhex)
settings.extend(heighthex)
out_file.write(bytes(settings))
while True:
eight_bytes = in_file.read(8)
if len(eight_bytes) == 0:
break
current_byte = 0x00
for i in range(8):
current_byte |= eight_bytes[i] << (7 - i)
current_byte ^= 0xFF
out_file.write(bytes([current_byte]))
if cutflag == "y":
out_file.write(bytes(cut))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment