add function to print full images including resizing
This commit is contained in:
parent
126dde8a7e
commit
b4f8de002c
|
@ -1 +1,2 @@
|
||||||
|
import constants, escpos, exceptions, printer
|
||||||
__all__ = ["constants","escpos","exceptions","printer"]
|
__all__ = ["constants","escpos","exceptions","printer"]
|
||||||
|
|
|
@ -7,8 +7,10 @@
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import Image
|
import Image
|
||||||
|
import PIL.Image
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
import operator
|
||||||
|
|
||||||
from constants import *
|
from constants import *
|
||||||
from exceptions import *
|
from exceptions import *
|
||||||
|
@ -51,6 +53,42 @@ class Escpos:
|
||||||
buffer = ""
|
buffer = ""
|
||||||
cont = 0
|
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):
|
def image(self, img):
|
||||||
""" Parse image and prepare it to a printable format """
|
""" Parse image and prepare it to a printable format """
|
||||||
|
@ -61,7 +99,7 @@ class Escpos:
|
||||||
switch = 0
|
switch = 0
|
||||||
img_size = [ 0, 0 ]
|
img_size = [ 0, 0 ]
|
||||||
|
|
||||||
if isinstance(img, Image):
|
if isinstance(img, (Image.Image, PIL.Image.Image)):
|
||||||
im = img.convert("RGB")
|
im = img.convert("RGB")
|
||||||
else:
|
else:
|
||||||
im = Image.open(img).convert("RGB")
|
im = Image.open(img).convert("RGB")
|
||||||
|
|
|
@ -14,6 +14,9 @@ class Error(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.msg
|
return self.msg
|
||||||
|
|
||||||
|
class NotFoundError(Error):
|
||||||
|
""" Device wasn't found (not plugged in) """
|
||||||
|
|
||||||
# Result/Exit codes
|
# Result/Exit codes
|
||||||
# 0 = success
|
# 0 = success
|
||||||
# 10 = No Barcode type defined
|
# 10 = No Barcode type defined
|
||||||
|
|
|
@ -38,7 +38,7 @@ class Usb(Escpos):
|
||||||
""" Search device on USB tree and set is as escpos device """
|
""" Search device on USB tree and set is as escpos device """
|
||||||
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
|
||||||
if self.device is None:
|
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):
|
if self.device.is_kernel_driver_active(0):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue