add function to print full images including resizing and fix band printing
This commit is contained in:
parent
1614298863
commit
598c893943
|
@ -1 +1,2 @@
|
|||
import constants, escpos, exceptions, printer
|
||||
__all__ = ["constants", "escpos", "exceptions", "printer"]
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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 <manpaz@bashlinux.com>`_ and others
|
||||
:organization: Bashlinux and `python-escpos <https://github.com/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"
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue