Merge 2fb27eabe2fb917ee1a57281c100f4953528ce74 into e5cd37bfbc5f0c70df4767fe37f7d165a6453d06
This commit is contained in:
commit
7e10c68367
200
escpos/cli.py
Executable file
200
escpos/cli.py
Executable file
@ -0,0 +1,200 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
"""A simple command-line interface for common python-escpos functionality
|
||||
|
||||
Usage: python -m escpos.cli --help
|
||||
|
||||
Dependencies:
|
||||
- DavisGoglin/python-escpos or better
|
||||
- A file named weather.png (for the 'test' subcommand)
|
||||
|
||||
Reasons for using the DavisGoglin/python-escpos fork:
|
||||
- image() accepts a PIL.Image object rather than requiring me to choose
|
||||
between writing a temporary file to disk or calling a "private" method.
|
||||
- fullimage() allows me to print images of arbitrary length using slicing.
|
||||
|
||||
How to print unsupported barcodes:
|
||||
barcode -b 'BARCODE' -e 'code39' -E | convert -density 200% eps:- code.png
|
||||
python test_escpos.py --images code.png
|
||||
|
||||
Copyright (C) 2014 Stephan Sokolow (deitarion/SSokolow)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
||||
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "Stephan Sokolow (deitarion/SSokolow)"
|
||||
__license__ = "MIT"
|
||||
|
||||
import re
|
||||
|
||||
from escpos import printer
|
||||
epson = printer.Usb(0x0416, 0x5011)
|
||||
# TODO: Un-hardcode this
|
||||
|
||||
def _print_text_file(path):
|
||||
"""Print the given text file"""
|
||||
epson.set(align='left')
|
||||
with open(path, 'rU') as fobj:
|
||||
for line in fobj:
|
||||
epson.text(line)
|
||||
|
||||
def _print_image_file(path):
|
||||
"""Print the given image file."""
|
||||
epson.fullimage(path, histeq=False, width=384)
|
||||
|
||||
def print_files(args):
|
||||
"""The 'print' subcommand"""
|
||||
for path in args.paths:
|
||||
if args.images:
|
||||
_print_image_file(path)
|
||||
else:
|
||||
_print_text_file(path)
|
||||
epson.cut()
|
||||
|
||||
# {{{ 'echo' Subcommand
|
||||
|
||||
KNOWN_BARCODE_TYPES = ['UPC-A', 'UPC-E', 'EAN13', 'ITF']
|
||||
re_barcode_escape = re.compile(r'^%(?P<type>\S+)\s(?P<data>[0-9X]+)$')
|
||||
|
||||
def echo(args): # pylint: disable=unused-argument
|
||||
"""TTY-like line-by-line keyboard-to-printer echo loop."""
|
||||
try:
|
||||
while True:
|
||||
line = raw_input()
|
||||
match = re_barcode_escape.match(line)
|
||||
if match and match.group('type') in KNOWN_BARCODE_TYPES:
|
||||
bctype, data = match.groups()
|
||||
epson.barcode(data, bctype, 48, 2, '', '')
|
||||
epson.set(align='left')
|
||||
else:
|
||||
epson.text('%s\n' % line)
|
||||
except KeyboardInterrupt:
|
||||
epson.cut()
|
||||
|
||||
# }}}
|
||||
# {{{ 'test' Subcommand
|
||||
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
def _stall_test(width, height):
|
||||
"""Generate a pattern to detect print glitches due to vertical stalling."""
|
||||
img = Image.new('1', (width, height))
|
||||
for pos in [(x, y) for y in range(0, height) for x in range(0, width)]:
|
||||
img.putpixel(pos, not sum(pos) % 10)
|
||||
return img
|
||||
|
||||
def _test_basic():
|
||||
"""The original test code from python-escpos's Usage wiki page"""
|
||||
epson.set(align='left')
|
||||
# Print text
|
||||
epson.text("TODO:\n") # pylint: disable=fixme
|
||||
epson.text("[ ] Task 1\n")
|
||||
epson.text("[ ] Task 2\n")
|
||||
# Print image
|
||||
# TODO: Bundle an image so this can be used
|
||||
# epson.image("weather.png")
|
||||
# Print QR Code (must have a white border to be scanned)
|
||||
epson.set(align='center')
|
||||
epson.text("Scan to recall TODO list") # pylint: disable=fixme
|
||||
epson.qr("http://www.example.com/")
|
||||
# Print barcode
|
||||
epson.barcode('1234567890128', 'EAN13', 32, 2, '', '')
|
||||
# Cut paper
|
||||
epson.cut()
|
||||
|
||||
def _test_barcodes():
|
||||
"""Print test barcodes for all ESCPOS-specified formats."""
|
||||
for name, data in (
|
||||
# pylint: disable=bad-continuation
|
||||
('UPC-A', '123456789012\x00'),
|
||||
('UPC-E', '02345036\x00'),
|
||||
('EAN13', '1234567890128\x00'),
|
||||
('EAN8', '12345670\x00'),
|
||||
('CODE39', 'BARCODE12345678\x00'),
|
||||
('ITF', '123456\x00'),
|
||||
('CODABAR', 'A40156B'),
|
||||
# TODO: CODE93 and CODE128
|
||||
):
|
||||
# TODO: Fix the library to restore old alignment somehow
|
||||
epson.set(align='center')
|
||||
epson.text('\n%s\n' % name)
|
||||
epson.barcode(data, name, 64, 2, '', '')
|
||||
|
||||
def _test_patterns(width=384, height=255):
|
||||
"""Print a set of test patterns for raster image output."""
|
||||
# Test our guess of the paper width
|
||||
img = Image.new('1', (width, height), color=1)
|
||||
draw = ImageDraw.Draw(img)
|
||||
draw.polygon(((0, 0), img.size, (0, img.size[1])), fill=0)
|
||||
epson.image(img)
|
||||
del draw, img
|
||||
|
||||
# Test the consistency of printing large data and whether stall rate is
|
||||
# affected by data rate
|
||||
epson.image(_stall_test(width, height))
|
||||
epson.image(_stall_test(width / 2, height))
|
||||
|
||||
def test(args):
|
||||
"""The 'test' subcommand"""
|
||||
if args.barcodes:
|
||||
_test_barcodes()
|
||||
elif args.patterns:
|
||||
_test_patterns()
|
||||
else:
|
||||
_test_basic()
|
||||
|
||||
# }}}
|
||||
|
||||
def main():
|
||||
"""Wrapped in a function for import and entry point compatibility"""
|
||||
# pylint: disable=bad-continuation
|
||||
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Command-line interface to python-escpos")
|
||||
subparsers = parser.add_subparsers(title='subcommands')
|
||||
|
||||
echo_parser = subparsers.add_parser('echo', help='Echo the keyboard to '
|
||||
'the printer line-by-line (Exit with Ctrl+C)')
|
||||
echo_parser.set_defaults(func=echo)
|
||||
|
||||
print_parser = subparsers.add_parser('print', help='Print the given files')
|
||||
print_parser.add_argument('--images', action='store_true',
|
||||
help="Provided files are images rather than text files.")
|
||||
print_parser.add_argument('paths', metavar='path', nargs='+')
|
||||
print_parser.set_defaults(func=print_files)
|
||||
|
||||
test_parser = subparsers.add_parser('test', help='Print test patterns')
|
||||
test_modes = test_parser.add_mutually_exclusive_group()
|
||||
test_modes.add_argument('--barcodes', action='store_true',
|
||||
help="Test supported barcode types (Warning: Some printers must be "
|
||||
"reset after attempting an unsupported barcode type.)")
|
||||
test_modes.add_argument('--patterns', action='store_true',
|
||||
help="Print test patterns")
|
||||
test_parser.set_defaults(func=test)
|
||||
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
# vim: set sw=4 sts=4 :
|
@ -1,86 +1,98 @@
|
||||
""" ESC/POS Commands (Constants) """
|
||||
|
||||
# Control characters
|
||||
# as labelled in http://www.novopos.ch/client/EPSON/TM-T20/TM-T20_eng_qr.pdf
|
||||
NUL = '\x00'
|
||||
EOT = '\x04'
|
||||
ENQ = '\x05'
|
||||
DLE = '\x10'
|
||||
DC4 = '\x14'
|
||||
CAN = '\x18'
|
||||
ESC = '\x1b'
|
||||
FS = '\x1c'
|
||||
GS = '\x1d'
|
||||
|
||||
# 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_SET_HT = '\x1b\x44' # Set horizontal tab positions
|
||||
CTL_VT = '\x1b\x64\x04' # Vertical tab
|
||||
CTL_SET_HT = ESC + '\x44' # Set horizontal tab positions
|
||||
CTL_VT = ESC + '\x64\x04' # 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 = ESC + '\x40' # Clear data in buffer and reset modes
|
||||
HW_SELECT = ESC + '\x3d\x01' # Printer select
|
||||
HW_RESET = ESC + '\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 = ESC + '\x70\x00' # Sends a pulse to pin 2 []
|
||||
CD_KICK_5 = ESC + '\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 = GS + '\x56\x00' # Full cut paper
|
||||
PAPER_PART_CUT = GS + '\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 = ESC + '\x21\x00' # Normal text
|
||||
TXT_2HEIGHT = ESC + '\x21\x10' # Double height text
|
||||
TXT_2WIDTH = ESC + '\x21\x20' # Double width text
|
||||
TXT_4SQUARE = ESC + '\x21\x30' # Quad area text
|
||||
TXT_UNDERL_OFF = ESC + '\x2d\x00' # Underline font OFF
|
||||
TXT_UNDERL_ON = ESC + '\x2d\x01' # Underline font 1-dot ON
|
||||
TXT_UNDERL2_ON = ESC + '\x2d\x02' # Underline font 2-dot ON
|
||||
TXT_BOLD_OFF = ESC + '\x45\x00' # Bold font OFF
|
||||
TXT_BOLD_ON = ESC + '\x45\x01' # Bold font ON
|
||||
TXT_FONT_A = ESC + '\x4d\x00' # Font type A
|
||||
TXT_FONT_B = ESC + '\x4d\x01' # Font type B
|
||||
TXT_ALIGN_LT = ESC + '\x61\x00' # Left justification
|
||||
TXT_ALIGN_CT = ESC + '\x61\x01' # Centering
|
||||
TXT_ALIGN_RT = ESC + '\x61\x02' # Right justification
|
||||
# Char code table
|
||||
CHARCODE_PC437 = '\x1b\x74\x00' # USA: Standard Europe
|
||||
CHARCODE_JIS = '\x1b\x74\x01' # Japanese Katakana
|
||||
CHARCODE_PC850 = '\x1b\x74\x02' # Multilingual
|
||||
CHARCODE_PC860 = '\x1b\x74\x03' # Portuguese
|
||||
CHARCODE_PC863 = '\x1b\x74\x04' # Canadian-French
|
||||
CHARCODE_PC865 = '\x1b\x74\x05' # Nordic
|
||||
CHARCODE_WEU = '\x1b\x74\x06' # Simplified Kanji, Hirakana
|
||||
CHARCODE_GREEK = '\x1b\x74\x07' # Simplified Kanji
|
||||
CHARCODE_HEBREW = '\x1b\x74\x08' # Simplified Kanji
|
||||
CHARCODE_PC1252 = '\x1b\x74\x11' # Western European Windows Code Set
|
||||
CHARCODE_PC866 = '\x1b\x74\x12' # Cirillic #2
|
||||
CHARCODE_PC852 = '\x1b\x74\x13' # Latin 2
|
||||
CHARCODE_PC858 = '\x1b\x74\x14' # Euro
|
||||
CHARCODE_THAI42 = '\x1b\x74\x15' # Thai character code 42
|
||||
CHARCODE_THAI11 = '\x1b\x74\x16' # Thai character code 11
|
||||
CHARCODE_THAI13 = '\x1b\x74\x17' # Thai character code 13
|
||||
CHARCODE_THAI14 = '\x1b\x74\x18' # Thai character code 14
|
||||
CHARCODE_THAI16 = '\x1b\x74\x19' # Thai character code 16
|
||||
CHARCODE_THAI17 = '\x1b\x74\x1a' # Thai character code 17
|
||||
CHARCODE_THAI18 = '\x1b\x74\x1b' # Thai character code 18
|
||||
CHARCODE_PC437 = ESC + '\x74\x00' # USA: Standard Europe
|
||||
CHARCODE_JIS = ESC + '\x74\x01' # Japanese Katakana
|
||||
CHARCODE_PC850 = ESC + '\x74\x02' # Multilingual
|
||||
CHARCODE_PC860 = ESC + '\x74\x03' # Portuguese
|
||||
CHARCODE_PC863 = ESC + '\x74\x04' # Canadian-French
|
||||
CHARCODE_PC865 = ESC + '\x74\x05' # Nordic
|
||||
CHARCODE_WEU = ESC + '\x74\x06' # Simplified Kanji, Hirakana
|
||||
CHARCODE_GREEK = ESC + '\x74\x07' # Simplified Kanji
|
||||
CHARCODE_HEBREW = ESC + '\x74\x08' # Simplified Kanji
|
||||
CHARCODE_PC1252 = ESC + '\x74\x11' # Western European Windows Code Set
|
||||
CHARCODE_PC866 = ESC + '\x74\x12' # Cirillic #2
|
||||
CHARCODE_PC852 = ESC + '\x74\x13' # Latin 2
|
||||
CHARCODE_PC858 = ESC + '\x74\x14' # Euro
|
||||
CHARCODE_THAI42 = ESC + '\x74\x15' # Thai character code 42
|
||||
CHARCODE_THAI11 = ESC + '\x74\x16' # Thai character code 11
|
||||
CHARCODE_THAI13 = ESC + '\x74\x17' # Thai character code 13
|
||||
CHARCODE_THAI14 = ESC + '\x74\x18' # Thai character code 14
|
||||
CHARCODE_THAI16 = ESC + '\x74\x19' # Thai character code 16
|
||||
CHARCODE_THAI17 = ESC + '\x74\x1a' # Thai character code 17
|
||||
CHARCODE_THAI18 = ESC + '\x74\x1b' # Thai character code 18
|
||||
# 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 = GS + '\x48\x00' # HRI barcode chars OFF
|
||||
BARCODE_TXT_ABV = GS + '\x48\x01' # HRI barcode chars above
|
||||
BARCODE_TXT_BLW = GS + '\x48\x02' # HRI barcode chars below
|
||||
BARCODE_TXT_BTH = GS + '\x48\x03' # HRI barcode chars both above and below
|
||||
BARCODE_FONT_A = GS + '\x66\x00' # Font type A for HRI barcode chars
|
||||
BARCODE_FONT_B = GS + '\x66\x01' # Font type B for HRI barcode chars
|
||||
BARCODE_HEIGHT = GS + '\x68\x64' # Barcode Height [1-255]
|
||||
BARCODE_WIDTH = GS + '\x77\x03' # Barcode Width [2-6]
|
||||
BARCODE_UPC_A = GS + '\x6b\x00' # Barcode type UPC-A
|
||||
BARCODE_UPC_E = GS + '\x6b\x01' # Barcode type UPC-E
|
||||
BARCODE_EAN13 = GS + '\x6b\x02' # Barcode type EAN13
|
||||
BARCODE_EAN8 = GS + '\x6b\x03' # Barcode type EAN8
|
||||
BARCODE_CODE39 = GS + '\x6b\x04' # Barcode type CODE39
|
||||
BARCODE_ITF = GS + '\x6b\x05' # Barcode type ITF
|
||||
BARCODE_NW7 = GS + '\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 = GS + '\x76\x30\x00' # Set raster image normal size
|
||||
S_RASTER_2W = GS + '\x76\x30\x01' # Set raster image double width
|
||||
S_RASTER_2H = GS + '\x76\x30\x02' # Set raster image double height
|
||||
S_RASTER_Q = GS + '\x76\x30\x03' # Set raster image quadruple
|
||||
# Printing Density
|
||||
PD_N50 = '\x1d\x7c\x00' # Printing Density -50%
|
||||
PD_N37 = '\x1d\x7c\x01' # Printing Density -37.5%
|
||||
PD_N25 = '\x1d\x7c\x02' # Printing Density -25%
|
||||
PD_N12 = '\x1d\x7c\x03' # Printing Density -12.5%
|
||||
PD_0 = '\x1d\x7c\x04' # Printing Density 0%
|
||||
PD_P50 = '\x1d\x7c\x08' # Printing Density +50%
|
||||
PD_P37 = '\x1d\x7c\x07' # Printing Density +37.5%
|
||||
PD_P25 = '\x1d\x7c\x06' # Printing Density +25%
|
||||
PD_P12 = '\x1d\x7c\x05' # Printing Density +12.5%
|
||||
PD_N50 = GS + '\x7c\x00' # Printing Density -50%
|
||||
PD_N37 = GS + '\x7c\x01' # Printing Density -37.5%
|
||||
PD_N25 = GS + '\x7c\x02' # Printing Density -25%
|
||||
PD_N12 = GS + '\x7c\x03' # Printing Density -12.5%
|
||||
PD_0 = GS + '\x7c\x04' # Printing Density 0%
|
||||
PD_P50 = GS + '\x7c\x08' # Printing Density +50%
|
||||
PD_P37 = GS + '\x7c\x07' # Printing Density +37.5%
|
||||
PD_P25 = GS + '\x7c\x06' # Printing Density +25%
|
||||
PD_P12 = GS + '\x7c\x05' # Printing Density +12.5%
|
||||
|
159
escpos/escpos.py
159
escpos/escpos.py
@ -6,15 +6,14 @@
|
||||
@license: GNU GPL v3
|
||||
"""
|
||||
|
||||
try:
|
||||
import Image
|
||||
except ImportError:
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
import struct
|
||||
import qrcode
|
||||
import time
|
||||
import textwrap
|
||||
import binascii
|
||||
import operator
|
||||
|
||||
from .constants import *
|
||||
from .exceptions import *
|
||||
@ -59,98 +58,85 @@ class Escpos(object):
|
||||
else:
|
||||
return (round(image_border / 2), round((image_border / 2) + 1))
|
||||
|
||||
def _print_image(self, line, size):
|
||||
def _print_image(self, imagedata, n_rows, col_bytes):
|
||||
""" Print formatted image
|
||||
|
||||
|
||||
:param line:
|
||||
:param size:
|
||||
"""
|
||||
i = 0
|
||||
cont = 0
|
||||
pbuffer = ""
|
||||
|
||||
self._raw(S_RASTER_N)
|
||||
pbuffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8)
|
||||
self._raw(binascii.unhexlify(pbuffer))
|
||||
pbuffer = struct.pack('<HH', col_bytes, n_rows)
|
||||
self._raw(pbuffer)
|
||||
self._raw(imagedata)
|
||||
pbuffer = ""
|
||||
|
||||
while i < len(line):
|
||||
hex_string = int(line[i:i+8], 2)
|
||||
pbuffer += "%02X" % hex_string
|
||||
i += 8
|
||||
cont += 1
|
||||
if cont % 4 == 0:
|
||||
self._raw(binascii.unhexlify(pbuffer))
|
||||
pbuffer = ""
|
||||
cont = 0
|
||||
def fullimage(self, img, max_height=860, width=512, histeq=True, bandsize=255):
|
||||
""" Resizes and prints an arbitrarily sized image """
|
||||
if isinstance(img, Image.Image):
|
||||
im = img.convert("RGB")
|
||||
else:
|
||||
im = Image.open(img).convert("RGB")
|
||||
|
||||
def _convert_image(self, im):
|
||||
""" Parse image and prepare it to a printable format
|
||||
if histeq:
|
||||
# Histogram equaliztion
|
||||
h = im.histogram()
|
||||
lut = []
|
||||
for b in range(0, len(h), 256):
|
||||
# step size
|
||||
step = reduce(operator.add, h[b:b+256]) // 255
|
||||
# create equalization lookup table
|
||||
n = 0
|
||||
for i in range(256):
|
||||
lut.append(n // step)
|
||||
n = n + h[i+b]
|
||||
im = im.point(lut)
|
||||
|
||||
:param im: image data
|
||||
:raises: ImageSizeError
|
||||
"""
|
||||
if width:
|
||||
ratio = float(width) / im.size[0]
|
||||
newheight = int(ratio * im.size[1])
|
||||
|
||||
# Resize the image
|
||||
im = im.resize((width, newheight), Image.ANTIALIAS)
|
||||
|
||||
if max_height and im.size[1] > max_height:
|
||||
im = im.crop((0, 0, im.size[0], max_height))
|
||||
|
||||
# Divide into bands
|
||||
current = 0
|
||||
while current < im.size[1]:
|
||||
self.image(im.crop((0, current, width or im.size[0],
|
||||
min(im.size[1], current + bandsize))))
|
||||
current += bandsize
|
||||
|
||||
def image(self, im):
|
||||
""" Parse image and prepare it to a printable format """
|
||||
pixels = []
|
||||
pix_line = ""
|
||||
im_left = ""
|
||||
im_right = ""
|
||||
switch = 0
|
||||
img_size = [0, 0]
|
||||
img_size = [ 0, 0 ]
|
||||
|
||||
if im.size[0] > 512:
|
||||
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
|
||||
if im.size[1] > 0xffff:
|
||||
if not isinstance(im, Image.Image):
|
||||
im = Image.open(im)
|
||||
|
||||
im = im.convert("L")
|
||||
im = ImageOps.invert(im)
|
||||
im = im.convert("1")
|
||||
|
||||
if im.size[0] > 640:
|
||||
print("WARNING: Image is wider than 640 and could be truncated at print time ")
|
||||
if im.size[1] > 640:
|
||||
raise ImageSizeError()
|
||||
|
||||
im_border = self._check_image_size(im.size[0])
|
||||
for i in range(im_border[0]):
|
||||
im_left += "0"
|
||||
for i in range(im_border[1]):
|
||||
im_right += "0"
|
||||
orig_width, height = im.size
|
||||
width = ((orig_width + 31) // 32) * 32
|
||||
new_image = Image.new("1", (width, height))
|
||||
new_image.paste(im, (0, 0, orig_width, height))
|
||||
|
||||
for y in range(im.size[1]):
|
||||
img_size[1] += 1
|
||||
pix_line += im_left
|
||||
img_size[0] += im_border[0]
|
||||
for x in range(im.size[0]):
|
||||
img_size[0] += 1
|
||||
RGB = im.getpixel((x, y))
|
||||
im_color = (RGB[0] + RGB[1] + RGB[2])
|
||||
im_pattern = "1X0"
|
||||
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_pattern[x] == "X":
|
||||
pix_line += "%d" % switch
|
||||
else:
|
||||
pix_line += im_pattern[x]
|
||||
break
|
||||
elif (255 * 3 / pattern_len * pattern_len) < im_color <= (255 * 3):
|
||||
pix_line += im_pattern[-1]
|
||||
break
|
||||
pix_line += im_right
|
||||
img_size[0] += im_border[1]
|
||||
|
||||
self._print_image(pix_line, img_size)
|
||||
|
||||
def image(self, path_img):
|
||||
""" Open image file
|
||||
|
||||
:param path_img: path to image
|
||||
"""
|
||||
im_open = Image.open(path_img)
|
||||
|
||||
# Remove the alpha channel on transparent images
|
||||
if im_open.mode == 'RGBA':
|
||||
im_open.load()
|
||||
im = Image.new("RGB", im_open.size, (255, 255, 255))
|
||||
im.paste(im_open, mask=im_open.split()[3])
|
||||
else:
|
||||
im = im_open.convert("RGB")
|
||||
|
||||
# Convert the RGB image in printable image
|
||||
self._convert_image(im)
|
||||
the_bytes = new_image.tobytes()
|
||||
self._print_image(the_bytes, n_rows=height, col_bytes=width//8)
|
||||
|
||||
def direct_image(self, image):
|
||||
""" Send image to printer"""
|
||||
@ -185,19 +171,26 @@ class Escpos(object):
|
||||
self._raw(binascii.unhexlify(bytes(buf, "ascii")))
|
||||
self._raw('\n')
|
||||
|
||||
def qr(self, text):
|
||||
def qr(self, text, error_correction=qrcode.constants.ERROR_CORRECT_M):
|
||||
""" Print QR Code for the provided string
|
||||
|
||||
:param text: text to generate a QR-Code from
|
||||
"""
|
||||
qr_code = qrcode.QRCode(version=4, box_size=4, border=1)
|
||||
qr_code = qrcode.QRCode(version=4, box_size=4, border=1, error_correction=error_correction)
|
||||
qr_code.add_data(text)
|
||||
qr_code.make(fit=True)
|
||||
qr_img = qr_code.make_image()
|
||||
im = qr_img._img.convert("RGB")
|
||||
|
||||
# Convert the RGB image in printable image
|
||||
self._convert_image(im)
|
||||
im = qr_img._img.convert("1")
|
||||
width = im.size[0]
|
||||
height = im.size[1]
|
||||
while width * 2 <= 640:
|
||||
width *= 2
|
||||
height *= 2
|
||||
|
||||
im = im.resize((width, height))
|
||||
self.image(im)
|
||||
self.text('\n')
|
||||
|
||||
def charcode(self, code):
|
||||
""" Set Character Code Table
|
||||
@ -302,7 +295,7 @@ class Escpos(object):
|
||||
self._raw(BARCODE_CODE39)
|
||||
elif bc.upper() == "ITF":
|
||||
self._raw(BARCODE_ITF)
|
||||
elif bc.upper() == "NW7":
|
||||
elif bc.upper() in ("NW7", "CODABAR"):
|
||||
self._raw(BARCODE_NW7)
|
||||
else:
|
||||
raise BarcodeTypeError()
|
||||
|
@ -146,7 +146,7 @@ class Network(Escpos):
|
||||
|
||||
def _raw(self, msg):
|
||||
""" Print any command sent in raw format """
|
||||
self.device.send(msg)
|
||||
self.device.sendall(msg)
|
||||
|
||||
def __del__(self):
|
||||
""" Close TCP connection """
|
||||
@ -177,9 +177,6 @@ class File(Escpos):
|
||||
|
||||
def _raw(self, msg):
|
||||
""" Print any command sent in raw format """
|
||||
if type(msg) is str:
|
||||
self.device.write(msg.encode());
|
||||
else:
|
||||
self.device.write(msg);
|
||||
|
||||
def __del__(self):
|
||||
|
9
escpos/utils.py
Normal file
9
escpos/utils.py
Normal file
@ -0,0 +1,9 @@
|
||||
try:
|
||||
bytes.fromhex
|
||||
def hex2bytes(hex_string):
|
||||
return bytes.fromhex(hex_string)
|
||||
|
||||
except:
|
||||
def hex2bytes(hex_string):
|
||||
return hex_string.decode('hex')
|
||||
|
Loading…
x
Reference in New Issue
Block a user