diff --git a/escpos/constants.py b/escpos/constants.py index b5fca55..8f533d4 100644 --- a/escpos/constants.py +++ b/escpos/constants.py @@ -1,54 +1,54 @@ """ ESC/POS Commands (Constants) """ # Feed control sequences -CTL_LF = '\x0a' # Print and line feed -CTL_FF = '\x0c' # Form feed -CTL_CR = '\x0d' # Carriage return -CTL_HT = '\x09' # Horizontal tab -CTL_VT = '\x0b' # Vertical tab +CTL_LF = b'\x0a' # Print and line feed +CTL_FF = b'\x0c' # Form feed +CTL_CR = b'\x0d' # Carriage return +CTL_HT = b'\x09' # Horizontal tab +CTL_VT = b'\x0b' # Vertical tab # Printer hardware -HW_INIT = '\x1b\x40' # Clear data in buffer and reset modes -HW_SELECT = '\x1b\x3d\x01' # Printer select -HW_RESET = '\x1b\x3f\x0a\x00' # Reset printer hardware +HW_INIT = b'\x1b\x40' # Clear data in buffer and reset modes +HW_SELECT = b'\x1b\x3d\x01' # Printer select +HW_RESET = b'\x1b\x3f\x0a\x00' # Reset printer hardware # Cash Drawer -CD_KICK_2 = '\x1b\x70\x00' # Sends a pulse to pin 2 [] -CD_KICK_5 = '\x1b\x70\x01' # Sends a pulse to pin 5 [] +CD_KICK_2 = b'\x1b\x70\x00' # Sends a pulse to pin 2 [] +CD_KICK_5 = b'\x1b\x70\x01' # Sends a pulse to pin 5 [] # Paper -PAPER_FULL_CUT = '\x1d\x56\x00' # Full cut paper -PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper +PAPER_FULL_CUT = b'\x1d\x56\x00' # Full cut paper +PAPER_PART_CUT = b'\x1d\x56\x01' # Partial cut paper # Text format -TXT_NORMAL = '\x1b\x21\x00' # Normal text -TXT_2HEIGHT = '\x1b\x21\x10' # Double height text -TXT_2WIDTH = '\x1b\x21\x20' # Double width text -TXT_4SQUARE = '\x1b\x21\x30' # Quad area text -TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF -TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON -TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON -TXT_BOLD_OFF = '\x1b\x45\x00' # Bold font OFF -TXT_BOLD_ON = '\x1b\x45\x01' # Bold font ON -TXT_FONT_A = '\x1b\x4d\x00' # Font type A -TXT_FONT_B = '\x1b\x4d\x01' # Font type B -TXT_ALIGN_LT = '\x1b\x61\x00' # Left justification -TXT_ALIGN_CT = '\x1b\x61\x01' # Centering -TXT_ALIGN_RT = '\x1b\x61\x02' # Right justification +TXT_NORMAL = b'\x1b\x21\x00' # Normal text +TXT_2HEIGHT = b'\x1b\x21\x10' # Double height text +TXT_2WIDTH = b'\x1b\x21\x20' # Double width text +TXT_4SQUARE = b'\x1b\x21\x30' # Quad area text +TXT_UNDERL_OFF = b'\x1b\x2d\x00' # Underline font OFF +TXT_UNDERL_ON = b'\x1b\x2d\x01' # Underline font 1-dot ON +TXT_UNDERL2_ON = b'\x1b\x2d\x02' # Underline font 2-dot ON +TXT_BOLD_OFF = b'\x1b\x45\x00' # Bold font OFF +TXT_BOLD_ON = b'\x1b\x45\x01' # Bold font ON +TXT_FONT_A = b'\x1b\x4d\x00' # Font type A +TXT_FONT_B = b'\x1b\x4d\x01' # Font type B +TXT_ALIGN_LT = b'\x1b\x61\x00' # Left justification +TXT_ALIGN_CT = b'\x1b\x61\x01' # Centering +TXT_ALIGN_RT = b'\x1b\x61\x02' # Right justification # Barcode format -BARCODE_TXT_OFF = '\x1d\x48\x00' # HRI barcode chars OFF -BARCODE_TXT_ABV = '\x1d\x48\x01' # HRI barcode chars above -BARCODE_TXT_BLW = '\x1d\x48\x02' # HRI barcode chars below -BARCODE_TXT_BTH = '\x1d\x48\x03' # HRI barcode chars both above and below -BARCODE_FONT_A = '\x1d\x66\x00' # Font type A for HRI barcode chars -BARCODE_FONT_B = '\x1d\x66\x01' # Font type B for HRI barcode chars -BARCODE_HEIGHT = '\x1d\x68\x64' # Barcode Height [1-255] -BARCODE_WIDTH = '\x1d\x77\x03' # Barcode Width [2-6] -BARCODE_UPC_A = '\x1d\x6b\x00' # Barcode type UPC-A -BARCODE_UPC_E = '\x1d\x6b\x01' # Barcode type UPC-E -BARCODE_EAN13 = '\x1d\x6b\x02' # Barcode type EAN13 -BARCODE_EAN8 = '\x1d\x6b\x03' # Barcode type EAN8 -BARCODE_CODE39 = '\x1d\x6b\x04' # Barcode type CODE39 -BARCODE_ITF = '\x1d\x6b\x05' # Barcode type ITF -BARCODE_NW7 = '\x1d\x6b\x06' # Barcode type NW7 +BARCODE_TXT_OFF = b'\x1d\x48\x00' # HRI barcode chars OFF +BARCODE_TXT_ABV = b'\x1d\x48\x01' # HRI barcode chars above +BARCODE_TXT_BLW = b'\x1d\x48\x02' # HRI barcode chars below +BARCODE_TXT_BTH = b'\x1d\x48\x03' # HRI barcode chars both above and below +BARCODE_FONT_A = b'\x1d\x66\x00' # Font type A for HRI barcode chars +BARCODE_FONT_B = b'\x1d\x66\x01' # Font type B for HRI barcode chars +BARCODE_HEIGHT = b'\x1d\x68\x64' # Barcode Height [1-255] +BARCODE_WIDTH = b'\x1d\x77\x03' # Barcode Width [2-6] +BARCODE_UPC_A = b'\x1d\x6b\x00' # Barcode type UPC-A +BARCODE_UPC_E = b'\x1d\x6b\x01' # Barcode type UPC-E +BARCODE_EAN13 = b'\x1d\x6b\x02' # Barcode type EAN13 +BARCODE_EAN8 = b'\x1d\x6b\x03' # Barcode type EAN8 +BARCODE_CODE39 = b'\x1d\x6b\x04' # Barcode type CODE39 +BARCODE_ITF = b'\x1d\x6b\x05' # Barcode type ITF +BARCODE_NW7 = b'\x1d\x6b\x06' # Barcode type NW7 # Image format -S_RASTER_N = '\x1d\x76\x30\x00' # Set raster image normal size -S_RASTER_2W = '\x1d\x76\x30\x01' # Set raster image double width -S_RASTER_2H = '\x1d\x76\x30\x02' # Set raster image double height -S_RASTER_Q = '\x1d\x76\x30\x03' # Set raster image quadruple +S_RASTER_N = b'\x1d\x76\x30\x00' # Set raster image normal size +S_RASTER_2W = b'\x1d\x76\x30\x01' # Set raster image double width +S_RASTER_2H = b'\x1d\x76\x30\x02' # Set raster image double height +S_RASTER_Q = b'\x1d\x76\x30\x03' # Set raster image quadruple diff --git a/escpos/escpos.py b/escpos/escpos.py index c79bea4..6b3171c 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -27,9 +27,9 @@ class Escpos: else: image_border = 32 - (size % 32) if (image_border % 2) == 0: - return (image_border / 2, image_border / 2) + return (image_border // 2, image_border // 2) else: - return (image_border / 2, (image_border / 2) + 1) + return (image_border // 2, (image_border // 2) + 1) def _print_image(self, line, size): @@ -39,7 +39,7 @@ class Escpos: buffer = "" self._raw(S_RASTER_N) - buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0) + buffer = "%02X%02X%02X%02X" % (((size[0]//size[1])//8), 0, size[1], 0) self._raw(buffer.decode('hex')) buffer = "" @@ -66,11 +66,11 @@ class Escpos: lut = [] for b in range(0, len(h), 256): # step size - step = reduce(operator.add, h[b:b+256]) / 255 + step = reduce(operator.add, h[b:b+256]) // 255 # create equalization lookup table n = 0 for i in range(256): - lut.append(n / step) + lut.append(n // step) n = n + h[i+b] im = im.point(lut) @@ -105,7 +105,7 @@ class Escpos: im = Image.open(img).convert("RGB") if im.size[0] > 512: - print "WARNING: Image is wider than 512 and could be truncated at print time " + print("WARNING: Image is wider than 512 and could be truncated at print time ") if im.size[1] > 255: raise ImageSizeError() @@ -127,13 +127,13 @@ class Escpos: pattern_len = len(im_pattern) switch = (switch - 1 ) * (-1) for x in range(pattern_len): - if im_color <= (255 * 3 / pattern_len * (x+1)): + if im_color <= (255 * 3 // pattern_len * (x+1)): if im_pattern[x] == "X": pix_line += "%d" % switch else: pix_line += im_pattern[x] break - elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3): + elif im_color > (255 * 3 // pattern_len * pattern_len) and im_color <= (255 * 3): pix_line += im_pattern[-1] break pix_line += im_right