From 0ef2951c7e187719b99a4ac0912955a6d203d448 Mon Sep 17 00:00:00 2001 From: Christoph Heuel Date: Wed, 10 Jun 2015 03:21:52 +0200 Subject: [PATCH] Introduce new direct_image * The direct_image method prints directly from the PIL image object * The image should be converted to 1 bit before * The method was derived from png2escpos (https://github.com/twg/png2escpos) --- escpos/escpos.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/escpos/escpos.py b/escpos/escpos.py index 810167e..9d37709 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -14,6 +14,7 @@ except ImportError: import qrcode import time import textwrap +import binascii from .constants import * from .exceptions import * @@ -114,6 +115,38 @@ class Escpos: # Convert the RGB image in printable image self._convert_image(im) + def direct_image(self, image): + """ Send image to printer""" + mask = 0x80 + i = 0 + temp = 0 + + (width, height) = image.size + self._raw(S_RASTER_N) + headerX = int(width / 8) + headerY = height + buf = "%02X" % (headerX & 0xff) + buf += "%02X" % ((headerX >> 8) & 0xff) + buf += "%02X" % (headerY & 0xff) + buf += "%02X" % ((headerY >> 8) & 0xff) + #self._raw(binascii.unhexlify(buf)) + for y in range(height): + for x in range(width): + value = image.getpixel((x,y)) + value = (value << 8) | value; + if value == 0: + temp |= mask + + mask = mask >> 1 + + i += 1 + if i == 8: + buf += ("%02X" % temp) + mask = 0x80 + i = 0 + temp = 0 + self._raw(binascii.unhexlify(buf)) + def qr(self,text): """ Print QR Code for the provided string """