add function to print full images including resizing

This commit is contained in:
Thomas van den Berg 2013-02-27 15:10:34 +01:00
parent 126dde8a7e
commit b4f8de002c
4 changed files with 44 additions and 2 deletions

View File

@ -1 +1,2 @@
import constants, escpos, exceptions, printer
__all__ = ["constants","escpos","exceptions","printer"]

View File

@ -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")

View File

@ -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

View File

@ -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: