diff --git a/escpos/__init__.py b/escpos/__init__.py index 0459ce8..8f74f5c 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 7e11ada..2ce4c57 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -18,6 +18,8 @@ import qrcode import time import textwrap import binascii +import os +import operator from .constants import * from .exceptions import * @@ -161,6 +163,42 @@ class Escpos(object): # Convert the RGB image in printable image self._convert_image(im) + def fullimage(self, img, max_height=860, width=512, histeq=True): + """ Resizes and prints an arbitrarily sized image """ + if isinstance(img, (Image, 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, min(im.size[1], current + bandsize)))) + current += bandsize + def direct_image(self, image): """ Send image to printer diff --git a/escpos/exceptions.py b/escpos/exceptions.py index c960387..0a24ffc 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -11,6 +11,7 @@ Result/Exit codes: - `60` = Invalid pin to send Cash Drawer pulse :py:exc:`~escpos.exceptions.CashDrawerError` - `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError` - `80` = Invalid char code :py:exc:`~escpos.exceptions.CharCodeError` + - `90` = USB device not found :py:exc:`~escpos.exceptions.USBNotFoundError` :author: `Manuel F Martinez `_ and others :organization: Bashlinux and `python-escpos `_ @@ -151,3 +152,17 @@ class CharCodeError(Error): def __str__(self): return "Valid char code must be set" + +class USBNotFoundError(Error): + """ Device wasn't found (probably not plugged in) + + The USB device seems to be not plugged in. + Ths returncode for this exception is `90`. + """ + def __init__(self, msg=""): + Error.__init__(self, msg) + self.msg = msg + self.resultcode = 90 + + def __str__(self): + return "USB device not found" diff --git a/escpos/printer.py b/escpos/printer.py index 07e7011..1f95665 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -43,7 +43,7 @@ class Usb(Escpos): """ Search device on USB tree and set it 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 USBNotFoundError("Device not found or cable not plugged in.") check_driver = None