From b4f8de002ccc8e87c0233378496303514697f40f Mon Sep 17 00:00:00 2001 From: Thomas van den Berg Date: Wed, 27 Feb 2013 15:10:34 +0100 Subject: [PATCH] add function to print full images including resizing --- escpos/__init__.py | 1 + escpos/escpos.py | 40 +++++++++++++++++++++++++++++++++++++++- escpos/exceptions.py | 3 +++ escpos/printer.py | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/escpos/__init__.py b/escpos/__init__.py index 22a5af6..956f246 100644 --- a/escpos/__init__.py +++ b/escpos/__init__.py @@ -1 +1,2 @@ +import constants, escpos, exceptions, printer __all__ = ["constants","escpos","exceptions","printer"] diff --git a/escpos/escpos.py b/escpos/escpos.py index d7ea18e..caf4ef3 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -7,8 +7,10 @@ ''' import Image +import PIL.Image import time import os +import operator from constants import * from exceptions import * @@ -51,6 +53,42 @@ class Escpos: buffer = "" cont = 0 + def fullimage(self, img, max_height=860, width=512, histeq=True): + """ Resizes and prints an arbitrarily sized image """ + if isinstance(img, (Image.Image, PIL.Image.Image)): + im = img.convert("RGB") + else: + im = Image.open(img).convert("RGB") + + 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) + + ratio = float(width) / im.size[0] + newheight = int(ratio * im.size[1]) + + # Resize the image + im = im.resize((width, newheight), Image.ANTIALIAS) + if im.size[1] > max_height: + im = im.crop((0, 0, im.size[0], max_height)) + + # Divide into bands + bandsize = 255 + current = 0 + while current < im.size[1]: + self.image(im.crop((0, current, width, current + bandsize))) + current += bandsize + def image(self, img): """ Parse image and prepare it to a printable format """ @@ -61,7 +99,7 @@ class Escpos: switch = 0 img_size = [ 0, 0 ] - if isinstance(img, Image): + if isinstance(img, (Image.Image, PIL.Image.Image)): im = img.convert("RGB") else: im = Image.open(img).convert("RGB") diff --git a/escpos/exceptions.py b/escpos/exceptions.py index adbe648..caad7a8 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -14,6 +14,9 @@ class Error(Exception): def __str__(self): return self.msg +class NotFoundError(Error): + """ Device wasn't found (not plugged in) """ + # Result/Exit codes # 0 = success # 10 = No Barcode type defined diff --git a/escpos/printer.py b/escpos/printer.py index dd5aa93..7ecd5d0 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -38,7 +38,7 @@ class Usb(Escpos): """ Search device on USB tree and set is as escpos device """ self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) if self.device is None: - print "Cable isn't plugged in" + raise NotFoundError("Device not found or cable not plugged in.") if self.device.is_kernel_driver_active(0): try: